-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
385 lines (313 loc) · 10.2 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package jwt
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
)
var errTokenParse = errors.New("failed parsing token")
type TokenSource int
const (
Unset TokenSource = iota
Cookie
Header
)
func (s TokenSource) String() string {
return [...]string{"unset", "cookie", "header"}[s]
}
type Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Key defines the RSA key used to verify tokens.
// Required.
Key any
// ExemptRoutes defines routes and methods that don't require tokens.
// Optional. Defaults to /login [POST].
ExemptRoutes map[string][]string
// ExemptMethods defines methods that don't require tokens.
// Optional. Defaults to [OPTIONS].
ExemptMethods []string
// OptionalRoutes defines routes and methods that
// can optionally require a token.
// Optional.
OptionalRoutes map[string][]string
// ParseTokenFunc defines a function used to decode tokens.
// Optional.
ParseTokenFunc func(string, []jwt.ParseOption) (jwt.Token, error)
// AfterParseFunc defines a function that will run after
// the ParseTokenFunc has successfully run.
// Optional.
AfterParseFunc func(echo.Context, jwt.Token, string, TokenSource) *echo.HTTPError
// Options defines jwt.ParseOption options for parsing tokens.
// Optional. Defaults [jwt.WithValidate(true)].
Options []jwt.ParseOption
// ContextKey defines the key that will be used to store the token
// on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "token".
ContextKey string
// CookieKey defines the key that will be used to read the token
// from an HTTP cookie.
// Optional. Defaults to "access_token".
CookieKey string
// AuthHeader defines the HTTP header that will be used to
// read the token from.
// Optional. Defaults to "Authorization".
AuthHeader string
// AuthScheme defines the authorization scheme in the AuthHeader.
// Optional. Defaults to "Bearer".
AuthScheme string
// UseRefreshToken controls whether refresh tokens are used or not.
// Optional. Defaults to false.
UseRefreshToken bool
// RefreshToken holds the configuration related to refresh tokens.
// Optional.
RefreshToken *RefreshToken
}
type RefreshToken struct {
// ContextKey defines the key that will be used to store the refresh token
// on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token".
ContextKey string
// ContextKeyEncoded defines the key that will be used to store the encoded
// refresh token on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token_encoded".
ContextKeyEncoded string
// CookieKey defines the key that will be used to read the refresh token
// from an HTTP cookie.
// Optional. Defaults to "refresh_token".
CookieKey string
// BodyMIMEType defines the expected MIME type of the request body.
// Returns a 400 Bad Request if the request's Content-Type header does not match.
// Optional. Defaults to "application/json".
BodyMIMEType string
// BodyKey defines the key that will be used to read the refresh token
// from the request's body.
// Returns a 422 UnprocessableEntity if the request's body key is missing.
// Optional. Defaults to "refresh_token".
BodyKey string
// Routes defines routes and methods that require a refresh token.
// Optional. Defaults to /auth/refresh [POST] and /auth/logout [POST].
Routes map[string][]string
}
var DefaultConfig = Config{
Skipper: middleware.DefaultSkipper,
ExemptRoutes: map[string][]string{"/login": {http.MethodPost}},
ExemptMethods: []string{http.MethodOptions},
OptionalRoutes: map[string][]string{},
ParseTokenFunc: parseToken,
Options: []jwt.ParseOption{jwt.WithValidate(true)},
ContextKey: "token",
CookieKey: "access_token",
AuthHeader: "Authorization",
AuthScheme: "Bearer",
UseRefreshToken: false,
RefreshToken: &RefreshToken{
ContextKey: "refresh_token",
ContextKeyEncoded: "refresh_token_encoded",
CookieKey: "refresh_token",
BodyMIMEType: echo.MIMEApplicationJSON,
BodyKey: "refresh_token",
Routes: map[string][]string{
"/auth/refresh": {http.MethodPost},
"/auth/logout": {http.MethodPost},
},
},
}
func JWT(key any) echo.MiddlewareFunc {
c := DefaultConfig
c.ExemptRoutes = DefaultConfig.ExemptRoutes
c.ExemptMethods = DefaultConfig.ExemptMethods
c.Key = key
c.Options = append(c.Options, jwt.WithKey(jwa.RS256, key))
return JWTWithConfig(c)
}
func JWTWithConfig(config Config) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = DefaultConfig.Skipper
}
if config.Key == nil {
panic("key is required")
}
if config.ParseTokenFunc == nil {
config.ParseTokenFunc = DefaultConfig.ParseTokenFunc
}
if len(config.Options) < 1 {
config.Options = DefaultConfig.Options
config.Options = append(config.Options, jwt.WithKey(jwa.RS256, config.Key))
}
if config.ContextKey == "" {
config.ContextKey = DefaultConfig.ContextKey
}
if config.CookieKey == "" {
config.CookieKey = DefaultConfig.CookieKey
}
if config.AuthHeader == "" {
config.AuthHeader = DefaultConfig.AuthHeader
}
if config.AuthScheme == "" {
config.AuthScheme = DefaultConfig.AuthScheme
}
if config.RefreshToken == nil {
config.RefreshToken = DefaultConfig.RefreshToken
}
if config.RefreshToken.ContextKey == "" {
config.RefreshToken.ContextKey = DefaultConfig.RefreshToken.ContextKey
}
if config.RefreshToken.ContextKeyEncoded == "" {
config.RefreshToken.ContextKeyEncoded = DefaultConfig.RefreshToken.ContextKeyEncoded
}
if config.RefreshToken.CookieKey == "" {
config.RefreshToken.CookieKey = DefaultConfig.RefreshToken.CookieKey
}
if config.RefreshToken.BodyMIMEType == "" {
config.RefreshToken.BodyMIMEType = DefaultConfig.RefreshToken.BodyMIMEType
}
if config.RefreshToken.BodyKey == "" {
config.RefreshToken.BodyKey = DefaultConfig.RefreshToken.BodyKey
}
if len(config.RefreshToken.Routes) < 1 {
config.RefreshToken.Routes = DefaultConfig.RefreshToken.Routes
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
path := c.Path()
method := c.Request().Method
if check(path, method, config.ExemptRoutes) {
return next(c)
}
for _, i := range config.ExemptMethods {
if i == c.Request().Method {
return next(c)
}
}
var tokenSource TokenSource
var encodedToken string
var refreshRoute bool
if config.UseRefreshToken && check(path, method, config.RefreshToken.Routes) {
refreshRoute = true
encodedToken = encodedTokenFromCookie(c, config.RefreshToken.CookieKey)
if encodedToken == "" {
if c.Request().Header.Get("Content-Type") != config.RefreshToken.BodyMIMEType {
return echo.NewHTTPError(ErrRequestMalformedStatus, ErrRequestMalformed)
}
// there's always a body, so we don't
// need to handle the error.
data, _ := io.ReadAll(c.Request().Body)
var m map[string]any
err := json.Unmarshal(data, &m)
if err != nil {
return echo.NewHTTPError(ErrRequestMalformedStatus, ErrRequestMalformed)
}
key := config.RefreshToken.BodyKey
if val, ok := m[key]; !ok {
return echo.NewHTTPError(ErrBodyMissingKeyStatus, ErrBodyMissingKey)
} else {
encodedToken = val.(string)
}
c.Request().Body = io.NopCloser(bytes.NewReader(data))
}
} else {
encodedToken = encodedTokenFromCookie(c, config.CookieKey)
tokenSource = Cookie
if encodedToken == "" {
tokenSource = Header
header := c.Request().Header.Get(config.AuthHeader)
if header != "" {
split := strings.Split(header, " ")
if strings.ToLower(split[0]) != strings.ToLower(config.AuthScheme) {
return echo.NewHTTPError(ErrAuthorizationSchemeStatus, ErrAuthorizationScheme)
}
if len(split) < 2 {
return echo.NewHTTPError(ErrAuthorizationHeaderStatus, ErrAuthorizationHeader)
}
encodedToken = split[1]
}
}
}
token, err := config.ParseTokenFunc(encodedToken, config.Options)
if err != nil {
if check(path, method, config.OptionalRoutes) {
return next(c)
}
var routeExists, methodMatches bool
for _, route := range c.Echo().Routes() {
if path == route.Path {
if method == route.Method {
methodMatches = true
}
routeExists = true
}
}
if routeExists && !methodMatches {
return echo.NewHTTPError(ErrMethodNotAllowedStatus, ErrMethodNotAllowed)
}
if !routeExists {
return echo.NewHTTPError(ErrRouteNotFoundStatus, ErrRouteNotFound)
}
if !errors.Is(err, errTokenParse) {
return err
} else {
return echo.NewHTTPError(ErrTokenInvalidStatus, ErrTokenInvalid)
}
}
if !refreshRoute {
c.Set(config.ContextKey, token)
} else {
c.Set(config.RefreshToken.ContextKey, token)
c.Set(config.RefreshToken.ContextKeyEncoded, encodedToken)
}
if config.AfterParseFunc != nil {
err := config.AfterParseFunc(c, token, encodedToken, tokenSource)
if err != nil {
return err
}
}
return next(c)
}
}
}
func encodedTokenFromCookie(c echo.Context, key string) string {
var encodedToken string
cookie, err := c.Request().Cookie(key)
if err == nil {
encodedToken = cookie.Value
}
return encodedToken
}
func parseToken(encodedToken string, options []jwt.ParseOption) (jwt.Token, error) {
token, err := jwt.Parse([]byte(encodedToken), options...)
if err != nil {
if errors.Is(err, jwt.ErrTokenExpired()) {
return nil, echo.NewHTTPError(ErrTokenExpiredStatus, ErrTokenExpired)
}
if errors.Is(err, jwt.ErrInvalidIssuedAt()) {
return nil, echo.NewHTTPError(ErrTokenInvalidIssuedAtStatus, ErrTokenInvalidIssuedAt)
}
if errors.Is(err, jwt.ErrTokenNotYetValid()) {
return nil, echo.NewHTTPError(ErrTokenNotYetValidStatus, ErrTokenNotYetValid)
}
return nil, errTokenParse
}
return token, nil
}
func check(path string, method string, m map[string][]string) bool {
for k, v := range m {
if k == path {
for _, i := range v {
if "*" == i || method == i {
return true
}
}
}
}
return false
}