예제 #1
0
파일: user.go 프로젝트: cmc333333/fragspace
func (handler *userHandler) Post(r *fhttp.JsonRequest) fhttp.Response {
	user := new(models.User)
	err := r.Extract(user)
	if err != nil {
		return fhttp.UserError("invalid json")
	}
	if user.Nickname == "" {
		return fhttp.UserError("nickname cannot be empty")
	}

	context := appengine.NewContext((*http.Request)(r))
	userKey, err := datastore.Put(context, datastore.NewIncompleteKey(context, "User", nil), user)
	if err != nil {
		return fhttp.ServerError(err.String())
	}

	auth := models.NewAuth(userKey)
	_, err = datastore.Put(context, datastore.NewIncompleteKey(context, "Auth", nil), auth)
	if err != nil {
		return fhttp.ServerError(err.String())
	}
	return fhttp.JsonResponse{
		&postResponse{
			fmt.Sprintf("%x", auth.Public),
		},
	}
}
예제 #2
0
func (handler *chargeHandler) Post(r *fhttp.JsonRequest) fhttp.Response {
	return httphelpers.ReqTrustedClient((*http.Request)(r), func(userId string) fhttp.Response {
		post := new(chargeReq)
		if err := r.Extract(post); err != nil || post.CardToken == "" {
			return fhttp.UserError("invalid json")
		}
		context := appengine.NewContext((*http.Request)(r))
		chargeKey := datastore.NewKey(context, "Charge", post.CardToken, 0, nil)
		//  Check if this charge already exists
		charge := new(model.Charge)
		if err := datastore.Get(context, chargeKey, charge); err != datastore.ErrNoSuchEntity {
			return fhttp.UserError("charge already exists")
		}
		charge = model.NewCharge(datastore.NewKey(context, "User", userId, 0, nil))
		if _, err := datastore.Put(context, chargeKey, charge); err != nil {
			return fhttp.ServerError(err.String())
		}
		return fhttp.Success{}
	})
}
예제 #3
0
func (handler *oauthclientHandler) Post(r *fhttp.JsonRequest) fhttp.Response {
	post := new(oauthclientReq)
	if err := r.Extract(post); err != nil || post.Redirect == "" || post.Name == "" || post.Email == "" {
		return fhttp.UserError("invalid json")
	}
	emailRegexp := regexp.MustCompile(`^[a-z0-9._%\-+]+@[a-z0-9.\-]+\.[a-z]+$`)
	if !emailRegexp.MatchString(post.Email) {
		return fhttp.UserError("invalid email address")
	}
	if _, err := url.ParseRequest(post.Redirect); err != nil {
		return fhttp.UserError("invalid redirect uri")
	}

	context := appengine.NewContext((*http.Request)(r))
	client := oauth2.NewClient(post.Redirect, post.Name, post.Email)
	clientKey := datastore.NewKey(context, "OAuthClient", client.Id, 0, nil)
	if _, err := datastore.Put(context, clientKey, client); err != nil {
		return fhttp.ServerError(err.String())
	}
	return fhttp.JsonResponse{oauthclientRes{
		client.Id,
		client.Secret,
	}}
}