-
Notifications
You must be signed in to change notification settings - Fork 9
/
token.go
278 lines (260 loc) · 7.06 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package dynamo
import (
"encoding/json"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/contamobi/oauth2"
"github.com/contamobi/oauth2/models"
"gopkg.in/mgo.v2/bson"
)
func NewTokenStore(config *Config) (store oauth2.TokenStore) {
session := config.SESSION
svc := dynamodb.New(session)
return &TokenStore{
config: config,
session: svc,
}
}
type TokenStore struct {
config *Config
session *dynamodb.DynamoDB
}
type tokenData struct {
ID string `json:"_id"`
BasicID string `json:"BasicID"`
ExpiredAt time.Time `json:"ExpiredAt"`
}
type basicData struct {
ID string `json:"_id"`
Data []byte `json:"Data"`
ExpiredAt time.Time `json:"ExpiredAt"`
}
// Create and store the new token information
func (tokenStorage *TokenStore) Create(info oauth2.TokenInfo) (err error) {
if code := info.GetCode(); code != "" {
err = CreateWithAuthorizationCode(tokenStorage, info, "")
if err != nil {
fmt.Printf("CreateWithAuthorizationCode error: %s\n", err)
}
return
}
if refresh := info.GetRefresh(); refresh != "" {
err = CreateWithRefreshToken(tokenStorage, info)
} else {
err = CreateWithAccessToken(tokenStorage, info, "")
}
return
}
func CreateWithAuthorizationCode(tokenStorage *TokenStore, info oauth2.TokenInfo, id string) (err error) {
code := info.GetCode()
if len(id) > 0 {
code = id
}
data, err := json.Marshal(info)
if err != nil {
return
}
expiredAt := info.GetCodeCreateAt().Add(info.GetCodeExpiresIn())
rExpiredAt := expiredAt
if refresh := info.GetRefresh(); refresh != "" {
rexp := info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
if expiredAt.Second() > rexp.Second() {
expiredAt = rexp
}
rExpiredAt = rexp
}
exp := rExpiredAt.Format(time.RFC3339)
params := &dynamodb.PutItemInput{
TableName: aws.String(tokenStorage.config.TABLE.BasicCname),
Item: map[string]*dynamodb.AttributeValue{
"ID": &dynamodb.AttributeValue{
S: aws.String(code),
},
"Data": &dynamodb.AttributeValue{
B: data,
},
"ExpiredAt": &dynamodb.AttributeValue{
S: &exp,
},
},
}
_, err = tokenStorage.session.PutItem(params)
return
}
func CreateWithAccessToken(tokenStorage *TokenStore, info oauth2.TokenInfo, id string) (err error) {
if len(id) == 0 {
id = bson.NewObjectId().Hex()
}
err = CreateWithAuthorizationCode(tokenStorage, info, id)
if err != nil {
return
}
expiredAt := info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()).Format(time.RFC3339)
accessParams := &dynamodb.PutItemInput{
TableName: aws.String(tokenStorage.config.TABLE.AccessCName),
Item: map[string]*dynamodb.AttributeValue{
"ID": &dynamodb.AttributeValue{
S: aws.String(info.GetAccess()),
},
"BasicID": &dynamodb.AttributeValue{
S: &id,
},
"ExpiredAt": &dynamodb.AttributeValue{
S: &expiredAt,
},
},
}
_, err = tokenStorage.session.PutItem(accessParams)
return
}
func CreateWithRefreshToken(tokenStorage *TokenStore, info oauth2.TokenInfo) (err error) {
id := bson.NewObjectId().Hex()
err = CreateWithAccessToken(tokenStorage, info, id)
if err != nil {
return
}
expiredAt := info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Format(time.RFC3339)
refreshParams := &dynamodb.PutItemInput{
TableName: aws.String(tokenStorage.config.TABLE.RefreshCName),
Item: map[string]*dynamodb.AttributeValue{
"ID": &dynamodb.AttributeValue{
S: aws.String(info.GetRefresh()),
},
"BasicID": &dynamodb.AttributeValue{
S: &id,
},
"ExpiredAt": &dynamodb.AttributeValue{
S: &expiredAt,
},
},
}
_, err = tokenStorage.session.PutItem(refreshParams)
return
}
// RemoveByCode use the authorization code to delete the token information
func (tokenStorage *TokenStore) RemoveByCode(code string) (err error) {
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(code),
},
},
TableName: aws.String(tokenStorage.config.TABLE.BasicCname),
}
_, err = tokenStorage.session.DeleteItem(input)
if err != nil {
fmt.Printf("RemoveByCode error: %s\n", err.Error())
}
return
}
// RemoveByAccess use the access token to delete the token information
func (tokenStorage *TokenStore) RemoveByAccess(access string) (err error) {
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(access),
},
},
TableName: aws.String(tokenStorage.config.TABLE.AccessCName),
}
_, err = tokenStorage.session.DeleteItem(input)
if err != nil {
fmt.Printf("RemoveByAccess error: %s\n", err.Error())
}
return
}
// RemoveByRefresh use the refresh token to delete the token information
func (tokenStorage *TokenStore) RemoveByRefresh(refresh string) (err error) {
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(refresh),
},
},
TableName: aws.String(tokenStorage.config.TABLE.RefreshCName),
}
_, err = tokenStorage.session.DeleteItem(input)
if err != nil {
fmt.Printf("RemoveByRefresh error: %s\n", err.Error())
}
return
}
func (tokenStorage *TokenStore) getData(basicID string) (to oauth2.TokenInfo, err error) {
if len(basicID) == 0 {
return
}
input := &dynamodb.GetItemInput{
TableName: aws.String(tokenStorage.config.TABLE.BasicCname),
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(basicID),
},
},
}
result, err := tokenStorage.session.GetItem(input)
if err != nil {
return
}
if len(result.Item) == 0 {
return
}
var b basicData
err = dynamodbattribute.UnmarshalMap(result.Item, &b)
if err != nil {
return
}
var tm models.Token
err = json.Unmarshal(b.Data, &tm)
if err != nil {
return
}
to = &tm
return
}
func (tokenStorage *TokenStore) getBasicID(cname, token string) (basicID string, err error) {
input := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"ID": {
S: aws.String(token),
},
},
TableName: aws.String(cname),
}
result, err := tokenStorage.session.GetItem(input)
if err != nil {
return
}
var td tokenData
err = dynamodbattribute.UnmarshalMap(result.Item, &td)
if err != nil {
return
}
basicID = td.BasicID
return
}
// GetByCode use the authorization code for token information data
func (tokenStorage *TokenStore) GetByCode(code string) (to oauth2.TokenInfo, err error) {
to, err = tokenStorage.getData(code)
return
}
// GetByAccess use the access token for token information data
func (tokenStorage *TokenStore) GetByAccess(access string) (to oauth2.TokenInfo, err error) {
basicID, err := tokenStorage.getBasicID(tokenStorage.config.TABLE.AccessCName, access)
if err != nil && basicID == "" {
return
}
to, err = tokenStorage.getData(basicID)
return
}
// GetByRefresh use the refresh token for token information data
func (tokenStorage *TokenStore) GetByRefresh(refresh string) (to oauth2.TokenInfo, err error) {
basicID, err := tokenStorage.getBasicID(tokenStorage.config.TABLE.RefreshCName, refresh)
if err != nil && basicID == "" {
return
}
to, err = tokenStorage.getData(basicID)
return
}