-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstore.go
177 lines (154 loc) · 4.4 KB
/
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
package mysql
import (
"context"
"database/sql"
"fmt"
"io"
"time"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
jsoniter "github.com/json-iterator/go"
"gopkg.in/gorp.v2"
)
// Store mysql token store
type Store struct {
tableName string
db *gorp.DbMap
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.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(*) FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')", s.tableName)
n, err := s.db.SelectInt(query, now)
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()
}
}
return s.db.Insert(item)
}
// 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 && err == sql.ErrNoRows {
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
err := s.db.SelectOne(&item, query, code)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
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
err := s.db.SelectOne(&item, query, access)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
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
err := s.db.SelectOne(&item, query, refresh)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return s.toTokenInfo(item.Data), nil
}