-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
140 lines (106 loc) · 2.83 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
package jwt
import (
"bytes"
"crypto/hmac"
"encoding/base64"
"time"
)
var dot = []byte(".")
func New(key []byte, algo *Algorithm, claims *Claims) ([]byte, error) {
var message []byte
header := acquireHeader()
defer releaseHeader(header)
header.Algorithm = algo.Name
encodedHeader, err := header.Encode()
if err != nil {
return nil, err
}
message = append(message, encodedHeader...)
encodedClaims, err := claims.Encode()
if err != nil {
return nil, err
}
message = append(message, dot...)
message = append(message, encodedClaims...)
mac := hmac.New(algo.Hash, key)
_, err = mac.Write(message)
if err != nil {
return nil, err
}
message = append(message, dot...)
sign := mac.Sum(nil)
encodedSign := base64.RawURLEncoding.EncodeToString(sign)
return append(message, encodedSign...), nil
}
func ParseAndValidate(token, key []byte, algo *Algorithm, claims *Claims) error {
header := acquireHeader()
defer releaseHeader(header)
sign, message, err := Parse(token, header, claims)
if err != nil {
return err
}
if header.Algorithm != algo.Name {
return ErrWrongAlgorithm
}
err = Verify(algo, message, sign, key)
if err != nil {
return err
}
if claims.ExpiresAt < time.Now().Unix() {
return ErrExpiredToken
}
return nil
}
func Verify(algo *Algorithm, message, sign, key []byte) error {
mac := hmac.New(algo.Hash, key)
_, err := mac.Write(message)
if err != nil {
return ErrInvalidSign
}
expected := mac.Sum(nil)
decodedSign := make([]byte, base64.RawURLEncoding.DecodedLen(len(sign)))
_, err = base64.RawURLEncoding.Decode(decodedSign, sign)
if err != nil {
return ErrInvalidSign
}
result := hmac.Equal(expected, decodedSign)
if !result {
return ErrInvalidSign
}
return nil
}
func Parse(token []byte, header *Header, claims *Claims) ([]byte, []byte, error) {
splitted := bytes.Split(token, dot)
if len(splitted) != 3 {
return nil, nil, ErrMalformedToken
}
err := header.Decode(splitted[0])
if err != nil {
return nil, nil, ErrMalformedToken
}
err = claims.Decode(splitted[1])
if err != nil {
return nil, nil, ErrMalformedToken
}
dotted := append(splitted[0], dot...)
dotted = append(dotted, splitted[1]...)
return splitted[2], dotted, nil
}
func NewHS256(key []byte, claims *Claims) ([]byte, error) {
return New(key, hs256Algo, claims)
}
func ParseHS256(key, token []byte, claims *Claims) error {
return ParseAndValidate(token, key, hs256Algo, claims)
}
func NewHS512(key []byte, claims *Claims) ([]byte, error) {
return New(key, hs512Algo, claims)
}
func ParseHS512(key, token []byte, claims *Claims) error {
return ParseAndValidate(token, key, hs512Algo, claims)
}
func NewHS384(key []byte, claims *Claims) ([]byte, error) {
return New(key, hs384Algo, claims)
}
func ParseHS384(key, token []byte, claims *Claims) error {
return ParseAndValidate(token, key, hs384Algo, claims)
}