-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_store.go
322 lines (257 loc) · 8.98 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package pgstore
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
const (
// DefaultTokenStoreTable is the default collection for storing tokens.
DefaultTokenStoreTable = "oauth2_tokens" // nolint: gosec
)
// TokenStoreOption is a function that configures the TokenStore.
type TokenStoreOption func(*TokenStore) error
// WithTokenStoreTable configures the auth token table.
func WithTokenStoreTable(table string) TokenStoreOption {
return func(s *TokenStore) error {
if table == "" {
return ErrNoTable
}
s.table = table
return nil
}
}
// WithTokenStoreConnPool configures the connection pool.
func WithTokenStoreConnPool(pool *pgxpool.Pool) TokenStoreOption {
return func(s *TokenStore) error {
if pool == nil {
return ErrNoConnPool
}
s.pool = pool
return nil
}
}
// WithTokenStoreCleanupInterval configures the cleanup interval.
func WithTokenStoreCleanupInterval(interval time.Duration) TokenStoreOption {
return func(s *TokenStore) error {
s.cleanupInterval = interval
return nil
}
}
// WithTokenStoreLogger configures the logger.
func WithTokenStoreLogger(logger Logger) TokenStoreOption {
return func(s *TokenStore) error {
if logger == nil {
return ErrNoLogger
}
s.logger = logger
return nil
}
}
// TokenStoreItem data item
type TokenStoreItem struct {
ID int64 `db:"id"`
Code string `db:"code"`
Access string `db:"access_token"`
Refresh string `db:"refresh_token"`
Data []byte `db:"data"`
CreatedAt time.Time `db:"created_at"`
ExpiresAt time.Time `db:"expires_at"`
}
// TokenStore is a data struct that stores oauth2 token information.
type TokenStore struct {
pool *pgxpool.Pool
table string
logger Logger
cleanupInterval time.Duration
cleanupTicker *time.Ticker
}
// scanToTokenInfo scans a row into an oauth2.TokenInfo.
func (s *TokenStore) scanToTokenInfo(ctx context.Context, row pgx.Row) (oauth2.TokenInfo, error) {
var item TokenStoreItem
if err := row.Scan(&item.ID, &item.Code, &item.Access, &item.Refresh, &item.Data, &item.CreatedAt, &item.ExpiresAt); err != nil {
s.logger.Log(ctx, LogLevelError, err.Error())
return nil, err
}
var info models.Token
if err := json.Unmarshal(item.Data, &info); err != nil {
s.logger.Log(ctx, LogLevelError, err.Error())
return nil, err
}
s.logger.Log(ctx, LogLevelDebug, "token found", "id", item.ID)
return &info, nil
}
// cleanExpiredTokens removes expired tokens from the store.
func (s *TokenStore) cleanExpiredTokens(ctx context.Context) error {
_, err := s.pool.Query(ctx, fmt.Sprintf("DELETE FROM %s WHERE expires_at <= $1", s.table), time.Now())
s.logger.Log(ctx, LogLevelDebug, "cleaning expired tokens", "err", err)
return err
}
// InitCleanup initializes the cleanup process.
func (s *TokenStore) InitCleanup(ctx context.Context) {
if s.cleanupInterval > 0 {
s.cleanupTicker = time.NewTicker(s.cleanupInterval)
go func() {
for range s.cleanupTicker.C {
if err := s.cleanExpiredTokens(ctx); err != nil {
s.logger.Log(ctx, LogLevelError, err.Error())
}
}
}()
}
}
// InitTable initializes the token store table if it does not exist and creates
// the indexes.
func (s *TokenStore) InitTable(ctx context.Context) error {
s.logger.Log(ctx, LogLevelDebug, "initializing token store table", "table", s.table)
_, err := s.pool.Exec(ctx, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %[1]s (
id BIGSERIAL PRIMARY KEY NOT NULL,
code TEXT NOT NULL,
access_token TEXT NOT NULL,
refresh_token TEXT NOT NULL,
data JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
expires_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_%[1]s_code_idx ON %[1]s (code);
CREATE INDEX IF NOT EXISTS idx_%[1]s_access_idx ON %[1]s (access_token);
CREATE INDEX IF NOT EXISTS idx_%[1]s_refresh_idx ON %[1]s (refresh_token);
CREATE INDEX IF NOT EXISTS idx_%[1]s_expires_idx ON %[1]s (expires_at);`,
s.table,
))
if err != nil {
s.logger.Log(ctx, LogLevelError, err.Error())
return err
}
return nil
}
// Create creates a new token in the store.
func (s *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
s.logger.Log(ctx, LogLevelDebug, "creating token", "info", info)
data, err := json.Marshal(info)
if err != nil {
s.logger.Log(ctx, LogLevelError, err.Error())
return err
}
item := TokenStoreItem{
Data: data,
CreatedAt: time.Now(),
}
if code := info.GetCode(); code != "" {
item.Code = code
item.ExpiresAt = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn())
} else {
if access := info.GetAccess(); access != "" {
item.Access = info.GetAccess()
item.ExpiresAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
}
if refresh := info.GetRefresh(); refresh != "" {
item.Refresh = info.GetRefresh()
item.ExpiresAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
}
}
_, err = s.pool.Exec(ctx, fmt.Sprintf(`
INSERT INTO %s (code, access_token, refresh_token, data, created_at, expires_at)
VALUES ($1, $2, $3, $4, $5, $6)`,
s.table,
), item.Code, item.Access, item.Refresh, item.Data, item.CreatedAt, item.ExpiresAt)
if err != nil {
s.logger.Log(ctx, LogLevelError, err.Error(), "info", info, "item", item)
return err
}
s.logger.Log(ctx, LogLevelDebug, "token created")
return nil
}
// GetByCode returns the token by its authorization code.
func (s *TokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
s.logger.Log(ctx, LogLevelDebug, "getting token by authorization code", "code", code)
row := s.pool.QueryRow(ctx, fmt.Sprintf("SELECT * FROM %s WHERE code = $1", s.table), code)
return s.scanToTokenInfo(ctx, row)
}
// GetByAccess returns the token by its access token.
func (s *TokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
s.logger.Log(ctx, LogLevelDebug, "getting token by access token", "access", access)
row := s.pool.QueryRow(ctx, fmt.Sprintf("SELECT * FROM %s WHERE access_token = $1", s.table), access)
return s.scanToTokenInfo(ctx, row)
}
// GetByRefresh returns the token by its refresh token.
func (s *TokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
s.logger.Log(ctx, LogLevelDebug, "getting token by refresh token", "refresh", refresh)
row := s.pool.QueryRow(ctx, fmt.Sprintf("SELECT * FROM %s WHERE refresh_token = $1", s.table), refresh)
return s.scanToTokenInfo(ctx, row)
}
// RemoveByCode deletes the token by its authorization code.
func (s *TokenStore) RemoveByCode(ctx context.Context, code string) error {
s.logger.Log(ctx, LogLevelDebug, "removing token by authorization code", "code", code)
if code == "" {
s.logger.Log(ctx, LogLevelWarn, "no code was provided")
return nil
}
_, err := s.pool.Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE code = $1", s.table), code)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
s.logger.Log(ctx, LogLevelError, err.Error())
return err
}
s.logger.Log(ctx, LogLevelInfo, "token removed")
return nil
}
func (s *TokenStore) RemoveByAccess(ctx context.Context, access string) error {
s.logger.Log(ctx, LogLevelDebug, "removing token by access token", "access", access)
if access == "" {
s.logger.Log(ctx, LogLevelWarn, "no access was provided")
return nil
}
_, err := s.pool.Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE access_token = $1", s.table), access)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
s.logger.Log(ctx, LogLevelError, err.Error())
return err
}
s.logger.Log(ctx, LogLevelInfo, "token removed")
return nil
}
func (s *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
s.logger.Log(ctx, LogLevelDebug, "removing token by refresh token", "refresh", refresh)
if refresh == "" {
s.logger.Log(ctx, LogLevelWarn, "no refresh was provided")
return nil
}
_, err := s.pool.Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE refresh_token = $1", s.table), refresh)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
s.logger.Log(ctx, LogLevelError, err.Error())
return err
}
s.logger.Log(ctx, LogLevelInfo, "token removed")
return nil
}
// Close closes the store and releases any resources.
func (s *TokenStore) Close(ctx context.Context) {
s.logger.Log(ctx, LogLevelDebug, "closing token store")
if s.cleanupTicker != nil {
s.logger.Log(ctx, LogLevelDebug, "stopping cleanup ticker")
s.cleanupTicker.Stop()
}
s.logger.Log(ctx, LogLevelDebug, "token store closed")
}
// NewTokenStore creates a new TokenStore.
func NewTokenStore(opts ...TokenStoreOption) (*TokenStore, error) {
s := &TokenStore{
table: DefaultTokenStoreTable,
logger: new(NoopLogger),
}
for _, o := range opts {
if err := o(s); err != nil {
return nil, err
}
}
if s.pool == nil {
return nil, ErrNoConnPool
}
s.InitCleanup(context.Background())
return s, nil
}