forked from pascaldekloe/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.go
More file actions
231 lines (199 loc) · 6.84 KB
/
web.go
File metadata and controls
231 lines (199 loc) · 6.84 KB
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
package jwt
import (
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"errors"
"net/http"
"strconv"
"strings"
"time"
)
// MIMEType is the IANA registered media type.
const MIMEType = "application/jwt"
// OAuthURN is the IANA registered OAuth URI.
const OAuthURN = "urn:ietf:params:oauth:token-type:jwt"
// ErrNoHeader signals an HTTP request without authorization.
var ErrNoHeader = errors.New("jwt: no HTTP authorization header")
var errAuthSchema = errors.New("jwt: want Bearer schema")
// ECDSACheckHeader applies ECDSACheck on a HTTP request.
// Specifically it looks for a bearer token in the Authorization header.
func ECDSACheckHeader(r *http.Request, key *ecdsa.PublicKey) (*Claims, error) {
token, err := tokenFromHeader(r)
if err != nil {
return nil, err
}
return ECDSACheck(token, key)
}
// EdDSACheckHeader applies EdDSACheck on a HTTP request.
// Specifically it looks for a bearer token in the Authorization header.
func EdDSACheckHeader(r *http.Request, key ed25519.PublicKey) (*Claims, error) {
token, err := tokenFromHeader(r)
if err != nil {
return nil, err
}
return EdDSACheck(token, key)
}
// HMACCheckHeader applies HMACCheck on a HTTP request.
// Specifically it looks for a bearer token in the Authorization header.
func HMACCheckHeader(r *http.Request, secret []byte) (*Claims, error) {
token, err := tokenFromHeader(r)
if err != nil {
return nil, err
}
return HMACCheck(token, secret)
}
// RSACheckHeader applies RSACheck on a HTTP request.
// Specifically it looks for a bearer token in the Authorization header.
func RSACheckHeader(r *http.Request, key *rsa.PublicKey) (*Claims, error) {
token, err := tokenFromHeader(r)
if err != nil {
return nil, err
}
return RSACheck(token, key)
}
// CheckHeader applies KeyRegister.Check on a HTTP request.
// Specifically it looks for a bearer token in the Authorization header.
func (keys *KeyRegister) CheckHeader(r *http.Request) (*Claims, error) {
token, err := tokenFromHeader(r)
if err != nil {
return nil, err
}
return keys.Check(token)
}
func tokenFromHeader(r *http.Request) ([]byte, error) {
h := r.Header["Authorization"]
if h == nil {
return nil, ErrNoHeader
}
// “Multiple message-header fields with the same field-name MAY be
// present in a message if and only if the entire field-value for that
// header field is defined as a comma-separated list.”
// — “Hypertext Transfer Protocol” RFC 2616, subsection 4.2
auth := strings.Join(h, ", ")
const prefix = "Bearer "
if !strings.HasPrefix(auth, prefix) {
return nil, errAuthSchema
}
return []byte(auth[len(prefix):]), nil
}
// ECDSASignHeader applies ECDSASign on a HTTP request.
// Specifically it sets a bearer token in the Authorization header.
func (c *Claims) ECDSASignHeader(r *http.Request, alg string, key *ecdsa.PrivateKey) error {
token, err := c.ECDSASign(alg, key)
if err != nil {
return err
}
r.Header.Set("Authorization", "Bearer "+string(token))
return nil
}
// EdDSASignHeader applies ECDSASign on a HTTP request.
// Specifically it sets a bearer token in the Authorization header.
func (c *Claims) EdDSASignHeader(r *http.Request, key ed25519.PrivateKey) error {
token, err := c.EdDSASign(key)
if err != nil {
return err
}
r.Header.Set("Authorization", "Bearer "+string(token))
return nil
}
// HMACSignHeader applies HMACSign on a HTTP request.
// Specifically it sets a bearer token in the Authorization header.
func (c *Claims) HMACSignHeader(r *http.Request, alg string, secret []byte) error {
token, err := c.HMACSign(alg, secret)
if err != nil {
return err
}
r.Header.Set("Authorization", "Bearer "+string(token))
return nil
}
// RSASignHeader applies RSASign on a HTTP request.
// Specifically it sets a bearer token in the Authorization header.
func (c *Claims) RSASignHeader(r *http.Request, alg string, key *rsa.PrivateKey) error {
token, err := c.RSASign(alg, key)
if err != nil {
return err
}
r.Header.Set("Authorization", "Bearer "+string(token))
return nil
}
// Handler protects an http.Handler with security enforcements.
// Requests are only passed to Target if the JWT checks out.
type Handler struct {
// Target is the secured service.
Target http.Handler
// Keys defines the trusted credentials.
Keys *KeyRegister
// HeaderBinding maps JWT claim names to HTTP header names.
// All requests passed to Target have these headers set. In
// case of failure the request is rejected with status code
// 401 (Unauthorized) and a description.
HeaderBinding map[string]string
// HeaderPrefix is an optional constraint for JWT claim binding.
// Any client headers that match the prefix are removed from the
// request. HeaderBinding entries that don't match the prefix
// are ignored.
HeaderPrefix string
// ContextKey places the validated Claims in the context of
// each respective request passed to Target when set. See
// http.Request.Context and context.Context.Value.
ContextKey interface{}
// When not nil, then Func is called after the JWT validation
// succeeds and before any header bindings. Target is skipped
// [request drop] when the return is false.
// This feature may be used to further customise requests or
// as a filter or as an extended http.HandlerFunc.
Func func(http.ResponseWriter, *http.Request, *Claims) (pass bool)
}
// ServeHTTP honors the http.Handler interface.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// verify claims
claims, err := h.Keys.CheckHeader(r)
if err != nil {
if err == ErrNoHeader {
w.Header().Set("WWW-Authenticate", "Bearer")
} else {
w.Header().Set("WWW-Authenticate", `Bearer error="invalid_token", error_description=`+strconv.QuoteToASCII(err.Error()))
}
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// verify time constraints
if !claims.Valid(time.Now()) {
w.Header().Set("WWW-Authenticate", `Bearer error="invalid_token", error_description="jwt: time constraints exceeded"`)
http.Error(w, "jwt: time constraints exceeded", http.StatusUnauthorized)
return
}
// filter request headers
if h.HeaderPrefix != "" {
for name := range r.Header {
if strings.HasPrefix(name, h.HeaderPrefix) {
delete(r.Header, name)
}
}
}
// apply the custom function when set
if h.Func != nil && !h.Func(w, r, claims) {
return
}
// claim propagation
for claimName, headerName := range h.HeaderBinding {
if !strings.HasPrefix(headerName, h.HeaderPrefix) {
continue // silent ignore
}
s, ok := claims.String(claimName)
if !ok {
msg := "jwt: want string for claim " + claimName
w.Header().Set("WWW-Authenticate", `Bearer error="invalid_token", error_description=`+strconv.QuoteToASCII(msg))
http.Error(w, msg, http.StatusUnauthorized)
return
}
r.Header.Set(headerName, s)
}
// place claims in request context
if h.ContextKey != nil {
r = r.WithContext(context.WithValue(r.Context(), h.ContextKey, claims))
}
h.Target.ServeHTTP(w, r)
}