user.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2011 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 provides a client for App Engine's user authentication service.
  5. package user // import "google.golang.org/appengine/user"
  6. import (
  7. "strings"
  8. "github.com/golang/protobuf/proto"
  9. "golang.org/x/net/context"
  10. "google.golang.org/appengine/internal"
  11. pb "google.golang.org/appengine/internal/user"
  12. )
  13. // User represents a user of the application.
  14. type User struct {
  15. Email string
  16. AuthDomain string
  17. Admin bool
  18. // ID is the unique permanent ID of the user.
  19. // It is populated if the Email is associated
  20. // with a Google account, or empty otherwise.
  21. ID string
  22. // ClientID is the ID of the pre-registered client so its identity can be verified.
  23. // See https://developers.google.com/console/help/#generatingoauth2 for more information.
  24. ClientID string
  25. FederatedIdentity string
  26. FederatedProvider string
  27. }
  28. // String returns a displayable name for the user.
  29. func (u *User) String() string {
  30. if u.AuthDomain != "" && strings.HasSuffix(u.Email, "@"+u.AuthDomain) {
  31. return u.Email[:len(u.Email)-len("@"+u.AuthDomain)]
  32. }
  33. if u.FederatedIdentity != "" {
  34. return u.FederatedIdentity
  35. }
  36. return u.Email
  37. }
  38. // LoginURL returns a URL that, when visited, prompts the user to sign in,
  39. // then redirects the user to the URL specified by dest.
  40. func LoginURL(c context.Context, dest string) (string, error) {
  41. return LoginURLFederated(c, dest, "")
  42. }
  43. // LoginURLFederated is like LoginURL but accepts a user's OpenID identifier.
  44. func LoginURLFederated(c context.Context, dest, identity string) (string, error) {
  45. req := &pb.CreateLoginURLRequest{
  46. DestinationUrl: proto.String(dest),
  47. }
  48. if identity != "" {
  49. req.FederatedIdentity = proto.String(identity)
  50. }
  51. res := &pb.CreateLoginURLResponse{}
  52. if err := internal.Call(c, "user", "CreateLoginURL", req, res); err != nil {
  53. return "", err
  54. }
  55. return *res.LoginUrl, nil
  56. }
  57. // LogoutURL returns a URL that, when visited, signs the user out,
  58. // then redirects the user to the URL specified by dest.
  59. func LogoutURL(c context.Context, dest string) (string, error) {
  60. req := &pb.CreateLogoutURLRequest{
  61. DestinationUrl: proto.String(dest),
  62. }
  63. res := &pb.CreateLogoutURLResponse{}
  64. if err := internal.Call(c, "user", "CreateLogoutURL", req, res); err != nil {
  65. return "", err
  66. }
  67. return *res.LogoutUrl, nil
  68. }
  69. func init() {
  70. internal.RegisterErrorCodeMap("user", pb.UserServiceError_ErrorCode_name)
  71. }