• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2012 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5package user
6
7import (
8	"golang.org/x/net/context"
9
10	"google.golang.org/appengine/internal"
11	pb "google.golang.org/appengine/internal/user"
12)
13
14// CurrentOAuth returns the user associated with the OAuth consumer making this
15// request. If the OAuth consumer did not make a valid OAuth request, or the
16// scopes is non-empty and the current user does not have at least one of the
17// scopes, this method will return an error.
18func CurrentOAuth(c context.Context, scopes ...string) (*User, error) {
19	req := &pb.GetOAuthUserRequest{}
20	if len(scopes) != 1 || scopes[0] != "" {
21		// The signature for this function used to be CurrentOAuth(Context, string).
22		// Ignore the singular "" scope to preserve existing behavior.
23		req.Scopes = scopes
24	}
25
26	res := &pb.GetOAuthUserResponse{}
27
28	err := internal.Call(c, "user", "GetOAuthUser", req, res)
29	if err != nil {
30		return nil, err
31	}
32	return &User{
33		Email:      *res.Email,
34		AuthDomain: *res.AuthDomain,
35		Admin:      res.GetIsAdmin(),
36		ID:         *res.UserId,
37		ClientID:   res.GetClientId(),
38	}, nil
39}
40
41// OAuthConsumerKey returns the OAuth consumer key provided with the current
42// request. This method will return an error if the OAuth request was invalid.
43func OAuthConsumerKey(c context.Context) (string, error) {
44	req := &pb.CheckOAuthSignatureRequest{}
45	res := &pb.CheckOAuthSignatureResponse{}
46
47	err := internal.Call(c, "user", "CheckOAuthSignature", req, res)
48	if err != nil {
49		return "", err
50	}
51	return *res.OauthConsumerKey, err
52}
53