OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package memory |
| 6 |
| 7 import ( |
| 8 "crypto/sha256" |
| 9 "encoding/binary" |
| 10 "fmt" |
| 11 "net/url" |
| 12 "strings" |
| 13 "sync" |
| 14 |
| 15 "golang.org/x/net/context" |
| 16 |
| 17 "github.com/luci/gae/service/user" |
| 18 ) |
| 19 |
| 20 type userData struct { |
| 21 sync.RWMutex |
| 22 user *user.User |
| 23 } |
| 24 |
| 25 // userImpl is a contextual pointer to the current userData. |
| 26 type userImpl struct { |
| 27 data *userData |
| 28 } |
| 29 |
| 30 var _ user.Interface = (*userImpl)(nil) |
| 31 |
| 32 // useUser adds a user.Interface implementation to context, accessible |
| 33 // by user.Get(c) |
| 34 func useUser(c context.Context) context.Context { |
| 35 data := &userData{} |
| 36 |
| 37 return user.SetFactory(c, func(ic context.Context) user.Interface { |
| 38 return &userImpl{data} |
| 39 }) |
| 40 } |
| 41 |
| 42 func (u *userImpl) Current() *user.User { |
| 43 u.data.RLock() |
| 44 defer u.data.RUnlock() |
| 45 if u.data.user != nil && u.data.user.ClientID == "" { |
| 46 ret := *u.data.user |
| 47 return &ret |
| 48 } |
| 49 return nil |
| 50 } |
| 51 |
| 52 func (u *userImpl) CurrentOAuth(scopes ...string) (*user.User, error) { |
| 53 // TODO(riannucci): something more clever in the Testable interface here
? |
| 54 u.data.RLock() |
| 55 defer u.data.RUnlock() |
| 56 if u.data.user != nil && u.data.user.ClientID != "" { |
| 57 ret := *u.data.user |
| 58 return &ret, nil |
| 59 } |
| 60 return nil, nil |
| 61 } |
| 62 |
| 63 func (u *userImpl) IsAdmin() bool { |
| 64 u.data.RLock() |
| 65 defer u.data.RUnlock() |
| 66 return u.data.user != nil && u.data.user.Admin |
| 67 } |
| 68 |
| 69 func (u *userImpl) LoginURL(dest string) (string, error) { |
| 70 return "https://fakeapp.example.com/_ah/login?redirect=" + url.QueryEsca
pe(dest), nil |
| 71 } |
| 72 |
| 73 func (u *userImpl) LogoutURL(dest string) (string, error) { |
| 74 return "https://fakeapp.example.com/_ah/logout?redirect=" + url.QueryEsc
ape(dest), nil |
| 75 } |
| 76 |
| 77 func (u *userImpl) LoginURLFederated(dest, identity string) (string, error) { |
| 78 return "", fmt.Errorf("LoginURLFederated is deprecated") |
| 79 } |
| 80 |
| 81 func (u *userImpl) OAuthConsumerKey() (string, error) { |
| 82 return "", fmt.Errorf("OAuthConsumerKey is deprecated") |
| 83 } |
| 84 |
| 85 func (u *userImpl) Testable() user.Testable { |
| 86 return u |
| 87 } |
| 88 |
| 89 func (u *userImpl) SetUser(user *user.User) { |
| 90 u.data.Lock() |
| 91 defer u.data.Unlock() |
| 92 u.data.user = user |
| 93 } |
| 94 |
| 95 func (u *userImpl) Login(email, clientID string, admin bool) { |
| 96 parts := strings.Split(email, "@") |
| 97 if len(parts) != 2 { |
| 98 panic(fmt.Errorf("%q doesn't seem to be a valid email", email)) |
| 99 } |
| 100 |
| 101 id := sha256.Sum256([]byte("ID:" + email)) |
| 102 |
| 103 u.SetUser(&user.User{ |
| 104 Email: email, |
| 105 AuthDomain: parts[1], |
| 106 Admin: admin, |
| 107 |
| 108 ID: fmt.Sprint(binary.LittleEndian.Uint64(id[:])), |
| 109 ClientID: clientID, |
| 110 }) |
| 111 } |
| 112 |
| 113 func (u *userImpl) Logout() { |
| 114 u.SetUser(nil) |
| 115 } |
OLD | NEW |