-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
106 lines (84 loc) · 2.36 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
package session
import (
"fmt"
"strconv"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
"github.com/luanguimaraesla/freegrow/internal/cache"
)
type Token struct {
Uuid string
Id interface{}
Jwt string
ExpiresAt time.Time
}
// NewToken generates a new Token intance with an auto-generated Uuid
func NewToken(id interface{}, secret string, duration time.Duration) (*Token, error) {
expiresAt := time.Now().Add(duration)
tokenUuid := uuid.New().String()
jwtToken, err := generateJWT(tokenUuid, id, secret, expiresAt)
if err != nil {
return nil, fmt.Errorf("unable to create a new session token: %v", err)
}
token := &Token{tokenUuid, id, jwtToken, expiresAt}
return token, nil
}
// LoadToken receives a jwtToken and decodes its metadata to create
// a valid Token instance
func LoadToken(jwtToken, secret string) (*Token, error) {
token, err := parseJWT(jwtToken, secret)
if err != nil {
return nil, err
}
claims, ok := token.Claims.(jwt.MapClaims)
if ok && token.Valid {
tokenUuid, ok := claims["uuid"].(string)
if !ok {
return nil, fmt.Errorf("unable to decode token's uuid: key not found")
}
id, ok := claims["id"]
if !ok {
return nil, fmt.Errorf("unable to decode token's id: key not found")
}
expString, ok := claims["exp"]
if !ok {
return nil, fmt.Errorf("unable to decode token's exp: key not found")
}
exp, err := strconv.ParseInt(fmt.Sprintf("%.f", expString), 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse JWT exp: %v", err)
}
t := &Token{
Id: id,
Uuid: tokenUuid,
Jwt: jwtToken,
ExpiresAt: time.Unix(exp, 0),
}
return t, nil
}
return nil, fmt.Errorf("unable to decode JWT claims")
}
// Save persists token in the cache
func (t *Token) Save() error {
now := time.Now()
if err := cache.Setex(t.Uuid, fmt.Sprintf("%v", t.Id), t.ExpiresAt.Sub(now)); err != nil {
return fmt.Errorf("unable to persist token into cache: %v", err)
}
return nil
}
// Delete removes token from cache
func (t *Token) Delete() error {
if err := cache.Delete(t.Uuid); err != nil {
return err
}
return nil
}
// Check checks if the token is still on the cache
func (t *Token) Check() (string, error) {
id, err := cache.GetString(t.Uuid)
if err != nil {
return "", fmt.Errorf("unable to get session uuid in the cache: %v", err)
}
return id, nil
}