예제 #1
0
func (rp RequestProcessor) loginHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("got login request")

	userid := r.PostFormValue("userid")
	if userid == "" {
		log.Println("received blank userid")
		writeJsonError(w, apiError{err: ErrInternal, code: http.StatusBadRequest})
		return
	}
	password := r.FormValue("password")
	if password == "" {
		log.Println("received blank password")
		writeJsonError(w, apiError{err: ErrInternal, code: http.StatusBadRequest})
		return
	}

	clientId := r.FormValue("clientid")
	clientSecret := r.FormValue("client_secret")
	clientInfo := client.NewClient(clientId, clientSecret)

	if err := rp.clientdbAccessor.VerifyClient(*clientInfo); err != nil {
		writeJsonError(w, apiError{err: err, code: http.StatusBadRequest})
		return
	}

	accessToken, refreshToken, username, lang, err :=
		rp.authenticator.Login(userid, []byte(password), clientId, rp.userdbAccessor)
	if err != nil {
		writeJsonError(w, apiError{err: err, code: http.StatusBadRequest})
		return
	}

	w.WriteHeader(http.StatusAccepted)
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	var res struct {
		AccessToken  string `json:"access_token"`
		RefreshToken string `json:"refresh_token"`
		Username     string `json:"username"`
		Lang         string `json:"lang"`
	}
	res.AccessToken = *accessToken
	res.RefreshToken = *refreshToken
	res.Username = *username
	res.Lang = *lang

	if err := json.NewEncoder(w).Encode(res); err != nil {
		panic(err)
	}
}
예제 #2
0
func (rp RequestProcessor) createUserHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("got createUser request")

	userid := r.PostFormValue("userid")
	if userid == "" {
		log.Println("received blank userid")
		writeJsonError(w, apiError{err: ErrInternal, code: http.StatusBadRequest})
		return
	}
	password := r.FormValue("password")
	if password == "" {
		log.Println("received blank password")
		writeJsonError(w, apiError{err: ErrInternal, code: http.StatusBadRequest})
		return
	}

	clientId := r.FormValue("clientid")
	clientSecret := r.FormValue("client_secret")
	clientInfo := client.NewClient(clientId, clientSecret)

	if err := rp.clientdbAccessor.VerifyClient(*clientInfo); err != nil {
		writeJsonError(w, apiError{err: err, code: http.StatusBadRequest})
		return
	}

	username := r.FormValue("username")

	if username == "" {
		log.Println("received blank username")
		writeJsonError(w, apiError{err: fmt.Errorf("Username can not be empty"), code: http.StatusBadRequest})
		return
	}
	userlang := r.FormValue("lang")
	if userlang == "" {
		log.Println("received blank lang str")
		writeJsonError(w, apiError{err: ErrInternal, code: http.StatusBadRequest})
		return
	}

	//user cred
	otherFields := make(map[auth.UserFieldName]interface{})
	otherFields[auth.UserField_Language] = userlang
	accessToken, refreshToken, err := rp.authenticator.CreateUser(
		userid, username, []byte(password), clientId, otherFields, rp.userdbAccessor)

	if err != nil {
		switch err {
		case auth.ErrUserExists, auth.ErrUserIdInvalid, auth.ErrUserIdForbidden:
			writeJsonError(w, apiError{err: err, code: http.StatusBadRequest})
		default:
			log.Printf("failed to create user:%v", err)
			writeJsonError(w, apiError{err: ErrInternal, code: http.StatusInternalServerError})
		}

		return
	}

	w.WriteHeader(http.StatusCreated)
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	var res struct {
		AccessToken  string `json:"access_token"`
		RefreshToken string `json:"refresh_token"`
	}
	res.AccessToken = *accessToken
	res.RefreshToken = *refreshToken

	if err := json.NewEncoder(w).Encode(res); err != nil {
		panic(err)
	}
}