-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjwt.go
139 lines (118 loc) · 2.84 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
// jwt.go
// JSON Web Tokens for Go
package jwt
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"hash"
)
var separator []byte = []byte{'.'}
var (
SecretError = errors.New("Signature verification failed")
)
func base64url_encode(b []byte) []byte {
encoded := []byte(base64.URLEncoding.EncodeToString(b))
var equalIndex = bytes.Index(encoded, []byte{'='})
if equalIndex > -1 {
encoded = encoded[:equalIndex]
}
return encoded
}
func base64url_decode(b []byte) ([]byte, error) {
if len(b)%4 != 0 {
b = append(b, bytes.Repeat([]byte{'='}, 4-(len(b)%4))...)
}
decoded, err := base64.URLEncoding.DecodeString(string(b))
if err != nil {
return nil, err
}
return decoded, nil
}
func getHash(algorithm string) (func() hash.Hash, error) {
switch algorithm {
case "HS256":
return sha256.New, nil
case "HS384":
return sha512.New384, nil
case "HS512":
return sha512.New, nil
}
return nil, errors.New("Algorithm not supported")
}
func Encode(jwt interface{}, key []byte, algorithm string) ([]byte, error) {
shaFunc, err := getHash(algorithm)
if err != nil {
return []byte{}, err
}
sha := hmac.New(shaFunc, key)
segments := [3][]byte{}
header, err := json.Marshal(
map[string]interface{}{
"typ": "JWT",
"alg": algorithm,
})
if err != nil {
return []byte{}, err
}
segments[0] = base64url_encode(header)
claims, err := json.Marshal(jwt)
if err != nil {
return []byte{}, err
}
segments[1] = base64url_encode(claims)
sha.Write(bytes.Join(segments[:2], separator))
segments[2] = base64url_encode(sha.Sum(nil))
return bytes.Join(segments[:], separator), nil
}
func Decode(encoded []byte, claims interface{}, key []byte) error {
segments := bytes.Split(encoded, separator)
// segments is currently slices make copies so functions like
// base64url_decode will not overwrite later portions
for k, v := range segments {
newBytes := make([]byte, len(v))
copy(newBytes, v)
segments[k] = newBytes
}
if len(segments) != 3 {
return errors.New("Incorrect segment count")
}
var header map[string]interface{} = make(map[string]interface{})
headerBase64, err := base64url_decode(segments[0])
if err != nil {
return err
}
err = json.Unmarshal(headerBase64, &header)
if err != nil {
return err
}
algorithm, ok := header["alg"].(string)
var sha hash.Hash
if ok {
shaFunc, err := getHash(algorithm)
if err != nil {
return err
}
sha = hmac.New(shaFunc, key)
} else {
return errors.New("Algorithm not supported")
}
claimsBase64, err := base64url_decode(segments[1])
if err != nil {
return err
}
err = json.Unmarshal(claimsBase64, claims)
if err != nil {
return err
}
sha.Write(bytes.Join(segments[:2], separator))
signature := base64url_encode(sha.Sum(nil))
if bytes.Compare(signature, segments[2]) != 0 {
return SecretError
}
return nil
}