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