-
Notifications
You must be signed in to change notification settings - Fork 39
/
jwt.go
319 lines (281 loc) · 8.71 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
package jwt
import (
"bytes"
"fmt"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
jwt "github.com/dgrijalva/jwt-go"
)
type TokenSource interface {
// If the returned string is empty, the token was not found.
// So far any implementation does not return errors.
ExtractToken(r *http.Request) string
}
// Extracts a token from the Authorization header in the form `Bearer <JWT Token>`
type HeaderTokenSource struct {
HeaderName string
}
func (hts *HeaderTokenSource) ExtractToken(r *http.Request) string {
jwtHeader := strings.Split(r.Header.Get("Authorization"), " ")
if jwtHeader[0] == hts.HeaderName && len(jwtHeader) == 2 {
return jwtHeader[1]
}
return ""
}
// Extracts a token from a cookie named `CookieName`.
type CookieTokenSource struct {
CookieName string
}
func (cts *CookieTokenSource) ExtractToken(r *http.Request) string {
jwtCookie, err := r.Cookie(cts.CookieName)
if err == nil {
return jwtCookie.Value
}
return ""
}
// Extracts a token from a URL query parameter of the form https://example.com?ParamName=<JWT token>
type QueryTokenSource struct {
ParamName string
}
func (qts *QueryTokenSource) ExtractToken(r *http.Request) string {
jwtQuery := r.URL.Query().Get(qts.ParamName)
if jwtQuery != "" {
return jwtQuery
}
return ""
}
var (
// Default TokenSources to be applied in the given order if the
// user did not explicitly configure them via the token_source option
DefaultTokenSources = []TokenSource{
&HeaderTokenSource{
HeaderName: "Bearer",
},
&CookieTokenSource{
CookieName: "jwt_token",
},
&QueryTokenSource{
ParamName: "token",
},
}
)
func (h Auth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// if the request path is any of the configured paths, validate JWT
for _, p := range h.Rules {
// TODO: this is a hack to work our CVE in Caddy dealing with parsing
// malformed URLs. Can be removed once upstream fix for path match
if r.URL.EscapedPath() == "" {
return handleUnauthorized(w, r, p, h.Realm), nil
}
cleanedPath := path.Clean(r.URL.Path)
if !httpserver.Path(cleanedPath).Matches(p.Path) {
continue
}
if r.Method == "OPTIONS" {
continue
}
// strip potentially spoofed claims
for header := range r.Header {
if strings.HasPrefix(header, "Token-Claim-") {
r.Header.Del(header)
}
}
// Check excepted paths for this rule and allow access without validating any token
var isExceptedPath bool
for _, e := range p.ExceptedPaths {
if httpserver.Path(r.URL.Path).Matches(e) {
isExceptedPath = true
}
}
if isExceptedPath {
continue
}
if r.URL.Path == "/" && p.AllowRoot {
// special case for protecting children of the root path, only allow access to base directory with directive `allowbase`
continue
}
// Path matches, look for unvalidated token
uToken, err := ExtractToken(p.TokenSources, r)
if err != nil {
if p.Passthrough {
continue
}
return handleUnauthorized(w, r, p, h.Realm), nil
}
var vToken *jwt.Token
if len(p.KeyBackends) <= 0 {
vToken, err = ValidateToken(uToken, &NoopKeyBackend{})
}
// Loop through all possible key files on disk, using cache
for _, keyBackend := range p.KeyBackends {
// Validate token
vToken, err = ValidateToken(uToken, keyBackend)
if err == nil {
// break on first correctly validated token
break
}
}
// Check last error of validating token. If error still exists, no keyfiles matched
if err != nil || vToken == nil {
if p.Passthrough {
continue
}
return handleUnauthorized(w, r, p, h.Realm), nil
}
vClaims, err := Flatten(vToken.Claims.(jwt.MapClaims), "", DotStyle)
if err != nil {
return handleUnauthorized(w, r, p, h.Realm), nil
}
// If token contains rules with allow or deny, evaluate
if len(p.AccessRules) > 0 {
var isAuthorized []bool
for _, rule := range p.AccessRules {
v := vClaims[rule.Claim]
ruleMatches := contains(v, rule.Value) || v == rule.Value
switch rule.Authorize {
case ALLOW:
isAuthorized = append(isAuthorized, ruleMatches)
case DENY:
isAuthorized = append(isAuthorized, !ruleMatches)
default:
return handleUnauthorized(w, r, p, h.Realm), fmt.Errorf("unknown rule type")
}
}
// test all flags, if any are true then ok to pass
ok := false
for _, result := range isAuthorized {
if result {
ok = true
}
}
if !ok {
return handleForbidden(w, r, p, h.Realm), nil
}
}
// set claims as separate headers for downstream to consume
for claim, value := range vClaims {
var headerName string
switch p.StripHeader {
case true:
stripped := strings.SplitAfter(claim, "/")
finalStrip := stripped[len(stripped)-1]
headerName = "Token-Claim-" + modTitleCase(finalStrip)
default:
escaped := url.PathEscape(claim)
headerName = "Token-Claim-" + modTitleCase(escaped)
}
switch v := value.(type) {
case string:
r.Header.Set(headerName, v)
case int64:
r.Header.Set(headerName, strconv.FormatInt(v, 10))
case bool:
r.Header.Set(headerName, strconv.FormatBool(v))
case int32:
r.Header.Set(headerName, strconv.FormatInt(int64(v), 10))
case float32:
r.Header.Set(headerName, strconv.FormatFloat(float64(v), 'f', -1, 32))
case float64:
r.Header.Set(headerName, strconv.FormatFloat(v, 'f', -1, 64))
case []interface{}:
b := bytes.NewBufferString("")
for i, item := range v {
if i > 0 {
b.WriteString(",")
}
b.WriteString(fmt.Sprintf("%v", item))
}
r.Header.Set(headerName, b.String())
default:
// ignore, because, JWT spec says in https://tools.ietf.org/html/rfc7519#section-4
// all claims that are not understood
// by implementations MUST be ignored.
}
}
return h.Next.ServeHTTP(w, r)
}
// pass request if no paths protected with JWT
return h.Next.ServeHTTP(w, r)
}
// ExtractToken will find a JWT token in the token sources specified.
// If tss is empty, the DefaultTokenSources are used.
func ExtractToken(tss []TokenSource, r *http.Request) (string, error) {
effectiveTss := tss
if len(effectiveTss) == 0 {
// Defaults are applied here as this keeps the tests the cleanest.
effectiveTss = DefaultTokenSources
}
for _, tss := range effectiveTss {
token := tss.ExtractToken(r)
if token != "" {
return token, nil
}
}
return "", fmt.Errorf("no token found")
}
// ValidateToken will return a parsed token if it passes validation, or an
// error if any part of the token fails validation. Possible errors include
// malformed tokens, unknown/unspecified signing algorithms, missing secret key,
// tokens that are not valid yet (i.e., 'nbf' field), tokens that are expired,
// and tokens that fail signature verification (forged)
func ValidateToken(uToken string, keyBackend KeyBackend) (*jwt.Token, error) {
if len(uToken) == 0 {
return nil, fmt.Errorf("Token length is zero")
}
token, err := jwt.Parse(uToken, keyBackend.ProvideKey)
if err != nil {
return nil, err
}
return token, nil
}
// handleUnauthorized checks, which action should be performed if access was denied.
// It returns the status code and writes the Location header in case of a redirect.
// Possible caddy variables in the location value will be substituted.
func handleUnauthorized(w http.ResponseWriter, r *http.Request, rule Rule, realm string) int {
if rule.Redirect != "" {
replacer := httpserver.NewReplacer(r, nil, "")
http.Redirect(w, r, replacer.Replace(rule.Redirect), http.StatusSeeOther)
return http.StatusSeeOther
}
w.Header().Add("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"%s\",error=\"invalid_token\"", realm))
return http.StatusUnauthorized
}
// handleForbidden checks, which action should be performed if access was denied.
// It returns the status code and writes the Location header in case of a redirect.
// Possible caddy variables in the location value will be substituted.
func handleForbidden(w http.ResponseWriter, r *http.Request, rule Rule, realm string) int {
if rule.Redirect != "" {
replacer := httpserver.NewReplacer(r, nil, "")
http.Redirect(w, r, replacer.Replace(rule.Redirect), http.StatusSeeOther)
return http.StatusSeeOther
}
w.Header().Add("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"%s\",error=\"insufficient_scope\"", realm))
return http.StatusForbidden
}
// contains checks weather list is a slice ans containts the
// supplied string value.
func contains(list interface{}, value string) bool {
switch l := list.(type) {
case []interface{}:
for _, v := range l {
if v == value {
return true
}
}
}
return false
}
func modTitleCase(s string) string {
switch {
case len(s) == 0:
return s
case len(s) == 1:
return strings.ToUpper(s)
default:
return strings.ToUpper(string(s[0])) + s[1:]
}
}