Example #1
0
File: api.go Project: enj/osa
// 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 context.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.")
	}
	return u, nil
}
Example #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 context.Context) (*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.")
	}
	log.Debugf(c, "Current user: %#v", u)
	return toUser(u), nil
}
Example #3
0
func (ChannelsAPI) Add(c endpoints.Context, ch *Channel) (*Channel, error) {
	u, err := endpoints.CurrentUser(c, scopes, nil, clients)
	if err != nil {
		c.Errorf("auth: %v", err)
		return nil, endpoints.NewUnauthorizedError("authorization required")
	}

	if !strings.HasSuffix(u.Email, "gmail.com") {
		return nil, endpoints.NewUnauthorizedError("authorization refused")
	}

	if ch.Name == "" || ch.YouTubeID == "" {
		return nil, endpoints.NewBadRequestError("empty channel")
	}

	k := datastore.NewKey(c, "Channel", ch.YouTubeID, 0, nil)
	_, err = datastore.Put(c, k, ch)
	if err != nil {
		return nil, endpoints.NewInternalServerError("save channel: %v", err)
	}

	return ch, endpoints.NewAPIError("created", "created", http.StatusCreated)
}