Chromium Code Reviews| 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/hex" | |
| 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 return u.data.user | |
|
Vadim Sh.
2015/12/13 21:20:48
return nil if user.ClientID != "" (to mimic behavi
iannucci
2015/12/14 08:49:01
done
| |
| 46 } | |
| 47 | |
| 48 func (u *userImpl) CurrentOAuth(scopes ...string) (*user.User, error) { | |
| 49 // TODO(riannucci): something more clever in the Testing interface here? | |
| 50 u.data.RLock() | |
| 51 defer u.data.RUnlock() | |
| 52 return u.data.user, nil | |
|
Vadim Sh.
2015/12/13 21:20:48
return nil if user.ClientID == "" (to mimic behavi
iannucci
2015/12/14 08:49:01
done
| |
| 53 } | |
| 54 | |
| 55 func (u *userImpl) IsAdmin() bool { | |
| 56 u.data.RLock() | |
| 57 defer u.data.RUnlock() | |
| 58 return u.data.user.Admin | |
| 59 } | |
| 60 | |
| 61 func (u *userImpl) LoginURL(dest string) (string, error) { | |
| 62 // TODO(riannucci): something more clever? | |
| 63 return "https://fakeapp.example.com/_ah/login?redirect=" + url.QueryEsca pe(dest), nil | |
| 64 } | |
| 65 | |
| 66 func (u *userImpl) LoginURLFederated(dest, identity string) (string, error) { | |
| 67 return "", fmt.Errorf("OpenID is deprecated") | |
| 68 } | |
| 69 | |
| 70 func (u *userImpl) LogoutURL(dest string) (string, error) { | |
| 71 // TODO(riannucci): something more clever? | |
| 72 return "https://fakeapp.example.com/_ah/logout?redirect=" + url.QueryEsc ape(dest), nil | |
| 73 } | |
| 74 | |
| 75 func (u *userImpl) OAuthConsumerKey() (string, error) { | |
|
Vadim Sh.
2015/12/13 21:20:48
I think this thing is also deprecated (as the rest
iannucci
2015/12/14 08:49:01
made it return an error, since we don't know how t
| |
| 76 // TODO(riannucci): something more clever? | |
| 77 u.data.RLock() | |
| 78 defer u.data.RUnlock() | |
| 79 return "consumer_key:" + u.data.user.Email, nil | |
| 80 } | |
| 81 | |
| 82 func (u *userImpl) Testing() user.Testing { | |
| 83 return u | |
| 84 } | |
| 85 | |
| 86 func (u *userImpl) SetUser(user *user.User) { | |
| 87 u.data.Lock() | |
| 88 defer u.data.Unlock() | |
| 89 u.data.user = user | |
| 90 } | |
| 91 | |
| 92 func (u *userImpl) Login(email string, admin bool) { | |
| 93 parts := strings.SplitN(email, "@", 2) | |
| 94 if len(parts) != 2 { | |
| 95 panic(fmt.Errorf("%q doesn't seem to be a valid email", email)) | |
| 96 } | |
| 97 | |
| 98 // TODO(riannucci): something more clever? | |
| 99 id := sha256.Sum256([]byte("ID:" + email)) | |
| 100 clientID := sha256.Sum256([]byte("ClientID:" + email)) | |
| 101 | |
| 102 u.SetUser(&user.User{ | |
| 103 Email: email, | |
| 104 AuthDomain: parts[1], | |
| 105 Admin: admin, | |
| 106 | |
| 107 ID: hex.EncodeToString(id[:]), | |
|
Vadim Sh.
2015/12/13 21:20:48
in real life it is usually int64 (as a string)
iannucci
2015/12/14 08:49:01
done
| |
| 108 ClientID: hex.EncodeToString(clientID[:]), | |
|
Vadim Sh.
2015/12/13 21:20:48
in real life this is set only for return value of
| |
| 109 }) | |
| 110 } | |
| 111 | |
| 112 func (u *userImpl) Logout() { | |
| 113 u.SetUser(nil) | |
| 114 } | |
| OLD | NEW |