-
Notifications
You must be signed in to change notification settings - Fork 28
/
auth.go
370 lines (317 loc) · 9.04 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package padlockcloud
import "time"
import "encoding/json"
import "encoding/base64"
import "net/http"
import "regexp"
import "fmt"
import "errors"
var authStringPattern = regexp.MustCompile("^(AuthToken|ApiKey|SkeletonKey) (.+):(.+)$")
var authMaxAge = func(authType string) time.Duration {
switch authType {
case "web":
return time.Hour
default:
return time.Duration(0)
}
}
// A wrapper for an api key containing some meta info like the user and device name
type AuthToken struct {
Email string
Token string
Type string
Id string
Created time.Time
LastUsed time.Time
Expires time.Time
ClientVersion string
ClientPlatform string
Device *Device
account *Account
}
// Returns the account associated with this auth token
func (t *AuthToken) Account() *Account {
return t.account
}
// Validates the auth token against account `a`, i.e. looks for the corresponding
// token in the accounts `AuthTokens` slice. If found, the token is considered valid
// and it's value is updated with the value of the corresponding auth token in `a.AuthTokens`
// and the `account` field is set to `a`
func (t *AuthToken) Validate(a *Account) bool {
if _, at := a.findAuthToken(t); at != nil && at.Token == t.Token {
*t = *at
t.account = a
return true
}
return false
}
// Returns a string representation of the auth token in the form "AuthToken base64(t.Email):t.Token"
func (t *AuthToken) String() string {
return fmt.Sprintf(
"AuthToken %s:%s",
base64.RawURLEncoding.EncodeToString([]byte(t.Email)),
t.Token,
)
}
// Returns true if `t` is expires, false otherwise
func (t *AuthToken) Expired() bool {
return !t.Expires.IsZero() && t.Expires.Before(time.Now())
}
func (t *AuthToken) Description() string {
if t.Device != nil {
return t.Device.Description()
} else if t.ClientPlatform != "" {
return PlatformDisplayName(t.ClientPlatform) + " Device"
} else {
// Older versions of the Padlock Client that didn't send device data
// were mostly only available on iOS and Android, so "Mobile Device"
// is the best guess we have in this case
return "Mobile Device"
}
}
func (t *AuthToken) ToMap() map[string]interface{} {
return map[string]interface{}{
"description": t.Description(),
"tokenId": t.Id,
}
}
// Creates an auth token from it's string representation of the form "AuthToken base64(t.Email):t.Token"
func AuthTokenFromString(str string) (*AuthToken, error) {
// Check if the Authorization header exists and is well formed
if !authStringPattern.MatchString(str) {
return nil, errors.New("invalid credentials")
}
// Extract email and auth token from Authorization header
matches := authStringPattern.FindStringSubmatch(str)
var authType string
if matches[1] == "SkeletonKey" {
authType = "skeleton"
} else {
authType = "api"
}
email := matches[2]
// Try to decode email in case it's in base64
if dec, err := base64.RawURLEncoding.DecodeString(email); err == nil {
email = string(dec)
}
t := &AuthToken{
Email: email,
Token: matches[3],
Type: authType,
}
return t, nil
}
// Creates an auth token from a given request by parsing the `Authorization` header
// and `auth` cookie
func AuthTokenFromRequest(r *http.Request) (*AuthToken, error) {
authString := r.Header.Get("Authorization")
if authString == "" {
if cookie, err := r.Cookie("auth"); err == nil {
authString = cookie.Value
}
}
return AuthTokenFromString(authString)
}
// Creates a new auth token for a given `email`
func NewAuthToken(email string, t string, device *Device) (*AuthToken, error) {
authT, err := token()
if err != nil {
return nil, err
}
id, err := randomBase64(6)
if err != nil {
return nil, err
}
if t == "" {
t = "api"
}
var expires time.Time
if maxAge := authMaxAge(t); maxAge != 0 {
expires = time.Now().Add(maxAge)
}
return &AuthToken{
Email: email,
Token: authT,
Type: t,
Id: id,
Created: time.Now(),
LastUsed: time.Now(),
Expires: expires,
Device: device,
}, nil
}
// A struct representing a user with a set of api keys
type Account struct {
// The email servers as a unique identifier and as a means for
// requesting/activating api keys
Email string
// Time the account was created
Created time.Time
// A set of api keys that can be used to access the data associated with this
// account
AuthTokens []*AuthToken
}
// Implements the `Key` method of the `Storable` interface
func (acc *Account) Key() []byte {
return []byte(acc.Email)
}
// Implementation of the `Storable.Deserialize` method
func (acc *Account) Deserialize(data []byte) error {
return json.Unmarshal(data, acc)
}
// Implementation of the `Storable.Serialize` method
func (acc *Account) Serialize() ([]byte, error) {
if acc.Created.IsZero() {
acc.Created = time.Now()
}
return json.Marshal(acc)
}
// Adds an api key to this account. If an api key for the given device
// is already registered, that one will be replaced
func (a *Account) AddAuthToken(token *AuthToken) {
a.AuthTokens = append(a.AuthTokens, token)
}
// Returns the matching AuthToken instance by comparing Token field, Id or both
// If either Id or Token field is empty, only the other one will compared. If
// both are empty, nil is returned
func (a *Account) findAuthToken(at *AuthToken) (int, *AuthToken) {
if at.Token == "" && at.Id == "" && (at.Device == nil || at.Device.UUID == "") {
return -1, nil
}
for i, t := range a.AuthTokens {
if t != nil &&
(at.Type == "" || t.Type == at.Type) &&
(at.Token == "" || t.Token == at.Token) &&
(at.Id == "" || t.Id == at.Id) &&
(at.Device == nil || at.Device.UUID == "" || t.Device != nil && t.Device.UUID == at.Device.UUID) {
return i, t
fmt.Println(at, i, t)
}
}
return -1, nil
}
// Updates the correspoding auth token in the accounts `AuthTokens` slice with the
// value of `t`
func (a *Account) UpdateAuthToken(t *AuthToken) {
if _, at := a.findAuthToken(t); at != nil {
*at = *t
}
}
// Removes the corresponding auth token from the accounts `AuthTokens` slice
func (a *Account) RemoveAuthToken(t *AuthToken) bool {
if i, _ := a.findAuthToken(t); i != -1 {
s := a.AuthTokens
s[i] = s[len(s)-1]
s[len(s)-1] = nil
a.AuthTokens = s[:len(s)-1]
return true
}
return false
}
// Filters out auth tokens that have been expired for 7 days or more
func (a *Account) RemoveExpiredAuthTokens() {
s := a.AuthTokens[:0]
for _, t := range a.AuthTokens {
var maxAge time.Duration = 0
// Keep expired api tokens around for a while longer
if t.Type == "api" {
maxAge = 7 * 24 * time.Hour
}
if t.Expires.IsZero() || t.Expires.After(time.Now().Add(-maxAge)) {
s = append(s, t)
}
}
a.AuthTokens = s
}
// Expires auth tokens that haven't been used in a while
func (a *Account) ExpireUnusedAuthTokens() {
maxIdle := 30 * 24 * time.Hour
s := a.AuthTokens[:0]
for _, t := range a.AuthTokens {
if t.LastUsed.After(time.Now().Add(-maxIdle)) {
s = append(s, t)
}
}
a.AuthTokens = s
}
func (a *Account) AuthTokensByType(typ string) []*AuthToken {
var tokens []*AuthToken
for _, t := range a.AuthTokens {
if t != nil && t.Type == typ {
tokens = append(tokens, t)
}
}
return tokens
}
func (a *Account) Devices() []*AuthToken {
devices := make([]*AuthToken, 0)
for _, at := range a.AuthTokensByType("api") {
if !at.Expired() {
devices = append(devices, at)
}
}
return devices
}
func (a *Account) ToMap() map[string]interface{} {
obj := map[string]interface{}{
"email": a.Email,
}
devices := make([]map[string]interface{}, 0)
for _, at := range a.Devices() {
devices = append(devices, at.ToMap())
}
obj["devices"] = devices
return obj
}
// AuthRequest represents an api key - activation token pair used to activate a given api key
// `AuthRequest.Token` is used to activate the AuthToken through a separate channel (e.g. email)
type AuthRequest struct {
Code string
Token string
AuthToken *AuthToken
Created time.Time
Redirect string
}
// Implementation of the `Storable.Key` interface method
func (ar *AuthRequest) Key() []byte {
if ar.Token != "" {
return []byte(ar.Token)
} else {
return []byte(fmt.Sprintf("%s-%s", ar.AuthToken.Email, ar.Code))
}
}
// Implementation of the `Storable.Deserialize` method
func (ar *AuthRequest) Deserialize(data []byte) error {
return json.Unmarshal(data, ar)
}
// Implementation of the `Storable.Serialize` method
func (ar *AuthRequest) Serialize() ([]byte, error) {
return json.Marshal(ar)
}
// Creates a new `AuthRequest` with a given `email`
func NewAuthRequest(email string, tType string, actType string, device *Device) (*AuthRequest, error) {
var authToken *AuthToken
var err error
// Create new auth token
if authToken, err = NewAuthToken(email, tType, device); err != nil {
return nil, err
}
ar := &AuthRequest{
AuthToken: authToken,
Created: time.Now(),
Redirect: "",
}
if actType == "code" {
ar.Code, err = randomHex(3)
} else {
ar.Token, err = token()
}
if err != nil {
return nil, err
}
return ar, nil
}
func init() {
RegisterStorable(&Account{}, "auth-accounts")
RegisterStorable(&AuthRequest{}, "auth-requests")
}