-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
344 lines (308 loc) · 11.1 KB
/
parse.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
package dpop
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"math/big"
"net/url"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
)
// HTTPVerb is a convenience for determining the HTTP method of a request.
// This package defines the all available HTTP verbs which can be used when calling the Parse function.
type HTTPVerb string
// HTTP method supported by the package.
const (
GET HTTPVerb = "GET"
POST HTTPVerb = "POST"
PUT HTTPVerb = "PUT"
DELETE HTTPVerb = "DELETE"
PATCH HTTPVerb = "PATCH"
HEAD HTTPVerb = "HEAD"
OPTIONS HTTPVerb = "OPTIONS"
TRACE HTTPVerb = "TRACE"
CONNECT HTTPVerb = "CONNECT"
)
const DEFAULT_ALLOWED_PROOF_AGE = time.Minute * 5
const DEFAULT_ALLOWED_TIME_WINDOW = time.Second * 0
// ParseOptions and its contents are optional for the Parse function.
type ParseOptions struct {
// The expected nonce if the authorization server has issued a nonce.
Nonce string
// Used to control if the `iat` field is within allowed clock-skew.
// If set to true the authorization server has to validate the nonce timestamp itself.
NonceHasTimestamp bool
// The allowed clock-skew (into the future) on the `iat` of the proof. If not set proof is rejected if issued in the future.
TimeWindow *time.Duration
// The allowed age of a proof. If not set the default is 5 minutes.
AllowedProofAge *time.Duration
// dpop_jkt parameter that is optionally sent by the client to the authorization server on token request.
// If set the proof proof-of-possession public key needs to match or the proof is rejected.
JKT string
}
// Parse translates a DPoP proof string into a JWT token and parses it with the jwt package (github.com/golang-jwt/jwt/v5).
// It will also validate the proof according to https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
// but not check whether the proof matches a bound access token. It also assumes point 1 is checked by the calling application.
//
// Protected resources should use the 'Validate' function on the returned proof to ensure that the proof matches any bound access token.
func Parse(
tokenString string,
httpMethod HTTPVerb,
httpURL *url.URL,
opts ParseOptions,
) (*Proof, error) {
// Parse the token string
// Ensure that it is a well-formed JWT, that a supported signature algorithm is used,
// that it contains a public key, and that the signature verifies with the public key.
// This satisfies point 2, 5, 6 and 7 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
claims := ProofTokenClaims{RegisteredClaims: &jwt.RegisteredClaims{}}
dpopToken, err := jwt.ParseWithClaims(tokenString, &claims, keyFunc)
if err != nil {
return nil, errors.Join(ErrInvalidProof, err)
}
// Check that all claims have been populated
// This satisfies point 3 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
if claims.Method == "" || claims.URL == "" || claims.ID == "" || claims.IssuedAt == nil {
return nil, errors.Join(ErrInvalidProof, ErrMissingClaims)
}
// Check `typ` JOSE header that it is correct
// This satisfies point 4 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
typeHeader := dpopToken.Header["typ"]
if typeHeader == nil || typeHeader.(string) != "dpop+jwt" {
return nil, errors.Join(ErrInvalidProof, ErrUnsupportedJWTType)
}
// Strip the incoming URI of query and fragment according to point 9 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
httpURL.RawQuery = ""
httpURL.Fragment = ""
// Check that `htm` and `htu` claims match the HTTP method and URL of the current request.
// This satisfies point 8 and 9 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
if httpMethod != claims.Method || httpURL.String() != claims.URL {
return nil, errors.Join(ErrInvalidProof, ErrIncorrectHTTPTarget)
}
// Check that `nonce` is correct
// This satisfies point 10 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
if opts.Nonce != "" && opts.Nonce != claims.Nonce {
return nil, ErrIncorrectNonce
}
// Check that `iat` is within the acceptable window unless `nonce` contains a server managed timestamp.
// This satisfies point 11 in https://datatracker.ietf.org/doc/html/rfc9449#section-4.3
if !opts.NonceHasTimestamp {
// Check that `iat` is not too far into the past.
past := DEFAULT_ALLOWED_PROOF_AGE
if opts.AllowedProofAge != nil {
past = *opts.AllowedProofAge
}
if claims.IssuedAt.Before(time.Now().Add(-past)) {
return nil, errors.Join(ErrInvalidProof, ErrExpired)
}
// Check that `iat` is not too far into the future.
future := DEFAULT_ALLOWED_TIME_WINDOW
if opts.TimeWindow != nil {
future = *opts.TimeWindow
}
if claims.IssuedAt.After(time.Now().Add(future)) {
return nil, errors.Join(ErrInvalidProof, ErrFuture)
}
}
// Extract the public key from the proof and hash it.
// This is done in order to store the public key
// without the need for extracting and hashing it again.
jwk, ok := dpopToken.Header["jwk"].(map[string]interface{})
if !ok {
// keyFunc used with parseWithClaims should ensure that this can not happen but better safe than sorry.
return nil, ErrMissingJWK
}
jwkJSONbytes, err := getThumbprintableJwkJSONbytes(jwk)
if err != nil {
// keyFunc used with parseWithClaims should ensure that this can not happen but better safe than sorry.
return nil, errors.Join(ErrInvalidProof, err)
}
h := sha256.New()
_, err = h.Write(jwkJSONbytes)
if err != nil {
return nil, errors.Join(ErrInvalidProof, err)
}
b64URLjwkHash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
// Check that `dpop_jkt` is correct if supplied to the authorization server on token request.
// This satisfies https://datatracker.ietf.org/doc/html/rfc9449#name-authorization-code-binding-
if opts.JKT != "" {
if b64URLjwkHash != opts.JKT {
return nil, errors.Join(ErrInvalidProof, ErrIncorrectJKT)
}
}
return &Proof{
Token: dpopToken,
HashedPublicKey: b64URLjwkHash,
}, nil
}
func keyFunc(t *jwt.Token) (interface{}, error) {
// Return the required jwkHeader header. See https://datatracker.ietf.org/doc/html/rfc9449#section-4.2
// Used to validate the signature of the DPoP proof.
jwkHeader := t.Header["jwk"]
if jwkHeader == nil {
return nil, ErrMissingJWK
}
jwkMap, ok := jwkHeader.(map[string]interface{})
if !ok {
return nil, ErrMissingJWK
}
return parseJwk(jwkMap)
}
// Parses a JWK and inherently strips it of optional fields
func parseJwk(jwkMap map[string]interface{}) (interface{}, error) {
// Ensure that JWK kty is present and is a string.
kty, ok := jwkMap["kty"].(string)
if !ok {
return nil, ErrInvalidProof
}
switch kty {
case "EC":
// Ensure that the required fields are present and are strings.
x, ok := jwkMap["x"].(string)
if !ok {
return nil, ErrInvalidProof
}
y, ok := jwkMap["y"].(string)
if !ok {
return nil, ErrInvalidProof
}
crv, ok := jwkMap["crv"].(string)
if !ok {
return nil, ErrInvalidProof
}
// Decode the coordinates from Base64.
//
// According to RFC 7518, they are Base64 URL unsigned integers.
// https://tools.ietf.org/html/rfc7518#section-6.3
xCoordinate, err := base64urlTrailingPadding(x)
if err != nil {
return nil, err
}
yCoordinate, err := base64urlTrailingPadding(y)
if err != nil {
return nil, err
}
// Read the specified curve of the key.
var curve elliptic.Curve
switch crv {
case "P-256":
curve = elliptic.P256()
case "P-384":
curve = elliptic.P384()
case "P-521":
curve = elliptic.P521()
default:
return nil, ErrUnsupportedCurve
}
return &ecdsa.PublicKey{
X: big.NewInt(0).SetBytes(xCoordinate),
Y: big.NewInt(0).SetBytes(yCoordinate),
Curve: curve,
}, nil
case "RSA":
// Ensure that the required fields are present and are strings.
e, ok := jwkMap["e"].(string)
if !ok {
return nil, ErrInvalidProof
}
n, ok := jwkMap["n"].(string)
if !ok {
return nil, ErrInvalidProof
}
// Decode the exponent and modulus from Base64.
//
// According to RFC 7518, they are Base64 URL unsigned integers.
// https://tools.ietf.org/html/rfc7518#section-6.3
exponent, err := base64urlTrailingPadding(e)
if err != nil {
return nil, err
}
modulus, err := base64urlTrailingPadding(n)
if err != nil {
return nil, err
}
return &rsa.PublicKey{
N: big.NewInt(0).SetBytes(modulus),
E: int(big.NewInt(0).SetBytes(exponent).Uint64()),
}, nil
case "OKP":
// Ensure that the required fields are present and are strings.
x, ok := jwkMap["x"].(string)
if !ok {
return nil, ErrInvalidProof
}
publicKey, err := base64urlTrailingPadding(x)
if err != nil {
return nil, err
}
return ed25519.PublicKey(publicKey), nil
case "OCT":
return nil, ErrUnsupportedKeyAlgorithm
default:
return nil, ErrUnsupportedKeyAlgorithm
}
}
// Borrowed from MicahParks/keyfunc See: https://github.com/MicahParks/keyfunc/blob/master/keyfunc.go#L56
//
// base64urlTrailingPadding removes trailing padding before decoding a string from base64url. Some non-RFC compliant
// JWKS contain padding at the end values for base64url encoded public keys.
//
// Trailing padding is required to be removed from base64url encoded keys.
// RFC 7517 Section 1.1 defines base64url the same as RFC 7515 Section 2:
// https://datatracker.ietf.org/doc/html/rfc7517#section-1.1
// https://datatracker.ietf.org/doc/html/rfc7515#section-2
func base64urlTrailingPadding(s string) ([]byte, error) {
s = strings.TrimRight(s, "=")
return base64.RawURLEncoding.DecodeString(s)
}
// Strips eventual optional members of a JWK in order to be able to compute the thumbprint of it
// https://datatracker.ietf.org/doc/html/rfc7638#section-3.2
func getThumbprintableJwkJSONbytes(jwk map[string]interface{}) ([]byte, error) {
minimalJwk, err := parseJwk(jwk)
if err != nil {
return nil, err
}
jwkHeaderJSONBytes, err := getKeyStringRepresentation(minimalJwk)
if err != nil {
return nil, err
}
return jwkHeaderJSONBytes, nil
}
// Returns the string representation of a key in JSON format.
func getKeyStringRepresentation(key interface{}) ([]byte, error) {
var keyParts interface{}
switch key := key.(type) {
case *ecdsa.PublicKey:
// Calculate the size of the byte array representation of an elliptic curve coordinate
// and ensure that the byte array representation of the key is padded correctly.
bits := key.Curve.Params().BitSize
keyCurveBytesSize := bits/8 + bits%8
keyParts = map[string]interface{}{
"kty": "EC",
"crv": key.Curve.Params().Name,
"x": base64.RawURLEncoding.EncodeToString(key.X.FillBytes(make([]byte, keyCurveBytesSize))),
"y": base64.RawURLEncoding.EncodeToString(key.Y.FillBytes(make([]byte, keyCurveBytesSize))),
}
case *rsa.PublicKey:
keyParts = map[string]interface{}{
"kty": "RSA",
"e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(key.E)).Bytes()),
"n": base64.RawURLEncoding.EncodeToString(key.N.Bytes()),
}
case ed25519.PublicKey:
keyParts = map[string]interface{}{
"kty": "OKP",
"crv": "Ed25519",
"x": base64.RawURLEncoding.EncodeToString(key),
}
default:
return nil, ErrUnsupportedKeyAlgorithm
}
return json.Marshal(keyParts)
}