-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
162 lines (124 loc) · 3.01 KB
/
jwt.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jwt
import (
"encoding/json"
"errors"
"strings"
"time"
)
// TimeFunc is used to get the current time when validating the "exp" claim
var TimeFunc = time.Now
var (
ErrInvalidKey = errors.New("The key is invalid or of invalid type")
ErrHashUnavailable = errors.New("The hashing algorithm is not available")
ErrBadSignature = errors.New("The signature doesn't match")
ErrTokenMalformed = errors.New("The token is malformed")
)
const (
BadSignatureError ValidationError = 1 << iota
ExpiredError
NotYetValidError
)
type ValidationError uint32
func (e ValidationError) Error() string {
return "the token is invalid"
}
type Token interface {
Encode(key interface{}) (payload string, err error)
Claim(string) interface{}
SetClaim(string, interface{})
}
type token struct {
raw string
alg SigningAlgorithm
header map[string]interface{}
claims map[string]interface{}
signature string
}
// NewToken creates a new token with the specified SigningAlgorithm
func NewToken(alg SigningAlgorithm) Token {
return &token{
header: map[string]interface{}{
"typ": "JWT",
"alg": alg.Name(),
},
claims: make(map[string]interface{}),
alg: alg,
}
}
func ParseToken(tokenString string, alg SigningAlgorithm, key interface{}) (Token, error) {
segments := strings.Split(tokenString, ".")
if len(segments) != 3 {
return nil, ErrTokenMalformed
}
t := &token{
raw: tokenString,
}
var (
headerBytes []byte
err error
)
if headerBytes, err = decode(segments[0]); err != nil {
return t, ErrTokenMalformed
}
if err = json.Unmarshal(headerBytes, &t.header); err != nil {
return t, ErrTokenMalformed
}
var claimBytes []byte
if claimBytes, err = decode(segments[1]); err != nil {
return t, ErrTokenMalformed
}
if err = json.Unmarshal(claimBytes, &t.claims); err != nil {
return t, ErrTokenMalformed
}
var errs ValidationError
// check sig
if err = alg.Verify(strings.Join(segments[0:2], "."), segments[2], key); err != nil {
errs |= BadSignatureError
}
// check exp
now := TimeFunc().Unix()
if exp, ok := t.claims["exp"].(float64); ok {
if now > int64(exp) {
errs |= ExpiredError
}
}
if nbf, ok := t.claims["nbf"].(float64); ok {
if now < int64(nbf) {
errs |= NotYetValidError
}
}
if errs == 0 {
return t, nil
}
return t, errs
}
func (t *token) Claim(claim string) interface{} {
return t.claims[claim]
}
func (t *token) SetClaim(claim string, v interface{}) {
t.claims[claim] = v
}
func (t *token) Encode(key interface{}) (payload string, err error) {
var sig string
if payload, err = t.payload(); err != nil {
return
}
if sig, err = t.alg.Sign(payload, key); err != nil {
return
}
payload += "." + sig
return
}
func (t *token) payload() (payload string, err error) {
var jsonValue []byte
// lets do the header
if jsonValue, err = json.Marshal(t.header); err != nil {
return
}
payload = encode(jsonValue)
if jsonValue, err = json.Marshal(t.claims); err != nil {
return
}
payload += "." + encode(jsonValue)
return
}