-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken_store.go
292 lines (245 loc) · 7.14 KB
/
token_store.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
package oauth2xorm
import (
"context"
"database/sql"
"fmt"
"io"
"os"
"time"
"xorm.io/xorm"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/json-iterator/go"
)
// StoreItem data item
type StoreItem struct {
ID int64 `xorm:"id"`
ExpiredAt int64 `xorm:"expired_at"`
Code string `xorm:"code"`
Access string `xorm:"access"`
Refresh string `xorm:"refresh"`
Data string `xorm:"data"`
}
// NewConfig create mysql configuration instance
func NewConfig(dsn string) *Config {
return &Config{
DSN: dsn,
MaxLifetime: time.Hour * 2,
MaxOpenConns: 50,
MaxIdleConns: 25,
}
}
// Config mysql configuration
type Config struct {
DSN string
MaxLifetime time.Duration
MaxOpenConns int
MaxIdleConns int
}
// NewDefaultStore create mysql store instance
func NewDefaultStore(config *Config, autoMigrate bool) *Store {
return NewStore(config, "", 0, autoMigrate)
}
// NewStore create mysql store instance,
// config mysql configuration,
// tableName table name (default oauth2_token),
// GC time interval (in seconds, default 600)
func NewStore(config *Config, tableName string, gcInterval int, autoMigrate bool) *Store {
db, err := xorm.NewEngine("mysql", config.DSN)
if err != nil {
panic(err)
}
db.SetMaxOpenConns(config.MaxOpenConns)
db.SetMaxIdleConns(config.MaxIdleConns)
db.SetConnMaxLifetime(config.MaxLifetime)
return NewStoreWithDB(db, tableName, gcInterval, autoMigrate)
}
// NewStoreWithDB create mysql store instance,
// db sql.DB,
// tableName table name (default oauth2_token),
// GC time interval (in seconds, default 600)
func NewStoreWithDB(db *xorm.Engine, tableName string, gcInterval int, autoMigrate bool) *Store {
store := &Store{
db: db,
tableName: "oauth2_token",
stdout: os.Stderr,
}
if tableName != "" {
store.tableName = tableName
}
interval := 600
if gcInterval > 0 {
interval = gcInterval
}
store.ticker = time.NewTicker(time.Second * time.Duration(interval))
if autoMigrate {
stmt := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
code varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
access varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
refresh varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
expired_at int(11) NOT NULL,
data varchar(2048) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (id),
KEY idx_oauth2_token_code (code),
KEY idx_oauth2_token_expired_at (expired_at),
KEY idx_oauth2_token_access (access),
KEY idx_oauth2_token_refresh (refresh)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`, store.tableName)
_, err := db.Exec(stmt)
if err != nil {
panic(err)
}
}
go store.gc()
return store
}
// Store mysql token store
type Store struct {
tableName string
db *xorm.Engine
stdout io.Writer
ticker *time.Ticker
}
// SetStdout set error output
func (s *Store) SetStdout(stdout io.Writer) *Store {
s.stdout = stdout
return s
}
// Close close the store
func (s *Store) Close() {
s.ticker.Stop()
s.db.Close()
}
func (s *Store) gc() {
for range s.ticker.C {
s.clean()
}
}
func (s *Store) clean() {
now := time.Now().Unix()
//query := fmt.Sprintf("SELECT COUNT(1) FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')", s.tableName)
//n, err := s.db.SelectInt(query, now)
n, err := s.db.Where("expired_at >= ?", now).Or("code='' AND access='' AND refresh=''").Table(s.tableName).Count()
if err != nil || n == 0 {
if err != nil {
s.errorf(err.Error())
}
return
}
_, err = s.db.Exec(fmt.Sprintf("DELETE FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')", s.tableName), now)
if err != nil {
s.errorf(err.Error())
}
}
func (s *Store) errorf(format string, args ...interface{}) {
if s.stdout != nil {
buf := fmt.Sprintf("[OAUTH2-MYSQL-ERROR]: "+format, args...)
s.stdout.Write([]byte(buf))
}
}
// Create create and store the new token information
func (s *Store) Create(ctx context.Context, info oauth2.TokenInfo) error {
buf, _ := jsoniter.Marshal(info)
item := &StoreItem{
Data: string(buf),
}
if code := info.GetCode(); code != "" {
item.Code = code
item.ExpiredAt = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn()).Unix()
} else {
item.Access = info.GetAccess()
item.ExpiredAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()).Unix()
if refresh := info.GetRefresh(); refresh != "" {
item.Refresh = info.GetRefresh()
item.ExpiredAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Unix()
}
}
_, err := s.db.Table(s.tableName).Insert(item)
return err
}
// RemoveByCode delete the authorization code
func (s *Store) RemoveByCode(ctx context.Context, code string) error {
query := fmt.Sprintf("UPDATE %s SET code='' WHERE code=? LIMIT 1", s.tableName)
_, err := s.db.Exec(query, code)
if err != nil {
return nil
}
return err
}
// RemoveByAccess use the access token to delete the token information
func (s *Store) RemoveByAccess(ctx context.Context, access string) error {
query := fmt.Sprintf("UPDATE %s SET access='' WHERE access=? LIMIT 1", s.tableName)
_, err := s.db.Exec(query, access)
if err != nil && err == sql.ErrNoRows {
return nil
}
return err
}
// RemoveByRefresh use the refresh token to delete the token information
func (s *Store) RemoveByRefresh(ctx context.Context, refresh string) error {
query := fmt.Sprintf("UPDATE %s SET refresh='' WHERE refresh=? LIMIT 1", s.tableName)
_, err := s.db.Exec(query, refresh)
if err != nil && err == sql.ErrNoRows {
return nil
}
return err
}
func (s *Store) toTokenInfo(data string) oauth2.TokenInfo {
var tm models.Token
jsoniter.Unmarshal([]byte(data), &tm)
return &tm
}
// GetByCode use the authorization code for token information data
func (s *Store) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
if code == "" {
return nil, nil
}
//query := fmt.Sprintf("SELECT * FROM %s WHERE code=? LIMIT 1", s.tableName)
//var item StoreItem
item := StoreItem{Code: code}
has, err := s.db.Table(s.tableName).Get(&item)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return s.toTokenInfo(item.Data), nil
}
// GetByAccess use the access token for token information data
func (s *Store) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
if access == "" {
return nil, nil
}
//query := fmt.Sprintf("SELECT * FROM %s WHERE access=? LIMIT 1", s.tableName)
//var item StoreItem
item := StoreItem{Access: access}
has, err := s.db.Table(s.tableName).Get(&item)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return s.toTokenInfo(item.Data), nil
}
// GetByRefresh use the refresh token for token information data
func (s *Store) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
if refresh == "" {
return nil, nil
}
//query := fmt.Sprintf("SELECT * FROM %s WHERE refresh=? LIMIT 1", s.tableName)
//var item StoreItem
item := StoreItem{Refresh: refresh}
has, err := s.db.Table(s.tableName).Get(&item)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return s.toTokenInfo(item.Data), nil
}