-
Notifications
You must be signed in to change notification settings - Fork 1
/
enforcer.go
669 lines (582 loc) · 16 KB
/
enforcer.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package token_go
import (
"errors"
"fmt"
"github.com/weloe/token-go/config"
"github.com/weloe/token-go/ctx"
httpCtx "github.com/weloe/token-go/ctx/go-http-context"
"github.com/weloe/token-go/log"
"github.com/weloe/token-go/model"
"github.com/weloe/token-go/persist"
"github.com/weloe/token-go/util"
log2 "log"
"net/http"
)
type Enforcer struct {
conf string
loginType string
config config.TokenConfig
generateFunc model.GenerateTokenFunc
adapter persist.Adapter
watcher persist.Watcher
logger log.Logger
dispatcher persist.Dispatcher
notifyDispatcher bool
updatableWatcher persist.UpdatableWatcher
notifyUpdatableWatcher bool
authManager interface{}
}
func (e *Enforcer) EnableUpdatableWatcher(b bool) {
if e.updatableWatcher == nil {
return
}
e.notifyUpdatableWatcher = b
}
func NewDefaultAdapter() persist.Adapter {
return persist.NewDefaultAdapter()
}
func NewHttpContext(req *http.Request, writer http.ResponseWriter) ctx.Context {
return httpCtx.NewHttpContext(req, writer)
}
func NewEnforcer(adapter persist.Adapter, args ...interface{}) (*Enforcer, error) {
var err error
var enforcer *Enforcer
if len(args) > 2 {
return nil, fmt.Errorf("NewEnforcer() failed: unexpected args length = %v, it should be less than or equal to 2", len(args))
}
if util.HasNil(args) {
return nil, errors.New("NewEnforcer() failed: parameters cannot be nil")
}
if len(args) == 0 {
enforcer, err = InitWithDefaultConfig(adapter)
} else if len(args) == 1 {
switch args[0].(type) {
case *config.TokenConfig:
enforcer, err = InitWithConfig(args[0].(*config.TokenConfig), adapter)
case string:
enforcer, err = InitWithFile(args[0].(string), adapter)
default:
return nil, errors.New("NewEnforcer() failed: the second parameter should be *TokenConfig or string")
}
}
return enforcer, err
}
func InitWithDefaultConfig(adapter persist.Adapter) (*Enforcer, error) {
if adapter == nil {
return nil, errors.New("InitWithDefaultConfig() failed: parameters cannot be nil")
}
return InitWithConfig(config.DefaultTokenConfig(), adapter)
}
func InitWithFile(conf string, adapter persist.Adapter) (*Enforcer, error) {
if conf == "" || adapter == nil {
return nil, errors.New("InitWithFile() failed: parameters cannot be nil")
}
newConfig, err := config.ReadConfig(conf)
if err != nil {
return nil, err
}
enforcer, err := InitWithConfig(newConfig.TokenConfig, adapter)
enforcer.conf = conf
return enforcer, err
}
func InitWithConfig(tokenConfig *config.TokenConfig, adapter persist.Adapter) (*Enforcer, error) {
fm := model.LoadFunctionMap()
if tokenConfig == nil || adapter == nil {
return nil, errors.New("InitWithConfig() failed: parameters cannot be nil")
}
tokenConfig.InitConfig()
e := &Enforcer{
loginType: "user",
config: *tokenConfig,
generateFunc: fm,
adapter: adapter,
logger: &log.DefaultLogger{},
}
e.startCleanTimer()
return e, nil
}
// if e.adapter.(type) == *persist.DefaultAdapter, can start cleanTimer
func (e *Enforcer) startCleanTimer() {
defaultAdapter, ok := e.adapter.(*persist.DefaultAdapter)
if ok {
if !defaultAdapter.GetCleanTimer() {
return
}
dataRefreshPeriod := e.config.DataRefreshPeriod
if period := dataRefreshPeriod; period > 0 {
err := defaultAdapter.StartCleanTimer(period)
if err != nil {
log2.Printf("enble adapter cleanTimer failed: %v", err)
return
}
e.logger.StartCleanTimer(period)
}
}
}
func (e *Enforcer) SetType(t string) {
e.loginType = t
}
func (e *Enforcer) GetType() string {
return e.loginType
}
func (e *Enforcer) GetAdapter() persist.Adapter {
return e.adapter
}
func (e *Enforcer) SetAdapter(adapter persist.Adapter) {
e.adapter = adapter
}
func (e *Enforcer) GetWatcher() persist.Watcher {
return e.watcher
}
func (e *Enforcer) SetWatcher(watcher persist.Watcher) {
e.watcher = watcher
}
func (e *Enforcer) SetUpdatableWatcher(watcher persist.UpdatableWatcher) {
if watcher != nil {
e.updatableWatcher = watcher
e.notifyUpdatableWatcher = true
}
}
func (e *Enforcer) GetUpdatableWatcher() persist.UpdatableWatcher {
return e.updatableWatcher
}
func (e *Enforcer) GetLogger() log.Logger {
return e.logger
}
func (e *Enforcer) SetLogger(logger log.Logger) {
e.logger = logger
}
func (e *Enforcer) EnableLog() {
e.logger.Enable(true)
}
func (e *Enforcer) IsLogEnable() bool {
return e.logger.IsEnabled()
}
// Login login by id and default loginModel, return tokenValue and error
func (e *Enforcer) Login(id string, ctx ...ctx.Context) (string, error) {
return e.LoginByModel(id, model.CreateLoginModelByDevice(""), ctx...)
}
func (e *Enforcer) LoginById(id string, device ...string) (string, error) {
if len(device) > 0 && device[0] != "" {
return e.LoginByModel(id, model.CreateLoginModelByDevice(device[0]), nil)
}
return e.Login(id, nil)
}
// LoginByModel login by id and loginModel, return tokenValue and error
func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, c ...ctx.Context) (string, error) {
if loginModel == nil {
return "", errors.New("arg loginModel can not be nil")
}
if len(c) == 0 {
c = []ctx.Context{nil}
}
var err error
var session *model.Session
var tokenValue string
tokenConfig := e.config
// allocate token
tokenValue, err = e.createLoginToken(id, loginModel)
device := loginModel.Device
if err != nil {
return "", err
}
// add tokenSign
if session = e.GetSession(id); session == nil {
session = model.NewSession("0", "account-session", id)
}
session.AddDistinctValueTokenSign(&model.TokenSign{
Value: tokenValue,
Device: device,
})
if e.config.DoubleToken {
refreshToken, err := e.createRefreshToken(id, tokenValue, loginModel)
if err != nil {
return "", err
}
err = e.responseRefreshToken(refreshToken, loginModel, c[0])
if err != nil {
return "", err
}
}
timeout := loginModel.Timeout
// reset session
err = e.SetSession(id, session, timeout)
if err != nil {
return "", err
}
// set token-id
err = e.SetIdByToken(id, tokenValue, timeout)
if err != nil {
return "", err
}
// response token
err = e.ResponseToken(tokenValue, loginModel, c[0])
if err != nil {
return "", err
}
// called watcher
m := &model.Login{
Device: device,
IsLastingCookie: loginModel.IsLastingCookie,
Timeout: timeout,
JwtData: loginModel.JwtData,
Token: tokenValue,
}
// called logger
e.logger.Login(e.loginType, id, tokenValue, m)
if e.watcher != nil {
e.watcher.Login(e.loginType, id, tokenValue, m)
}
if device != "" && tokenConfig.DeviceMaxLoginCount != -1 {
if session = e.GetSession(id); session != nil {
// get by login device
tokenSignList := session.GetFilterTokenSignSlice(device)
if len(tokenSignList) > int(tokenConfig.DeviceMaxLoginCount) {
err = e.deleteRedundantTokenSign(session, tokenConfig.DeviceMaxLoginCount)
if err != nil {
return "", err
}
}
}
}
// check if the number of sessions for this account exceeds the maximum limit.
if tokenConfig.MaxLoginCount != -1 {
if session = e.GetSession(id); session != nil {
if session.TokenSignSize() <= int(tokenConfig.MaxLoginCount) {
return tokenValue, nil
}
err = e.deleteRedundantTokenSign(session, tokenConfig.MaxLoginCount)
if err != nil {
return "", err
}
}
}
return tokenValue, nil
}
// Logout user logout
func (e *Enforcer) Logout(ctx ctx.Context) error {
tokenConfig := e.config
token := e.GetRequestToken(ctx)
if token == "" {
return errors.New("logout() failed: token doesn't exist")
}
if e.config.IsReadCookie {
ctx.Response().DeleteCookie(tokenConfig.TokenName,
tokenConfig.CookieConfig.Path,
tokenConfig.CookieConfig.Domain)
}
err := e.LogoutByToken(token)
if err != nil {
return err
}
return nil
}
// LogoutById force user to logout
func (e *Enforcer) LogoutById(id string, device ...string) error {
session := e.GetSession(id)
if session != nil {
for _, tokenSign := range session.TokenSignList {
if len(device) > 0 && device[0] != "" && tokenSign.Device == device[0] {
err := e.LogoutByToken(tokenSign.Value)
if err != nil {
return err
}
} else {
err := e.LogoutByToken(tokenSign.Value)
if err != nil {
return err
}
}
}
}
return nil
}
// LogoutByToken clear token info
func (e *Enforcer) LogoutByToken(token string) error {
var err error
// delete token-id
id := e.getIdByToken(token)
if id == "" {
return errors.New("user not logged in")
}
// delete token-id
err = e.deleteIdByToken(token)
if err != nil {
return err
}
session := e.GetSession(id)
if session != nil {
// delete tokenSign
session.RemoveTokenSign(token)
err = e.UpdateSession(id, session)
if err != nil {
return err
}
}
// check TokenSignList length, if length == 0, delete this session
if session != nil && session.TokenSignSize() == 0 {
err = e.DeleteSession(id)
if err != nil {
return err
}
}
err = e.deleteRefreshToken(token)
if err != nil {
return err
}
e.logger.Logout(e.loginType, id, token)
if e.watcher != nil {
e.watcher.Logout(e.loginType, id, token)
}
return nil
}
// IsLoginById check if user logged in by loginId.
// check all tokenValue and if one is validated return true
func (e *Enforcer) IsLoginById(id string, device ...string) (bool, error) {
var err error
session := e.GetSession(id)
if session != nil {
var l []*model.TokenSign
if len(device) > 0 && device[0] != "" {
l = session.GetFilterTokenSignSlice(device[0])
} else {
l = session.TokenSignList
}
for _, tokenSign := range l {
err = e.CheckLoginByToken(tokenSign.Value)
if err != nil {
continue
}
return true, nil
}
}
return false, err
}
// GetId get the id from the Adapter, do not check the value
// if GetId()= -4, it means that user be replaced
// if GetId()= -5, it means that user be kicked
// if GetId()= -6, it means that user be banned
func (e *Enforcer) GetId(ctx ctx.Context) string {
token := e.GetRequestToken(ctx)
return e.GetIdByToken(token)
}
// GetIdByToken get the id from the Adapter
func (e *Enforcer) GetIdByToken(token string) string {
if token == "" {
return ""
}
loginId := e.getIdByToken(token)
return loginId
}
// IsLogin check if user logged in by token.
func (e *Enforcer) IsLogin(ctx ctx.Context) (bool, error) {
tokenValue := e.GetRequestToken(ctx)
return e.IsLoginByToken(tokenValue)
}
func (e *Enforcer) IsLoginByToken(tokenValue string) (bool, error) {
if tokenValue == "" {
return false, nil
}
err := e.CheckLoginByToken(tokenValue)
if err != nil {
return false, err
}
return true, nil
}
func (e *Enforcer) CheckLogin(ctx ctx.Context) error {
_, err := e.GetLoginId(ctx)
if err != nil {
return err
}
return nil
}
func (e *Enforcer) CheckLoginByToken(token string) error {
_, err := e.GetLoginIdByToken(token)
if err != nil {
return err
}
return nil
}
// GetLoginId get id and check it
func (e *Enforcer) GetLoginId(ctx ctx.Context) (string, error) {
tokenValue := e.GetRequestToken(ctx)
return e.GetLoginIdByToken(tokenValue)
}
func (e *Enforcer) GetLoginIdByToken(token string) (string, error) {
str := e.GetIdByToken(token)
if str == "" {
return "", errors.New("GetLoginId() failed: not logged in")
}
validate, err := e.checkId(str)
if !validate {
return "", err
}
// auto refresh timeout, When the user accesses
if e.config.AutoRenew {
_ = e.updateTokenTimeout(token, e.config.Timeout)
_ = e.UpdateSessionTimeout(str, e.config.Timeout)
}
return str, nil
}
func (e *Enforcer) GetLoginCount(id string, device ...string) int {
if session := e.GetSession(id); session != nil {
if len(device) > 0 && device[0] != "" {
return session.GetFilterTokenSign(device[0]).Len()
}
return session.TokenSignSize()
}
return 0
}
// GetBannedTime get banned time
func (e *Enforcer) GetBannedTime(id string, service string) int64 {
timeout := e.getBannedTime(id, service)
return timeout
}
// GetRequestToken read token from requestHeader | cookie | requestBody
func (e *Enforcer) GetRequestToken(ctx ctx.Context) string {
var tokenValue string
if ctx == nil {
return ""
}
if e.config.IsReadHeader {
if tokenValue = ctx.Request().Header(e.config.TokenName); tokenValue != "" {
return tokenValue
}
}
if e.config.IsReadCookie {
if tokenValue = ctx.Request().Cookie(e.config.TokenName); tokenValue != "" {
return tokenValue
}
}
if e.config.IsReadBody {
if tokenValue = ctx.Request().PostForm(e.config.TokenName); tokenValue != "" {
return tokenValue
}
}
return tokenValue
}
// AddTokenGenerateFun add token generate strategy
func (e *Enforcer) AddTokenGenerateFun(tokenStyle string, f model.HandlerFunc) error {
e.generateFunc.AddFunc(tokenStyle, f)
return nil
}
func (e *Enforcer) GetSession(id string) *model.Session {
if v := e.adapter.Get(e.spliceSessionKey(id), util.GetType(&model.Session{})); v != nil {
return v.(*model.Session)
}
return nil
}
func (e *Enforcer) SetSession(id string, session *model.Session, timeout int64) error {
err := e.notifySet(e.spliceSessionKey(id), session, timeout)
if err != nil {
return err
}
return nil
}
func (e *Enforcer) DeleteSession(id string) error {
err := e.notifyDelete(e.spliceSessionKey(id))
if err != nil {
return err
}
return nil
}
func (e *Enforcer) UpdateSession(id string, session *model.Session) error {
err := e.notifyUpdate(e.spliceSessionKey(id), session)
if err != nil {
return err
}
return nil
}
func (e *Enforcer) UpdateSessionTimeout(id string, timeout int64) error {
err := e.notifyUpdateTimeout(id, timeout)
return err
}
func (e *Enforcer) GetTokenConfig() config.TokenConfig {
return e.config
}
func (e *Enforcer) GetLoginCounts() (int, error) {
adapter, ok := e.adapter.(persist.BatchAdapter)
if !ok {
return 0, fmt.Errorf("the adapter does not implement persist.BatchAdapter")
}
c, err := adapter.GetCountsFilteredKey(e.spliceSessionKey(""))
if err != nil {
return 0, err
}
return c, nil
}
func (e *Enforcer) GetLoginTokenCounts() (int, error) {
adapter, ok := e.adapter.(persist.BatchAdapter)
if !ok {
return 0, fmt.Errorf("the adapter does not implement persist.BatchAdapter")
}
c, err := adapter.GetCountsFilteredKey(e.spliceTokenKey(""))
if err != nil {
return 0, err
}
return c, nil
}
func (e *Enforcer) GetRefreshToken(tokenValue string) string {
return e.getRefreshTokenValue(tokenValue)
}
func (e *Enforcer) RefreshToken(refreshToken string, refreshModel ...*model.Refresh) (*model.RefreshRes, error) {
var m *model.Refresh
if len(refreshModel) != 0 {
m = refreshModel[0]
} else {
m = model.DefaultRefresh()
}
return e.RefreshTokenByModel(refreshToken, m, nil)
}
func (e *Enforcer) RefreshTokenByModel(refreshToken string, refreshModel *model.Refresh, ctx ...ctx.Context) (*model.RefreshRes, error) {
if refreshModel == nil {
return nil, errors.New("arg refreshModel can not be nil")
}
if !e.config.DoubleToken {
return nil, fmt.Errorf("double tokens are not enabled")
}
refreshTokenSign := e.getRefreshTokenSign(refreshToken)
if refreshTokenSign == nil {
return nil, fmt.Errorf("the refresh token does not exist: %v", refreshToken)
}
err := e.deleteRefreshToken(refreshTokenSign.Token)
if err != nil {
return nil, err
}
login := &model.Login{
Device: refreshTokenSign.Device,
IsLastingCookie: refreshModel.IsLastingCookie,
Timeout: refreshModel.Timeout,
JwtData: refreshModel.JwtData,
Token: refreshModel.Token,
RefreshToken: refreshModel.RefreshToken,
RefreshTokenTimeout: refreshModel.RefreshTokenTimeout,
}
token, err := e.LoginByModel(refreshTokenSign.Id, login, ctx...)
if err != nil {
return nil, err
}
return &model.RefreshRes{
Token: token,
RefreshToken: refreshToken,
}, nil
}
func (e *Enforcer) GetLoginDevices(id string) []string {
session := e.GetSession(id)
if session == nil {
return nil
}
return session.GetAllDevice()
}
func (e *Enforcer) GetDeviceByToken(token string) string {
id := e.getIdByToken(token)
session := e.GetSession(id)
if session == nil {
return ""
}
tokenSign := session.GetTokenSign(token)
if tokenSign == nil {
return ""
}
return tokenSign.Device
}