Skip to content

Commit da15acc

Browse files
committed
Token storage optimization
1 parent 65792f0 commit da15acc

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

token.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ package redis
22

33
import (
44
"encoding/json"
5-
"strconv"
5+
"time"
66

7+
"github.com/satori/go.uuid"
78
"gopkg.in/redis.v4"
89

910
"gopkg.in/oauth2.v3"
1011
"gopkg.in/oauth2.v3/models"
1112
)
1213

13-
// DefaultIncrKey The name of the key stored on the ID
14-
const DefaultIncrKey = "oauth2_incr"
15-
1614
// NewTokenStore Create a token store instance based on redis
1715
func NewTokenStore(cfg *Config) (ts oauth2.TokenStore, err error) {
1816
opt := &redis.Options{
@@ -41,35 +39,20 @@ type TokenStore struct {
4139
cli *redis.Client
4240
}
4341

44-
func (rs *TokenStore) getBasicID(id int64, info oauth2.TokenInfo) string {
45-
return "oauth2_" + info.GetClientID() + "_" + strconv.FormatInt(id, 10)
46-
}
47-
4842
// Create Create and store the new token information
4943
func (rs *TokenStore) Create(info oauth2.TokenInfo) (err error) {
44+
ct := time.Now()
5045
jv, err := json.Marshal(info)
5146
if err != nil {
5247
return
5348
}
54-
id, err := rs.cli.Incr(DefaultIncrKey).Result()
55-
if err != nil {
56-
return
57-
}
5849
pipe := rs.cli.Pipeline()
59-
basicID := rs.getBasicID(id, info)
50+
basicID := uuid.NewV4().String()
6051
aexp := info.GetAccessExpiresIn()
6152
rexp := aexp
6253

6354
if refresh := info.GetRefresh(); refresh != "" {
64-
rexp = info.GetRefreshExpiresIn()
65-
ttl := rs.cli.TTL(refresh)
66-
if verr := ttl.Err(); verr != nil {
67-
err = verr
68-
return
69-
}
70-
if v := ttl.Val(); v.Seconds() > 0 {
71-
rexp = v
72-
}
55+
rexp = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Sub(ct)
7356
if aexp.Seconds() > rexp.Seconds() {
7457
aexp = rexp
7558
}

0 commit comments

Comments
 (0)