Skip to content

Commit 26118d2

Browse files
committed
Token storage optimization
1 parent b2aeb71 commit 26118d2

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

token.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,14 @@ func (ts *TokenStore) c(name string) *mgo.Collection {
8181

8282
// Create Create and store the new token information
8383
func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
84-
var expiredAt time.Time
84+
aexp := info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
85+
rexp := aexp
8586
if refresh := info.GetRefresh(); refresh != "" {
86-
expiredAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
87-
rinfo, rerr := ts.GetByRefresh(refresh)
88-
if rerr != nil {
89-
err = rerr
90-
return
91-
}
92-
if rinfo != nil {
93-
expiredAt = rinfo.GetRefreshCreateAt().Add(rinfo.GetRefreshExpiresIn())
87+
rexp = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
88+
if aexp.Second() > rexp.Second() {
89+
aexp = rexp
9490
}
9591
}
96-
if expiredAt.IsZero() {
97-
expiredAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
98-
}
9992
id := bson.NewObjectId().Hex()
10093
jv, err := json.Marshal(info)
10194
if err != nil {
@@ -108,15 +101,15 @@ func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
108101
Assert: txn.DocMissing,
109102
Insert: basicData{
110103
Data: jv,
111-
ExpiredAt: expiredAt,
104+
ExpiredAt: rexp,
112105
},
113106
}, {
114107
C: ts.tcfg.AccessCName,
115108
Id: info.GetAccess(),
116109
Assert: txn.DocMissing,
117110
Insert: tokenData{
118111
BasicID: id,
119-
ExpiredAt: info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()),
112+
ExpiredAt: aexp,
120113
},
121114
}}
122115
if refresh := info.GetRefresh(); refresh != "" {
@@ -126,7 +119,7 @@ func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
126119
Assert: txn.DocMissing,
127120
Insert: tokenData{
128121
BasicID: id,
129-
ExpiredAt: expiredAt,
122+
ExpiredAt: rexp,
130123
},
131124
})
132125
}

0 commit comments

Comments
 (0)