-
Notifications
You must be signed in to change notification settings - Fork 198
/
jwt.go
78 lines (66 loc) · 1.73 KB
/
jwt.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
package jwt
import (
"fmt"
"github.com/eolinker/apinto/application"
"github.com/eolinker/eosc/log"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)
var _ application.IAuth = (*jwt)(nil)
type jwt struct {
id string
tokenName string
position string
cfg *Rule
users application.IUserManager
}
func (j *jwt) GetUser(ctx http_service.IHttpContext) (*application.UserInfo, bool) {
token, has := application.GetToken(ctx, j.tokenName, j.position)
if !has || token == "" {
return nil, false
}
name, err := j.doJWTAuthentication(token)
if err != nil {
log.DebugF("[%s] get user error:%s", driverName, token)
return nil, false
}
return j.users.Get(name)
}
func (j *jwt) ID() string {
return j.id
}
func (j *jwt) Driver() string {
return driverName
}
func (j *jwt) Check(appID string, users []application.ITransformConfig) error {
us := make([]application.IUser, 0, len(users))
for _, u := range users {
v, ok := u.Config().(*User)
if !ok {
return fmt.Errorf("%s check error: invalid config type", driverName)
}
us = append(us, v)
}
return j.users.Check(appID, driverName, us)
}
func (j *jwt) Set(app application.IApp, users []application.ITransformConfig) {
infos := make([]*application.UserInfo, 0, len(users))
for _, user := range users {
v, _ := user.Config().(*User)
infos = append(infos, &application.UserInfo{
Name: v.Username(),
Expire: v.Expire,
Labels: v.Labels,
HideCredential: v.HideCredential,
TokenName: j.tokenName,
Position: j.position,
App: app,
})
}
j.users.Set(app.Id(), infos)
}
func (j *jwt) Del(appID string) {
j.users.DelByAppID(appID)
}
func (j *jwt) UserCount() int {
return j.users.Count()
}