-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
490 lines (433 loc) · 14.5 KB
/
login.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright (C) 2020 Christopher E. Miller
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package main
import (
"context"
"crypto/rsa"
"errors"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/millerlogic/auth-service/store"
"github.com/ulule/limiter/v3"
limiter_memory "github.com/ulule/limiter/v3/drivers/store/memory"
)
var Realm = "auth-service"
// TODO: don't use in-memory limit store.
var loginRateStore = limiter_memory.NewStoreWithOptions(limiter.StoreOptions{
Prefix: "",
CleanUpInterval: limiter.DefaultCleanUpInterval,
})
var loginIPRate = limiter.Rate{
Period: 60 * time.Second,
Limit: 6,
}
var loginIPLimiter = limiter.New(loginRateStore, loginIPRate)
// This protects a single user from being brute forced from various locations.
var loginUserRate = limiter.Rate{
Period: 120 * time.Second,
Limit: 12,
}
var loginUserLimiter = limiter.New(loginRateStore, loginUserRate)
func loginPOST(w http.ResponseWriter, r *http.Request) {
fromToken, fromScopes := httpTokenInfo(r)
if fromScopes.IsService() {
httpError(w, &Error{http.StatusBadRequest,
"service must use login-as"})
return
}
fromExchangeToken := fromScopes.Has("user_exchange_token") // user exchange tok
var username, password string
if fromExchangeToken {
username, _ = fromToken.Claims.(jwt.MapClaims)["sub"].(string)
if username == "" {
httpError(w, errors.New("empty username from exchange token"))
return
}
} else {
username, password, _ = r.BasicAuth()
if username == "" || password == "" {
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, Realm))
httpError(w, &Error{http.StatusUnauthorized, "invalid auth"})
return
}
}
loginCommonReq(w, r, userLoginCommonInfo{
username: username,
password: password,
fromToken: fromToken,
fromScopes: fromScopes,
fromExchangeToken: fromExchangeToken,
})
}
// login as user, for granted service.
func loginAsPOST(w http.ResponseWriter, r *http.Request) {
var payload struct {
User string `json:"user"`
Password string `json:"password"`
NoPassword bool `json:"noPassword"`
}
if !httpGetJSON(w, r, &payload) {
return
}
fromToken, fromScopes := httpTokenInfo(r)
if !fromScopes.IsService() {
httpError(w, &Error{http.StatusBadRequest,
"login-as reserved for service"})
return
}
if !fromScopes.Has("user_login_as") {
httpError(w, &ScopeRequiredError{"user_login_as"})
return
}
loginCommonReq(w, r, userLoginCommonInfo{
username: payload.User,
password: payload.Password,
fromToken: fromToken,
fromScopes: fromScopes,
fromServiceLoginAs: true,
noPassword: payload.NoPassword,
})
}
type userLoginCommonInfo struct {
username string
password string
fromToken *jwt.Token
fromScopes Scopes
fromExchangeToken bool
fromServiceLoginAs bool
noPassword bool
}
// Note: fromToken and fromScopes are different depending on fromServiceLoginAs.
func loginCommonReq(w http.ResponseWriter, r *http.Request, info userLoginCommonInfo) {
fromServiceLoginOverride := false
if info.fromServiceLoginAs {
fromServiceLoginOverride = info.fromScopes.Has("user_login_override")
}
usernameLower := strings.ToLower(info.username)
remoteIP := limiter.GetIP(r, limiter.Options{
TrustForwardHeader: info.fromScopes.Has("login_proxy"),
})
// IP limiter:
limitipkey := maskIP(remoteIP).String()
//log.Printf("ctx.Request.RemoteAddr='%v' - limitipkey='%v'\n", ctx.Request.RemoteAddr, limitipkey)
limitIPCtx, err := loginIPLimiter.Get(r.Context(), limitipkey)
if err != nil {
log.Printf("Login IP limiter error: %s", err)
httpError(w, &Error{http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError)})
return
}
w.Header().Set("X-RateLimit-Limit", strconv.FormatInt(limitIPCtx.Limit, 10))
w.Header().Set("X-RateLimit-Remaining", strconv.FormatInt(limitIPCtx.Remaining, 10))
w.Header().Set("X-RateLimit-Reset", strconv.FormatInt(limitIPCtx.Reset, 10))
// User limiter:
// This one is more stealthy, but is harder to hit.
limituserkey := usernameLower
limitUserCtx, err := loginUserLimiter.Get(r.Context(), limituserkey)
if err != nil {
log.Printf("Login User limiter error: %s", err)
httpError(w, &Error{http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError)})
return
}
// If either limits hit:
// Be sure to do this after updating all limits.
if limitIPCtx.Reached {
httpError(w, &UserError{http.StatusTooManyRequests,
"Rate limit exceeded"})
return
}
if limitUserCtx.Reached {
// Fake this one as generally unauthorized;
// also because the X-RateLimit-* headers would be "wrong" otherwise.
log.Println("Login User limiter; rate limit exceeded; RemoteAddr=", r.RemoteAddr)
httpError(w, &Error{http.StatusUnauthorized,
http.StatusText(http.StatusUnauthorized)})
return
}
tx := httpBeginDB(w, r)
if tx == nil {
return
}
defer tx.RollbackUnlessCommitted()
q := r.URL.Query()
lttDuration, err := time.ParseDuration(q.Get("duration"))
if err != nil {
lttDuration = 24 * time.Hour
}
var fromTCode float64
var requestScopes []string
var demandScopes []string
var allowScopes Scopes
var denyScopes Scopes
if info.fromExchangeToken {
claims := info.fromToken.Claims.(jwt.MapClaims)
fromTCode, _ = claims["tcode"].(float64)
}
if info.fromServiceLoginAs || info.fromExchangeToken {
claims := info.fromToken.Claims.(jwt.MapClaims)
allowScopes, _ = loadScopes(claims["allow"])
if q.Get("allow") != "" {
httpError(w, &Error{http.StatusBadRequest,
"do not specify allow querystring when using token"})
return
}
denyScopes, _ = loadScopes(claims["deny"])
denyScopes = append(denyScopes, parseCommaList(q.Get("deny"))...)
} else {
allowScopes = parseCommaList(q.Get("allow"))
denyScopes = parseCommaList(q.Get("deny"))
}
//requestScopes = parseCommaList(q.Get("request"))
if x := q["request"]; len(x) > 0 {
requestScopes = parseCommaList(x[0])
} else {
// If no request param, request the allowed scopes.
requestScopes = allowScopes
}
demandScopes = parseCommaList(q.Get("demand"))
token, _, ltt, err := UserLogin(r.Context(), tx, UserLoginInfo{
Username: info.username,
Password: info.password,
RequestScopes: requestScopes,
DemandScopes: demandScopes,
AllowScopes: allowScopes,
DenyScopes: denyScopes,
IP: remoteIP,
FromTokenCode: fromTCode,
LTTDuration: lttDuration,
FromExchangeToken: info.fromExchangeToken,
FromServiceLoginAs: info.fromServiceLoginAs,
FromServiceLoginOverride: fromServiceLoginOverride,
NoPassword: info.noPassword,
})
if err != nil {
w.Header().Add("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, Realm))
httpError(w, err)
return
}
// Sign token by private key
privateKey := r.Context().Value(privkeyCtxKey).(*rsa.PrivateKey)
signedToken, err := token.SignedString(privateKey)
if err != nil {
httpError(w, err)
return
}
token.Raw = signedToken
token.Valid = true
lttSignedToken := ""
if ltt != nil {
lttSignedToken, err = ltt.SignedString(privateKey)
if err != nil {
httpError(w, err)
return
}
ltt.Raw = lttSignedToken
ltt.Valid = true
}
err = tx.Commit()
if err != nil {
httpError(w, err)
return
}
httpWriteJSON(w, http.StatusOK, getTokenResult(token, lttSignedToken))
}
func parseCommaList(s string) []string {
if s == "" {
return nil
}
return strings.FieldsFunc(s, func(r rune) bool { return r == ',' })
}
type UserLoginInfo struct {
Username string
Password string
RequestScopes Scopes
DemandScopes Scopes
AllowScopes Scopes
DenyScopes Scopes
IP net.IP
FromTokenCode float64 // 0 if no token; needed for FromExchangeToken.
LTTDuration time.Duration // LTT output duration, 0 for no LTT at all.
FromExchangeToken bool // LTT input; ignores Password.
FromServiceLoginAs bool
FromServiceLoginOverride bool // allows service to login as user without user's password.
NoPassword bool // be explicit about password bypass (only if login override)
}
// Caller needs to commit the transaction.
// info.FromExchangeToken being set means it is a LTT, and Password is not checked!
// The second *jwt.Token returned is the new LTT, which can be nil when not applicable!
func UserLogin(ctx context.Context, tx store.DBTx, info UserLoginInfo) (*jwt.Token, Scopes, *jwt.Token, error) {
now := time.Now()
if info.FromServiceLoginOverride && !info.FromServiceLoginAs {
panic("not ok: info.FromServiceLoginOverride && !info.FromServiceLoginAs")
}
if info.FromExchangeToken && info.FromServiceLoginAs {
panic("not ok: info.FromExchangeToken && info.FromServiceLoginAs")
}
if info.NoPassword && !info.FromServiceLoginOverride {
panic("not ok: info.NoPassword && !info.FromServiceLoginOverride")
}
if info.FromServiceLoginAs {
x := "with password"
if info.FromServiceLoginOverride && info.NoPassword {
x = "service override"
}
log.Printf("* Logging into %s from %s using login-as (%s)",
info.Username, info.IP, x)
} else if info.FromExchangeToken {
log.Printf("* Logging into %s from %s using exchange token",
info.Username, info.IP)
} else {
log.Printf("* Logging into %s from %s using password",
info.Username, info.IP)
}
// Login:
user, err := tx.GetUserByName(ctx, info.Username)
errInvalidUserPw := &UserError{http.StatusUnauthorized,
"Invalid username or password"}
if err != nil {
return nil, nil, nil, errInvalidUserPw
}
if !info.FromExchangeToken && (!info.FromServiceLoginOverride || !info.NoPassword) {
if !checkPasswordAuth(info.Password, user.Auth) {
return nil, nil, nil, errInvalidUserPw
}
}
if info.FromExchangeToken {
// Confirm the tcode (TokenCode) is valid on the LTT,
// This is the way to invalidate exchange tokens.
if info.FromTokenCode != float64(user.TokenCode) {
return nil, nil, nil, &Error{http.StatusUnauthorized, "invalid token"}
}
}
if strings.IndexByte(user.Flags, 's') != -1 {
// Suspended user account.
return nil, nil, nil, &UserError{http.StatusUnauthorized,
"Unable to login"}
}
if strings.IndexByte(user.Flags, 'd') != -1 {
// Deleted user account.
return nil, nil, nil, errInvalidUserPw
}
user.LastIP = info.IP.String()
user.LastLogin = now
err = tx.UpdateUser(ctx, user, "LastIP", "LastLogin")
if err != nil {
return nil, nil, nil, err
}
log.Printf("* Logged into %s from %s successfully!", info.Username, info.IP)
expiresAt := now.Add(15 * time.Minute)
var strScopes Scopes
strScopes = append(strScopes, "user") // it's a user token
if strings.IndexByte(user.Flags, 'c') != -1 {
strScopes = append(strScopes, "user_confirmed")
} else {
// Usually we don't have two scopes to mean the opposite,
// but in this case it's more for the shaming effect!
strScopes = append(strScopes, "user_unconfirmed")
}
requestScopesMap := make(map[string]bool, len(info.RequestScopes))
for _, x := range info.RequestScopes {
if x == "user_edit" {
return nil, nil, nil, &Error{http.StatusUnauthorized,
"cannot request user_edit scope, must demand"}
}
requestScopesMap[x] = true
}
editUser := false
demandScopesMap := make(map[string]bool, len(info.DemandScopes))
for _, x := range info.DemandScopes {
if x == "user_edit" {
editUser = true
}
demandScopesMap[x] = true
}
var allowScopesMap map[string]bool // nil if no allows!
if info.AllowScopes != nil {
allowScopesMap = make(map[string]bool, len(info.AllowScopes))
for _, x := range info.AllowScopes {
allowScopesMap[x] = true
}
}
denyScopesMap := make(map[string]bool, len(info.DenyScopes))
for _, x := range info.DenyScopes {
denyScopesMap[x] = true
}
if editUser {
// user_edit scope gets special handling.
if info.FromExchangeToken || info.FromServiceLoginAs {
// An exchange token can't create an edit token.
return nil, nil, nil, &Error{http.StatusUnauthorized, "denied"}
}
if len(requestScopesMap)+len(demandScopesMap) != 1 || len(denyScopesMap) != 0 {
return nil, nil, nil, &Error{http.StatusUnauthorized,
"invalid use of user_edit scope"}
}
strScopes = append(strScopes, "user_edit")
expiresAt = now.Add(5 * time.Minute)
} else {
scopes, err := tx.GetUserScopes(ctx, user.ID)
if err != nil {
log.Printf("ERROR can't get user scopes: %v", err)
}
for _, scope := range scopes {
if requestScopesMap[scope.Name] || demandScopesMap[scope.Name] ||
strings.IndexByte(scope.Flags, 'e') == -1 {
if (allowScopesMap == nil || allowScopesMap[scope.Name]) &&
!denyScopesMap[scope.Name] {
strScopes = append(strScopes, scope.Name)
delete(demandScopesMap, scope.Name)
}
}
}
if len(demandScopesMap) != 0 {
// Note: this can happen if demand and deny have the same scope.
return nil, nil, nil, &Error{http.StatusUnauthorized,
"demand claims failed"}
}
}
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims{
"iat": now.Unix(), // when the token was issued/created (now)
"exp": expiresAt.Unix(), // time when the token will expire
"sub": info.Username, // the subject/principal is whom the token is about
"scopes": strScopes,
//"jti": uuid.Must(uuid.NewV4()).String(), // a unique identifier for the token
"id": user.ID,
"tcode": user.TokenCode,
})
var ltt *jwt.Token
wantLTT := !editUser &&
!info.FromExchangeToken &&
!info.FromServiceLoginAs &&
info.LTTDuration >= time.Second
if wantLTT {
lttExpiresAt := now.Add(info.LTTDuration)
var lttScopes Scopes = []string{"user_exchange_token"}
lttClaims := jwt.MapClaims{
"iat": now.Unix(), // when the token was issued/created (now)
"exp": lttExpiresAt.Unix(), // time when the token will expire
"sub": info.Username, // the subject/principal is whom the token is about
"scopes": lttScopes,
//"jti": uuid.Must(uuid.NewV4()).String(), // a unique identifier for the token
"id": user.ID,
"tcode": user.TokenCode,
}
if info.AllowScopes != nil {
lttClaims["allow"] = info.AllowScopes
}
if len(info.DenyScopes) != 0 {
lttClaims["deny"] = info.DenyScopes
}
ltt = jwt.NewWithClaims(jwt.SigningMethodRS512, lttClaims)
}
return token, strScopes, ltt, err
}