-
Notifications
You must be signed in to change notification settings - Fork 13
/
jwt.go
190 lines (139 loc) · 3.5 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package jwt
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"hash"
"strconv"
"strings"
"time"
)
type JWT struct {
}
func (t *JWT) EncodeJWT(secret string, headers, payload map[string]string) (string, error) {
algo, ok := headers["algo"]
if !ok {
return "", fmt.Errorf("cannot create JWT")
}
hashData := t.createHash(secret, algo)
head, err := json.Marshal(headers)
if err != nil {
return "", err
}
payl, err := json.Marshal(payload)
if err != nil {
return "", err
}
h := base64.URLEncoding.EncodeToString(head)
pl := base64.URLEncoding.EncodeToString(payl)
token := fmt.Sprintf("%s.%s", h, pl)
_, err = hashData.Write([]byte(token))
if err != nil {
return "", err
}
newHash := hashData.Sum(nil)
hs := base64.URLEncoding.EncodeToString(newHash)
return fmt.Sprintf("%s.%s", token, hs), nil
}
func (t *JWT) VerifyJWT(secret, jwt string, expire int64) error {
jwtTokenParts, err := t.extractJWTParts(jwt)
if err != nil {
return err
}
err = t.checkSignature(secret, jwtTokenParts)
if err != nil {
return err
}
payload, err := t.extractPayload(jwtTokenParts[1])
if err != nil {
return err
}
return t.checkTime(payload, expire)
}
func (t *JWT) VerifyJWTAndGetPayload(secret, jwt string, expire int64) (map[string]string, error) {
jwtTokenParts, err := t.extractJWTParts(jwt)
if err != nil {
return nil, err
}
err = t.checkSignature(secret, jwtTokenParts)
if err != nil {
return nil, err
}
payload, err := t.extractPayload(jwtTokenParts[1])
if err != nil {
return nil, err
}
return payload, t.checkTime(payload, expire)
}
func (t *JWT) extractJWTParts(jwt string) ([]string, error) {
jwtToken := strings.Split(jwt, ".")
if len(jwtToken) != 3 {
return nil, fmt.Errorf("token not valid need to be from three parts")
}
return jwtToken, nil
}
func (t *JWT) checkSignature(secret string, jwtToken []string) error {
header := make(map[string]string)
h, err := base64.URLEncoding.DecodeString(jwtToken[0])
if err != nil {
return err
}
err = json.Unmarshal(h, &header)
if err != nil {
return err
}
algo, ok := header["algo"]
if !ok {
return fmt.Errorf("missing algo in header part")
}
jwtType, ok := header["type"]
if !ok || jwtType != "JWT" {
return fmt.Errorf("missing type in header part")
}
mhash := t.createHash(secret, algo)
_, err = mhash.Write([]byte(fmt.Sprintf("%s.%s", jwtToken[0], jwtToken[1])))
if err != nil {
return err
}
jwtTokenValue, _ := base64.URLEncoding.DecodeString(jwtToken[2])
valid := hmac.Equal(mhash.Sum(nil), jwtTokenValue)
if !valid {
return fmt.Errorf("token not valid")
}
return nil
}
func (t *JWT) extractPayload(jwtPayloadPart string) (map[string]string, error) {
payload := make(map[string]string)
payl, err := base64.URLEncoding.DecodeString(jwtPayloadPart)
if err != nil {
return nil, err
}
err = json.Unmarshal(payl, &payload)
if err != nil {
return nil, err
}
return payload, nil
}
func (t *JWT) checkTime(payload map[string]string, expire int64) error {
expireTime, ok := payload["exp"]
if !ok {
return fmt.Errorf("token expire time not valid")
}
expTime, _ := strconv.ParseInt(expireTime, 10, 64)
expTime = time.Now().Unix() - expTime
valid := expTime < expire
if !valid {
return fmt.Errorf("token time not valid %d %d", expTime, expire)
}
return nil
}
func (t *JWT) createHash(secret, algo string) hash.Hash {
switch algo {
case "HS256":
return hmac.New(sha256.New, []byte(secret))
default:
return hmac.New(sha256.New, []byte(secret))
}
}