-
Notifications
You must be signed in to change notification settings - Fork 43
/
jwtToken.go
105 lines (79 loc) · 2.76 KB
/
jwtToken.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
package jwt
import (
"errors"
"log"
"time"
jwtGo "github.com/dgrijalva/jwt-go"
)
type jwtToken struct {
Token *jwtGo.Token
ParseErr error
options tokenOptions
}
type tokenOptions struct {
ValidTime time.Duration
SigningMethodString string
Debug bool
}
func (t *jwtToken) myLog(stoofs interface{}) {
if t.options.Debug {
log.Println(stoofs)
}
}
func (c *credentials) buildTokenWithClaimsFromString(tokenString string, verifyKey interface{}, validTime time.Duration) *jwtToken {
// note @adam-hanna: should we be checking inputs? Especially the token string?
var newToken jwtToken
token, err := jwtGo.ParseWithClaims(tokenString, &ClaimsType{}, func(token *jwtGo.Token) (interface{}, error) {
if token.Method != jwtGo.GetSigningMethod(c.options.SigningMethodString) {
c.myLog("Incorrect singing method on token")
return nil, errors.New("Incorrect singing method on token")
}
return verifyKey, nil
})
newToken.Token = token
newToken.ParseErr = err
newToken.options.ValidTime = validTime
newToken.options.SigningMethodString = c.options.SigningMethodString
newToken.options.Debug = c.options.Debug
return &newToken
}
func (c *credentials) newTokenWithClaims(claims *ClaimsType, validTime time.Duration) *jwtToken {
var newToken jwtToken
newToken.Token = jwtGo.NewWithClaims(jwtGo.GetSigningMethod(c.options.SigningMethodString), claims)
newToken.ParseErr = nil
newToken.options.ValidTime = validTime
newToken.options.SigningMethodString = c.options.SigningMethodString
newToken.options.Debug = c.options.Debug
return &newToken
}
func (t *jwtToken) updateTokenExpiry() *jwtError {
tokenClaims, ok := t.Token.Claims.(*ClaimsType)
if !ok {
return newJwtError(errors.New("Cannot read token claims"), 500)
}
tokenClaims.StandardClaims.ExpiresAt = time.Now().Add(t.options.ValidTime).Unix()
// update the token
t.Token = jwtGo.NewWithClaims(jwtGo.GetSigningMethod(t.options.SigningMethodString), tokenClaims)
return nil
}
func (t *jwtToken) updateTokenCsrf(csrfString string) *jwtError {
tokenClaims, ok := t.Token.Claims.(*ClaimsType)
if !ok {
return newJwtError(errors.New("Cannot read token claims"), 500)
}
tokenClaims.Csrf = csrfString
// update the token
t.Token = jwtGo.NewWithClaims(jwtGo.GetSigningMethod(t.options.SigningMethodString), tokenClaims)
return nil
}
func (t *jwtToken) updateTokenExpiryAndCsrf(csrfString string) *jwtError {
tokenClaims, ok := t.Token.Claims.(*ClaimsType)
if !ok {
return newJwtError(errors.New("Cannot read token claims"), 500)
}
tokenClaims.StandardClaims.ExpiresAt = time.Now().Add(t.options.ValidTime).Unix()
tokenClaims.Csrf = csrfString
// update the token
t.Token = jwtGo.NewWithClaims(jwtGo.GetSigningMethod(t.options.SigningMethodString), tokenClaims)
return nil
}