Esempio n. 1
0
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),
		},
	}
}
Esempio n. 2
0
func ReqToken(req *http.Request, success func(string) fhttp.Response) fhttp.Response {
	token := oauth2.DecodeToken(req)
	if token == nil {
		return fhttp.UserError("invalid_token")
	}
	return success(token.User)
}
Esempio n. 3
0
func ReqUser(req *http.Request, success func(*model.User) fhttp.Response) fhttp.Response {
	return ReqToken(req, func(keyStr string) fhttp.Response {
		user, err := model.UserFromKey(keyStr, appengine.NewContext(req))
		if err != nil {
			return fhttp.UserError("invalid_token")
		}
		return success(user)
	})
}
Esempio n. 4
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{}
	})
}
Esempio n. 5
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,
	}}
}
Esempio n. 6
0
func ReqTrustedClient(req *http.Request, success func(string) fhttp.Response) fhttp.Response {
	config, err := conf.ReadConfigFile("config.ini")
	if err != nil {
		panic(err)
	}
	clientId, err := config.GetString("webclient", "clientId")
	if err != nil {
		panic(err)
	}
	token := oauth2.DecodeToken(req)
	if token == nil || token.Client != clientId {
		return fhttp.UserError("invalid_token")
	}
	return success(token.User)
}