-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_store.go
221 lines (180 loc) · 5.56 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
package arangostore
import (
"context"
"encoding/json"
"time"
arangoDriver "github.com/arangodb/go-driver"
"github.com/go-oauth2/oauth2/v4"
"github.com/go-oauth2/oauth2/v4/models"
)
const (
// DefaultTokenStoreCollection is the default collection for storing tokens.
DefaultTokenStoreCollection = "oauth2_tokens" // nolint: gosec
)
// TokenStoreOption is a function that configures the TokenStore.
type TokenStoreOption func(*TokenStore) error
// WithTokenStoreCollection configures the collection for the TokenStore.
func WithTokenStoreCollection(collection string) TokenStoreOption {
return func(s *TokenStore) error {
if collection == "" {
return ErrNoCollection
}
s.collection = collection
return nil
}
}
// WithTokenStoreDatabase configures the database for the TokenStore.
func WithTokenStoreDatabase(db arangoDriver.Database) TokenStoreOption {
return func(s *TokenStore) error {
if db == nil {
return ErrNoDatabase
}
s.db = db
return nil
}
}
// TokenStoreItem data item
type TokenStoreItem struct {
Key string `json:"_key,omitempty"`
Code string `json:"code"`
Access string `json:"access_token"`
Refresh string `json:"refresh_token"`
Data []byte `json:"data"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
// TokenStore is a data struct that stores oauth2 token information.
type TokenStore struct {
db arangoDriver.Database
collection string
}
func (s *TokenStore) getByQuery(ctx context.Context, query string, bindVars map[string]any) (oauth2.TokenInfo, error) {
cursor, err := s.db.Query(ctx, query, bindVars)
if err != nil {
return nil, err
}
defer func(cursor arangoDriver.Cursor) {
if err := cursor.Close(); err != nil {
panic(err)
}
}(cursor)
var doc TokenStoreItem
for cursor.HasMore() {
_, err := cursor.ReadDocument(ctx, &doc)
if err != nil {
return nil, err
}
}
var info models.Token
if err = json.Unmarshal(doc.Data, &info); err != nil {
return nil, err
}
return &info, nil
}
func (s *TokenStore) removeByQuery(ctx context.Context, query string, bindVars map[string]any) error {
cursor, err := s.db.Query(ctx, query, bindVars)
if err != nil {
return err
}
return cursor.Close()
}
// Create creates a new token in the store.
func (s *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
data, err := json.Marshal(info)
if err != nil {
return err
}
coll, err := s.db.Collection(ctx, s.collection)
if err != nil {
return err
}
doc := TokenStoreItem{
Data: data,
CreatedAt: time.Now(),
}
if code := info.GetCode(); code != "" {
doc.Code = code
doc.ExpiresAt = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn())
} else {
if access := info.GetAccess(); access != "" {
doc.Access = info.GetAccess()
doc.ExpiresAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
}
if refresh := info.GetRefresh(); refresh != "" {
doc.Refresh = info.GetRefresh()
doc.ExpiresAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
}
}
_, err = coll.CreateDocument(ctx, doc)
if err != nil {
return err
}
return nil
}
// GetByCode returns the token by its authorization code.
func (s *TokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
query := "FOR doc IN @@collection FILTER doc.code == @code RETURN doc"
bindVars := map[string]interface{}{
"@collection": s.collection,
"code": code,
}
return s.getByQuery(ctx, query, bindVars)
}
// GetByAccess returns the token by its access token.
func (s *TokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
query := "FOR doc IN @@collection FILTER doc.access_token == @access_token RETURN doc"
bindVars := map[string]interface{}{
"@collection": s.collection,
"access_token": access,
}
return s.getByQuery(ctx, query, bindVars)
}
// GetByRefresh returns the token by its refresh token.
func (s *TokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
query := "FOR doc IN @@collection FILTER doc.refresh_token == @refresh_token RETURN doc"
bindVars := map[string]interface{}{
"@collection": s.collection,
"refresh_token": refresh,
}
return s.getByQuery(ctx, query, bindVars)
}
// RemoveByCode deletes the token by its authorization code.
func (s *TokenStore) RemoveByCode(ctx context.Context, code string) error {
query := "FOR doc IN @@collection FILTER doc.code == @code REMOVE doc IN @@collection"
bindVars := map[string]interface{}{
"@collection": s.collection,
"code": code,
}
return s.removeByQuery(ctx, query, bindVars)
}
func (s *TokenStore) RemoveByAccess(ctx context.Context, access string) error {
query := "FOR doc IN @@collection FILTER doc.access_token == @access_token REMOVE doc IN @@collection"
bindVars := map[string]interface{}{
"@collection": s.collection,
"access_token": access,
}
return s.removeByQuery(ctx, query, bindVars)
}
func (s *TokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
query := "FOR doc IN @@collection FILTER doc.refresh_token == @refresh_token REMOVE doc IN @@collection"
bindVars := map[string]interface{}{
"@collection": s.collection,
"refresh_token": refresh,
}
return s.removeByQuery(ctx, query, bindVars)
}
// NewTokenStore creates a new TokenStore.
func NewTokenStore(opts ...TokenStoreOption) (*TokenStore, error) {
s := &TokenStore{
collection: DefaultTokenStoreCollection,
}
for _, o := range opts {
if err := o(s); err != nil {
return nil, err
}
}
if s.db == nil {
return nil, ErrNoDatabase
}
return s, nil
}