Ejemplo n.º 1
0
func (t *SwiftSampleApi) PostMessage(r *http.Request, req *PostReq, res *PostRes) error {

	c := endpoints.NewContext(r)

	if u, error := endpoints.CurrentUser(c, Scopes, Audiences, ClientIds); error != nil {
		return endpoints.NewUnauthorizedError("Need auth")
	} else {
		req.Email = u.Email
	}

	req.RegisteredAt = time.Now()

	g := goon.NewGoon(r)

	if _, error := g.Put(req); error != nil {
		return error
	}

	res.Email = req.Email
	res.Message = req.Message
	res.Id = req.Id
	res.RegisteredAt = req.RegisteredAt

	return nil
}
Ejemplo n.º 2
0
// getCurrentUser retrieves a user associated with the request.
// If there's no user (e.g. no auth info present in the request) returns
// an "unauthorized" error.
func getCurrentUser(c endpoints.Context) (*user.User, error) {
	u, err := endpoints.CurrentUser(c, scopes, audiences, clientIds)
	if err != nil {
		return nil, err
	}
	if u == nil {
		return nil, errors.New("Unauthorized: Please, sign in.")
	}
	c.Debugf("Current user: %#v", u)
	return u, nil
}
Ejemplo n.º 3
0
func (t *SwiftSampleApi) GetMessage(r *http.Request, _ *GetReq, res *GetRes) error {

	c := endpoints.NewContext(r)

	if u, error := endpoints.CurrentUser(c, Scopes, Audiences, ClientIds); error != nil {
		res.Message = "Hello World!"
		return nil
	} else {
		res.Message = fmt.Sprintf("Hello %s", u.Email)
		return nil
	}
}