-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathoption.go
68 lines (60 loc) · 1.69 KB
/
option.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
package jwtmiddleware
import (
"net/http"
)
// Option is how options for the JWTMiddleware are set up.
type Option func(*JWTMiddleware)
// WithCredentialsOptional sets up if credentials are
// optional or not. If set to true then an empty token
// will be considered valid.
//
// Default value: false.
func WithCredentialsOptional(value bool) Option {
return func(m *JWTMiddleware) {
m.credentialsOptional = value
}
}
// WithValidateOnOptions sets up if OPTIONS requests
// should have their JWT validated or not.
//
// Default value: true.
func WithValidateOnOptions(value bool) Option {
return func(m *JWTMiddleware) {
m.validateOnOptions = value
}
}
// WithErrorHandler sets the handler which is called
// when we encounter errors in the JWTMiddleware.
// See the ErrorHandler type for more information.
//
// Default value: DefaultErrorHandler.
func WithErrorHandler(h ErrorHandler) Option {
return func(m *JWTMiddleware) {
m.errorHandler = h
}
}
// WithTokenExtractor sets up the function which extracts
// the JWT to be validated from the request.
//
// Default value: AuthHeaderTokenExtractor.
func WithTokenExtractor(e TokenExtractor) Option {
return func(m *JWTMiddleware) {
m.tokenExtractor = e
}
}
// WithExclusionUrls allows configuring the exclusion URL handler with multiple URLs
// that should be excluded from JWT validation.
func WithExclusionUrls(exclusions []string) Option {
return func(m *JWTMiddleware) {
m.exclusionUrlHandler = func(r *http.Request) bool {
requestFullURL := r.URL.String()
requestPath := r.URL.Path
for _, exclusion := range exclusions {
if requestFullURL == exclusion || requestPath == exclusion {
return true
}
}
return false
}
}
}