forked from go-chi/jwtauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwtauth.go
275 lines (236 loc) · 6.92 KB
/
jwtauth.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package jwtauth
import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
var (
ErrUnauthorized = errors.New("jwtauth: unauthorized token")
ErrExpired = errors.New("jwtauth: expired token")
)
type JwtAuth struct {
signKey []byte
verifyKey []byte
signer jwt.SigningMethod
parser *jwt.Parser
}
// New creates a JwtAuth authenticator instance that provides middleware handlers
// and encoding/decoding functions for JWT signing.
func New(alg string, signKey []byte, verifyKey []byte) *JwtAuth {
return &JwtAuth{
signKey: signKey,
verifyKey: verifyKey,
signer: jwt.GetSigningMethod(alg),
}
}
// NewWithParser is the same as New, except it supports custom parser settings
// introduced in ver. 2.4.0 of jwt-go
func NewWithParser(alg string, parser *jwt.Parser, signKey []byte, verifyKey []byte) *JwtAuth {
return &JwtAuth{
signKey: signKey,
verifyKey: verifyKey,
signer: jwt.GetSigningMethod(alg),
parser: parser,
}
}
// Verifier middleware will verify a JWT passed by a client request.
// The Verifier will look for a JWT token from:
// 1. 'jwt' URI query parameter
// 2. 'Authorization: BEARER T' request header
// 3. Cookie 'jwt' value
//
// The verification processes finishes here and sets the token and
// a error in the request context and calls the next handler.
//
// Make sure to have your own handler following the Validator that
// will check the value of the "jwt" and "jwt.err" in the context
// and respond to the client accordingly. A generic Authenticator
// middleware is provided by this package, that will return a 401
// message for all unverified tokens, see jwtauth.Authenticator.
func (ja *JwtAuth) Verifier(next http.Handler) http.Handler {
return ja.Verify("")(next)
}
func (ja *JwtAuth) Verify(paramAliases ...string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
hfn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var tokenStr string
var err error
// Get token from query params
tokenStr = r.URL.Query().Get("jwt")
// Get token from other query param aliases
if tokenStr == "" && paramAliases != nil && len(paramAliases) > 0 {
for _, p := range paramAliases {
tokenStr = r.URL.Query().Get(p)
if tokenStr != "" {
break
}
}
}
// Get token from authorization header
if tokenStr == "" {
bearer := r.Header.Get("Authorization")
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
tokenStr = bearer[7:]
}
}
// Get token from cookie
if tokenStr == "" {
cookie, err := r.Cookie("jwt")
if err == nil {
tokenStr = cookie.Value
}
}
// Token is required, cya
if tokenStr == "" {
err = ErrUnauthorized
}
// Verify the token
token, err := ja.Decode(tokenStr)
if err != nil {
switch err.Error() {
case "token is expired":
err = ErrExpired
}
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
if token == nil || !token.Valid || token.Method != ja.signer {
err = ErrUnauthorized
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
// Check expiry via "exp" claim
err = token.Claims.Valid()
// Check expiry via "exp" claim
if err != nil {
err = ErrExpired
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
// Valid! pass it down the context to an authenticator middleware
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(hfn)
}
}
func (ja *JwtAuth) SetContext(ctx context.Context, t *jwt.Token, err error) context.Context {
ctx = context.WithValue(ctx, "jwt", t)
ctx = context.WithValue(ctx, "jwt.err", err)
return ctx
}
func (ja *JwtAuth) Encode(claims Claims) (t *jwt.Token, tokenString string, err error) {
t = jwt.New(ja.signer)
t.Claims = toMapClaims(claims)
tokenString, err = t.SignedString(ja.signKey)
t.Raw = tokenString
return
}
func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
if ja.parser != nil {
return ja.parser.Parse(tokenString, ja.keyFunc)
}
return jwt.Parse(tokenString, ja.keyFunc)
}
func (ja *JwtAuth) keyFunc(t *jwt.Token) (interface{}, error) {
if ja.verifyKey != nil && len(ja.verifyKey) > 0 {
return ja.verifyKey, nil
} else {
return ja.signKey, nil
}
}
func (ja *JwtAuth) IsExpired(t *jwt.Token) bool {
if expv, ok := t.Claims.(jwt.MapClaims)["exp"]; ok {
var exp int64
switch v := expv.(type) {
case float64:
exp = int64(v)
case int64:
exp = v
case json.Number:
exp, _ = v.Int64()
default:
}
if exp < EpochNow() {
return true
}
}
return false
}
// Authenticator is a default authentication middleware to enforce access following
// the Verifier middleware. The Authenticator sends a 401 Unauthorized response for
// all unverified tokens and passes the good ones through. It's just fine until you
// decide to write something similar and customize your client response.
func Authenticator(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if jwtErr, ok := ctx.Value("jwt.err").(error); ok {
if jwtErr != nil {
http.Error(w, http.StatusText(401), 401)
return
}
}
jwtToken, ok := ctx.Value("jwt").(*jwt.Token)
if !ok || jwtToken == nil || !jwtToken.Valid {
http.Error(w, http.StatusText(401), 401)
return
}
// Token is authenticated, pass it through
next.ServeHTTP(w, r)
})
}
// Claims is a convenience type to manage a JWT claims hash.
type Claims map[string]interface{}
func (c Claims) Set(k string, v interface{}) Claims {
c[k] = v
return c
}
func (c Claims) Get(k string) (interface{}, bool) {
v, ok := c[k]
return v, ok
}
// Set issued at ("iat") to specified time in the claims
func (c Claims) SetIssuedAt(tm time.Time) Claims {
c["iat"] = tm.UTC().Unix()
return c
}
// Set issued at ("iat") to present time in the claims
func (c Claims) SetIssuedNow() Claims {
c["iat"] = EpochNow()
return c
}
// Set expiry ("exp") in the claims and return itself so it can be chained
func (c Claims) SetExpiry(tm time.Time) Claims {
c["exp"] = tm.UTC().Unix()
return c
}
// Set expiry ("exp") in the claims to some duration from the present time
// and return itself so it can be chained
func (c Claims) SetExpiryIn(tm time.Duration) Claims {
c["exp"] = ExpireIn(tm)
return c
}
// Helper function that returns the NumericDate time value used by the spec
func EpochNow() int64 {
return time.Now().UTC().Unix()
}
// Helper function to return calculated time in the future for "exp" claim.
func ExpireIn(tm time.Duration) int64 {
return EpochNow() + int64(tm.Seconds())
}
// Helper function to build jwt.MapClaims from convenience type Claims.
func toMapClaims(claims Claims) jwt.MapClaims {
c := jwt.MapClaims{}
for k, v := range claims {
c[k] = v
}
return c
}