forked from BTBurke/caddy-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
260 lines (230 loc) · 7.38 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
package jwt
import (
"bytes"
"fmt"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
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(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 passed one of three ways: (1) as the Authorization
// header in the form `Bearer <JWT Token>`; (2) as a cookie named `jwt_token`; (3) as
// a URL query paramter of the form https://example.com?token=<JWT token>
func ExtractToken(r *http.Request) (string, error) {
jwtHeader := strings.Split(r.Header.Get("Authorization"), " ")
if jwtHeader[0] == "Bearer" && len(jwtHeader) == 2 {
return jwtHeader[1], nil
}
jwtCookie, err := r.Cookie("jwt_token")
if err == nil {
return jwtCookie.Value, nil
}
jwtQuery := r.URL.Query().Get("token")
if jwtQuery != "" {
return jwtQuery, 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:]
}
}