-
Notifications
You must be signed in to change notification settings - Fork 193
/
oauth2.go
108 lines (92 loc) · 2.51 KB
/
oauth2.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
package oauth2
import (
"fmt"
"sync"
"github.com/eolinker/apinto/resources"
scope_manager "github.com/eolinker/apinto/scope-manager"
"github.com/eolinker/eosc/log"
"github.com/eolinker/apinto/application"
http_service "github.com/eolinker/eosc/eocontext/http-context"
)
var _ application.IAuth = (*oauth2)(nil)
type oauth2 struct {
id string
tokenName string
position string
users application.IUserManager
cache scope_manager.IProxyOutput[resources.ICache]
once sync.Once
}
func (o *oauth2) GetUser(ctx http_service.IHttpContext) (*application.UserInfo, bool) {
token, has := application.GetToken(ctx, o.tokenName, o.position)
if !has || token == "" {
return nil, false
}
o.once.Do(func() {
o.cache = scope_manager.Auto[resources.ICache]("", "redis")
})
list := o.cache.List()
if len(list) < 1 {
return nil, false
}
clientID, err := validToken(ctx.Context(), list[0], token)
if err != nil {
log.Error("valid token error:", err, "token:", token)
return nil, false
}
return o.users.Get(clientID)
}
func (o *oauth2) ID() string {
return o.id
}
func (o *oauth2) Driver() string {
return driverName
}
func (o *oauth2) 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 o.users.Check(appID, driverName, us)
}
func (o *oauth2) Set(app application.IApp, users []application.ITransformConfig) {
infos := make([]*application.UserInfo, 0, len(users))
for _, user := range users {
v, _ := user.Config().(*User)
client := &Client{
Pattern: &v.Pattern,
Expire: v.Expire,
}
if v.Pattern.HashSecret {
hr, err := extractHashRule(v.Pattern.ClientSecret)
if err != nil {
log.Error("extract hash error:", err, "client secret:", v.Pattern.ClientSecret)
continue
}
log.Debug("hash rule: ", *hr)
client.hashRule = hr
}
registerClient(v.Pattern.ClientId, client)
infos = append(infos, &application.UserInfo{
Name: v.Username(),
Value: v.Pattern.ClientSecret,
Expire: v.Expire,
Labels: v.Labels,
HideCredential: v.HideCredential,
TokenName: o.tokenName,
Position: o.position,
App: app,
})
}
o.users.Set(app.Id(), infos)
}
func (o *oauth2) Del(appID string) {
o.users.DelByAppID(appID)
}
func (o *oauth2) UserCount() int {
return o.users.Count()
}