This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
209 lines (187 loc) · 5.75 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package jwt
import (
"bytes"
"crypto/ecdsa"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
jwt "github.com/golang-jwt/jwt"
)
const (
// AuthorizationHeader is the constant string used to get the Authorization
// headers
AuthorizationHeader = "Authorization"
)
// Claims is a map of string->something containing the meta infos associated with a token
type Claims map[string]interface{}
// CreateToken takes some claims and a private key (either rsa or ec) and returns a signed json web token
func CreateToken(claims Claims, key interface{}) (string, error) {
switch k := key.(type) {
case *rsa.PrivateKey:
{
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims(claims))
return token.SignedString(k)
}
case *ecdsa.PrivateKey:
{
token := jwt.NewWithClaims(jwt.SigningMethodES512, jwt.MapClaims(claims))
return token.SignedString(k)
}
case []byte:
{
token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims(claims))
return token.SignedString(k)
}
}
return "", errors.New("invalid private key")
}
// ValidateToken checks the signature of the token with a given public key and returns the associated claims
func ValidateToken(tokenString string, key interface{}) (Claims, error) {
var (
token *jwt.Token
err error
)
switch k := key.(type) {
case *rsa.PublicKey:
{
token, err = jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return k, nil
})
}
case *ecdsa.PublicKey:
{
token, err = jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return k, nil
})
}
case []byte:
{
token, err = jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return k, nil
})
}
}
if err != nil {
return nil, err
}
if token != nil {
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return Claims(claims), nil
}
}
return nil, errors.New("invalid token")
}
// GetUnvalidatedClaims extracts the token claims without validating the token
func GetUnvalidatedClaims(tokenString string) (claims Claims, err error) {
parts := strings.Split(tokenString, ".")
if len(parts) != 3 {
return nil, errors.New("token contains an invalid number of segments")
}
claimBytes, err := jwt.DecodeSegment(parts[1])
if err != nil {
return nil, err
}
return claims, json.NewDecoder(bytes.NewBuffer(claimBytes)).Decode(&claims)
}
// LoadPublicKey loads a PEM encoded public key (either rsa or ec)
func LoadPublicKey(keyFile string) (interface{}, error) {
bs, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
return ParsePublicKey(bs)
}
// ParsePublicKey parses a pem encoded public key (rsa or ecdsa based)
func ParsePublicKey(data []byte) (interface{}, error) {
rsaKey, err := jwt.ParseRSAPublicKeyFromPEM(data)
if err != nil {
ecKey, err := jwt.ParseECPublicKeyFromPEM(data)
if err != nil {
return nil, errors.New("unknown public key type")
}
return ecKey, nil
}
return rsaKey, nil
}
// LoadPrivateKey loads a PEM encoded private key (either rsa or ec)
func LoadPrivateKey(keyFile string) (interface{}, error) {
bs, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
return ParsePrivateKey(bs)
}
// ParsePrivateKey parses a pem encoded private key (rsa or ecdsa based)
func ParsePrivateKey(data []byte) (interface{}, error) {
rsaKey, err := jwt.ParseRSAPrivateKeyFromPEM(data)
if err != nil {
ecKey, err := jwt.ParseECPrivateKeyFromPEM(data)
if err != nil {
return nil, errors.New("unknown public key type")
}
return ecKey, nil
}
return rsaKey, nil
}
// GetTokenFromRequest takes the first Authorization header or `token` GET pararm , then
// extract the token prefix and json web token
func GetTokenFromRequest(r *http.Request, header string) (prefix string, token string, err error) {
if header == "" {
header = AuthorizationHeader
}
tokenList, ok := r.Header[http.CanonicalHeaderKey(header)]
// pull from GET if not in the headers
if !ok || len(tokenList) < 1 {
tokenList, ok = r.URL.Query()["token"]
prefix = "GET"
}
if len(tokenList) < 1 {
prefix = ""
return prefix, token, errors.New("no valid authorization header")
}
tokenParts := strings.Fields(tokenList[0])
switch len(tokenParts) {
case 1:
token = tokenParts[0]
case 2:
prefix = tokenParts[0]
token = tokenParts[1]
default:
return prefix, token, errors.New("invalid token: unexpected number of parts")
}
return prefix, token, nil
}
// GetClaimsFromRequestWithValidation extracts and validates the token from a request, returning the claims
func GetClaimsFromRequestWithValidation(r *http.Request, header string, key interface{}) (prefix string, claims Claims, err error) {
prefix, token, err := GetTokenFromRequest(r, header)
if err != nil {
return prefix, nil, err
}
claims, err = ValidateToken(token, key)
return prefix, claims, err
}
// GetClaimsFromRequest extracts the token from a request, returning the
// claims without validating the token. This should only be used in situations
// where you can already trust or if you are simply logging the claim
// information.
func GetClaimsFromRequest(r *http.Request, header string) (prefix string, claims Claims, err error) {
prefix, token, err := GetTokenFromRequest(r, header)
if err != nil {
return prefix, nil, err
}
claims, err = GetUnvalidatedClaims(token)
return prefix, claims, err
}