func handleLogin(w http.ResponseWriter, r *http.Request) {
	cxt := appengine.NewContext(r)
	chLogin := make(chan *user.Logined)
	defer func() {
		if err := recover(); err != nil {
			cxt.Errorf("handleLogin: %v", err)
			fmt.Fprintf(w, `{"status":%d}`, common.STATUS_ERR)
		}
	}()

	base64Text, _ := ioutil.ReadAll(r.Body)
	if len(base64Text) == 0 {
		panic("Needs login account info.")
		return
	}

	ciphertext := decodeBase64(string(base64Text))
	decrypted := make([]byte, len(ciphertext))
	decryptAESCFB(decrypted, ciphertext, common.KEY, common.IV)
	plainText := string(decrypted)

	data := strings.Split(plainText, "&")
	account := strings.TrimSpace((strings.Split(data[0], "="))[1])
	pwd := strings.TrimSpace((strings.Split(data[1], "="))[1])
	appId := strings.TrimSpace((strings.Split(data[2], "="))[1])
	appSec := strings.TrimSpace((strings.Split(data[3], "="))[1])
	redirectUrl := strings.TrimSpace((strings.Split(data[4], "="))[1])
	scope := strings.TrimSpace((strings.Split(data[5], "="))[1])

	pUser := user.NewOscUser(account, pwd, appId, appSec, redirectUrl, scope)
	go pUser.Login(cxt, chLogin)

	//Get cookie.
	pLogined := <-chLogin
	session := pLogined.Cookie.Value    //Got user session.
	expires := pLogined.Token.ExpiresIn //Time-up for session.
	uid := pLogined.Token.UID
	access_token := pLogined.Token.AccessToken
	s := fmt.Sprintf(`{"status":%d, "user":{"uid":%d, "expired":%d}}`, common.STATUS_OK, uid, expires)

	w.Header().Set("Set-Cookie", "oscid="+session+";access_tokien="+access_token)
	w.Header().Set("Content-Type", common.API_RESTYPE)
	fmt.Fprintf(w, s)
}
//Login a user and store oschina session-id to cookie.
//Use the cookie to access rest APIs.
Login a user and store oschina session-id to cookie.
Use the cookie to access rest APIs.
// Login a user and store oschina session - id to cookie . // Use the cookie to access rest APIs .
func handleLogin(w http.ResponseWriter, r *http.Request) {
	cxt := appengine.NewContext(r)
	chLogin := make(chan *user.Logined)
	defer func() {
		if err := recover(); err != nil {
			cxt.Errorf("handleLogin: %v", err)
			fmt.Fprintf(w, `{"status":%d}`, common.STATUS_ERR)
		}
	}()
	base64Text, _ := ioutil.ReadAll(r.Body)
	if len(base64Text) == 0 {
		panic("Needs login account info.")
		return
	}
	ciphertext := decodeBase64(string(base64Text))
	decrypted := make([]byte, len(ciphertext))
	decryptAESCFB(decrypted, ciphertext, common.KEY, common.IV)
	plainText := string(decrypted)
	data := strings.Split(plainText, "&")
	account := strings.TrimSpace((strings.Split(data[0], "="))[1])
	pwd := strings.TrimSpace((strings.Split(data[1], "="))[1])
	appId := strings.TrimSpace((strings.Split(data[2], "="))[1])
	appSec := strings.TrimSpace((strings.Split(data[3], "="))[1])
	redirectUrl := strings.TrimSpace((strings.Split(data[4], "="))[1])
	scope := strings.TrimSpace((strings.Split(data[5], "="))[1])
	pUser := user.NewOscUser(account, pwd, appId, appSec, redirectUrl, scope)
	go pUser.Login(cxt, chLogin)
	pLogined := <-chLogin
	session := pLogined.Cookie.Value    
	expires := pLogined.Token.ExpiresIn 
	uid := pLogined.Token.UID
	access_token := pLogined.Token.AccessToken
	s := fmt.Sprintf(`{"status":%d, "user":{"uid":%d, "expired":%d}}`, common.STATUS_OK, uid, expires)
	w.Header().Set("Set-Cookie", "oscid="+session+";access_tokien="+access_token)
	w.Header().Set("Content-Type", common.API_RESTYPE)
	fmt.Fprintf(w, s)
}
func handleLogin ( w http . ResponseWriter , r * http . Request ) { cxt := appengine . NewContext ( r ) chLogin := make ( chan * user . Logined ) defer func ( ) { if err := recover ( ) ; err != nil { cxt . Errorf ( " " , err ) fmt . Fprintf ( w , `{"status":%d}` , common . STATUS_ERR ) } } ( ) base64Text , _ := ioutil . ReadAll ( r . Body ) if len ( base64Text ) == 0 { panic ( " " ) return } ciphertext := decodeBase64 ( string ( base64Text ) ) decrypted := make ( [ ] byte , len ( ciphertext ) ) decryptAESCFB ( decrypted , ciphertext , common . KEY , common . IV ) plainText := string ( decrypted ) data := strings . Split ( plainText , " " ) account := strings . TrimSpace ( ( strings . Split ( data [ 0 ] , " " ) ) [ 1 ] ) pwd := strings . TrimSpace ( ( strings . Split ( data [ 1 ] , " " ) ) [ 1 ] ) appId := strings . TrimSpace ( ( strings . Split ( data [ 2 ] , " " ) ) [ 1 ] ) appSec := strings . TrimSpace ( ( strings . Split ( data [ 3 ] , " " ) ) [ 1 ] ) redirectUrl := strings . TrimSpace ( ( strings . Split ( data [ 4 ] , " " ) ) [ 1 ] ) scope := strings . TrimSpace ( ( strings . Split ( data [ 5 ] , " " ) ) [ 1 ] ) pUser := user . NewOscUser ( account , pwd , appId , appSec , redirectUrl , scope ) go pUser . Login ( cxt , chLogin ) pLogined := <- chLogin session := pLogined . Cookie . Value expires := pLogined . Token . ExpiresIn uid := pLogined . Token . UID access_token := pLogined . Token . AccessToken s := fmt . Sprintf ( `{"status":%d, "user":{"uid":%d, "expired":%d}}` , common . STATUS_OK , uid , expires ) w . Header ( ) . Set ( " " , " " + session + " " + access_token ) w . Header ( ) . Set ( " " , common . API_RESTYPE ) fmt . Fprintf ( w , s ) }
Login a user and store oschina session-id to cookie.
Login a user and store oschina session - id to cookie .
//Get cookie. //Got user session. //Time-up for session.