oauth.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. package user
  5. import (
  6. "golang.org/x/net/context"
  7. "google.golang.org/appengine/internal"
  8. pb "google.golang.org/appengine/internal/user"
  9. )
  10. // CurrentOAuth returns the user associated with the OAuth consumer making this
  11. // request. If the OAuth consumer did not make a valid OAuth request, or the
  12. // scopes is non-empty and the current user does not have at least one of the
  13. // scopes, this method will return an error.
  14. func CurrentOAuth(c context.Context, scopes ...string) (*User, error) {
  15. req := &pb.GetOAuthUserRequest{}
  16. if len(scopes) != 1 || scopes[0] != "" {
  17. // The signature for this function used to be CurrentOAuth(Context, string).
  18. // Ignore the singular "" scope to preserve existing behavior.
  19. req.Scopes = scopes
  20. }
  21. res := &pb.GetOAuthUserResponse{}
  22. err := internal.Call(c, "user", "GetOAuthUser", req, res)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &User{
  27. Email: *res.Email,
  28. AuthDomain: *res.AuthDomain,
  29. Admin: res.GetIsAdmin(),
  30. ID: *res.UserId,
  31. ClientID: res.GetClientId(),
  32. }, nil
  33. }
  34. // OAuthConsumerKey returns the OAuth consumer key provided with the current
  35. // request. This method will return an error if the OAuth request was invalid.
  36. func OAuthConsumerKey(c context.Context) (string, error) {
  37. req := &pb.CheckOAuthSignatureRequest{}
  38. res := &pb.CheckOAuthSignatureResponse{}
  39. err := internal.Call(c, "user", "CheckOAuthSignature", req, res)
  40. if err != nil {
  41. return "", err
  42. }
  43. return *res.OauthConsumerKey, err
  44. }