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