-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
256 lines (220 loc) · 5.66 KB
/
token.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
package gooauth2gorm
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"time"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
const (
defaultTokenTable = "oauth2_token"
defaultClientTable = "oauth2_client"
)
type Token struct {
gorm.Model
ExpiredAt int64
Code string
Access string
Refresh string
Data string
}
type TokenStore struct {
db *gorm.DB
table string
stdout io.Writer
ticker *time.Ticker
}
func NewTokenStore(cfg *Config, table string, gcInterval int) *TokenStore {
if cfg == nil {
panic(errors.New("db config is null"))
}
var d gorm.Dialector
switch cfg.DBType {
case PostgresSQL:
d = postgres.New(postgres.Config{
DSN: cfg.DSN,
})
case MySQL:
d = mysql.New(mysql.Config{
DSN: cfg.DSN,
})
case SQLite:
d = sqlite.Open(cfg.DSN)
case SQLServer:
d = sqlserver.Open(cfg.DSN)
case Clickhouse:
d = clickhouse.Open(cfg.DSN)
}
db, err := gorm.Open(d)
if err != nil {
panic(err)
}
sqlDB, err := db.DB()
if err != nil {
panic(err)
}
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
return NewTokenStoreWithDB(cfg, db, table, gcInterval)
}
func NewTokenStoreWithDB(cfg *Config, db *gorm.DB, table string, gcInterval int) *TokenStore {
ts := &TokenStore{
db: db,
table: defaultTokenTable,
}
if table != "" {
ts.table = table
}
interval := 600
if gcInterval > 0 {
interval = gcInterval
}
ts.ticker = time.NewTicker(time.Second * time.Duration(interval))
if !db.Migrator().HasTable(ts.table) {
if err := db.Table(ts.table).Migrator().CreateTable(&Token{}); err != nil {
panic(err)
}
}
go ts.gc()
return ts
}
func (ts TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
data, err := json.Marshal(info)
if err != nil {
return err
}
token := &Token{
Data: string(data),
}
if code := info.GetCode(); code != "" {
token.Code = code
token.ExpiredAt = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn()).Unix()
} else {
token.Access = info.GetAccess()
token.ExpiredAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()).Unix()
if refresh := info.GetRefresh(); refresh != "" {
token.Refresh = info.GetRefresh()
token.ExpiredAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Unix()
}
}
return ts.db.WithContext(ctx).Table(ts.table).Create(token).Error
}
// delete the authorization code
func (ts *TokenStore) RemoveByCode(ctx context.Context, code string) error {
return ts.db.WithContext(ctx).Table(ts.table).
Where("code = ?", code).
Update("code", "").Error
}
// use the access token to delete the token information
func (ts *TokenStore) RemoveByAccess(ctx context.Context, access string) error {
return ts.db.WithContext(ctx).Table(ts.table).
Where("access = ?", access).
Update("access", "").Error
}
// use the refresh token to delete the token information
func (ts *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
return ts.db.WithContext(ctx).Table(ts.table).
Where("refresh = ?", refresh).
Update("refresh", "").Error
}
// use the authorization code for token information data
func (ts *TokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
if code == "" {
return nil, nil
}
var token Token
if err := ts.db.WithContext(ctx).Table(ts.table).
Where("code = ?", code).
Find(&token).Error; err != nil {
return nil, err
}
if token.ID == 0 {
return nil, nil
}
return ts.toTokenInfo(token.Data), nil
}
// GetByAccess use the access token for token information data
func (ts *TokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
if access == "" {
return nil, nil
}
var token Token
if err := ts.db.WithContext(ctx).Table(ts.table).
Where("access = ?", access).
Find(&token).Error; err != nil {
return nil, err
}
if token.ID == 0 {
return nil, nil
}
return ts.toTokenInfo(token.Data), nil
}
//GetByRefresh use the refresh token for token information data
func (ts *TokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
if refresh == "" {
return nil, nil
}
var token Token
if err := ts.db.WithContext(ctx).Table(ts.table).
Where("refresh = ?", refresh).
Find(&token).Error; err != nil {
return nil, err
}
if token.ID == 0 {
return nil, nil
}
return ts.toTokenInfo(token.Data), nil
}
// SetStdout set error output
func (ts *TokenStore) SetStdout(stdout io.Writer) *TokenStore {
ts.stdout = stdout
return ts
}
// Close close the store
func (ts *TokenStore) Close() {
ts.ticker.Stop()
}
func (ts *TokenStore) toTokenInfo(data string) oauth2.TokenInfo {
var t models.Token
err := json.Unmarshal([]byte(data), &t)
if err != nil {
return nil
}
return &t
}
func (ts *TokenStore) gc() {
for range ts.ticker.C {
ts.cleanup()
}
}
func (ts TokenStore) cleanup() {
for range ts.ticker.C {
now := time.Now().Unix()
var count int64
if err := ts.db.Table(ts.table).Where("expired_at <= ?", now).Or("code = ? and access = ? AND refresh = ?", "", "", "").Count(&count).Error; err != nil {
ts.errorf("%s\n", err)
return
}
if count > 0 {
if err := ts.db.Table(ts.table).Where("expired_at <= ?", now).Or("code = ? and access = ? AND refresh = ?", "", "", "").Unscoped().Delete(&Token{}).Error; err != nil {
ts.errorf("%s\n", err)
}
}
}
}
func (ts *TokenStore) errorf(format string, args ...interface{}) {
if ts.stdout != nil {
buf := fmt.Sprintf(format, args...)
_, _ = ts.stdout.Write([]byte(buf))
}
}