-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
57 lines (47 loc) · 1.17 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
package token
import "time"
type token struct {
Tokener
decoder Decoder
tokenType string
expires int64
}
func (t *token) GetDecoder() Decoder {
return t.decoder
}
func (t *token) GetType() string {
return t.tokenType
}
func (t *token) GetExpires() int64 {
return t.expires
}
func (t *token) Get(data interface{}) (token string, error error) {
time.Now().Unix()
d := NewData(data, t.GetType(), t.GetExpires())
return t.GetDecoder().Encode(d)
}
func (t *token) GetRefresh(data interface{}) (newRefreshToken string, error error) {
time.Now().Unix()
d := NewData(data, t.GetType(), t.GetExpires())
d.Refresh = true
return t.GetDecoder().Encode(d)
}
func (t *token) Refresh(refreshToken string) (newRefreshToken string, err error) {
data, err := t.GetDecoder().Decode(refreshToken)
if err != nil {
return "", err
}
data.Timestamp = time.Now().Unix()
data.Refresh = true
return t.GetDecoder().Encode(data)
}
func (t *token) Validate(token string) (data *Data, err error) {
return t.GetDecoder().Decode(token)
}
func NewToken(decoder Decoder, expires int64) *token {
return &token{
decoder: decoder,
tokenType: decoder.GetTokenType(),
expires: expires,
}
}