@@ -2,17 +2,15 @@ package redis
2
2
3
3
import (
4
4
"encoding/json"
5
- "strconv "
5
+ "time "
6
6
7
+ "github.com/satori/go.uuid"
7
8
"gopkg.in/redis.v4"
8
9
9
10
"gopkg.in/oauth2.v3"
10
11
"gopkg.in/oauth2.v3/models"
11
12
)
12
13
13
- // DefaultIncrKey The name of the key stored on the ID
14
- const DefaultIncrKey = "oauth2_incr"
15
-
16
14
// NewTokenStore Create a token store instance based on redis
17
15
func NewTokenStore (cfg * Config ) (ts oauth2.TokenStore , err error ) {
18
16
opt := & redis.Options {
@@ -41,35 +39,20 @@ type TokenStore struct {
41
39
cli * redis.Client
42
40
}
43
41
44
- func (rs * TokenStore ) getBasicID (id int64 , info oauth2.TokenInfo ) string {
45
- return "oauth2_" + info .GetClientID () + "_" + strconv .FormatInt (id , 10 )
46
- }
47
-
48
42
// Create Create and store the new token information
49
43
func (rs * TokenStore ) Create (info oauth2.TokenInfo ) (err error ) {
44
+ ct := time .Now ()
50
45
jv , err := json .Marshal (info )
51
46
if err != nil {
52
47
return
53
48
}
54
- id , err := rs .cli .Incr (DefaultIncrKey ).Result ()
55
- if err != nil {
56
- return
57
- }
58
49
pipe := rs .cli .Pipeline ()
59
- basicID := rs . getBasicID ( id , info )
50
+ basicID := uuid . NewV4 (). String ( )
60
51
aexp := info .GetAccessExpiresIn ()
61
52
rexp := aexp
62
53
63
54
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 )
73
56
if aexp .Seconds () > rexp .Seconds () {
74
57
aexp = rexp
75
58
}
0 commit comments