| 1 |
package rp |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rand" |
| 9 |
"crypto/rsa" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"net/url" |
| 14 |
"testing" |
| 15 |
|
| 16 |
jose "github.com/go-jose/go-jose/v4" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
"golang.org/x/oauth2" |
| 20 |
|
| 21 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 22 |
) |
| 23 |
|
| 24 |
func mustECKey(t *testing.T, c elliptic.Curve) *ecdsa.PrivateKey { |
| 25 |
t.Helper() |
| 26 |
key, err := ecdsa.GenerateKey(c, rand.Reader) |
| 27 |
require.NoError(t, err) |
| 28 |
return key |
| 29 |
} |
| 30 |
|
| 31 |
func newKeyBoundRP(t *testing.T) *relyingParty { |
| 32 |
t.Helper() |
| 33 |
rp := &relyingParty{oauthConfig: &oauth2.Config{Scopes: []string{"openid"}}} |
| 34 |
require.NoError(t, WithKeyBinding(mustECKey(t, elliptic.P256()), jose.ES256)(rp)) |
| 35 |
return rp |
| 36 |
} |
| 37 |
|
| 38 |
type roundTripFunc func(*http.Request) (*http.Response, error) |
| 39 |
|
| 40 |
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } |
| 41 |
|
| 42 |
func TestKeyBindingAlgMatchesKey(t *testing.T) { |
| 43 |
rsaPub := &mustRSAKey(t, 2048).PublicKey |
| 44 |
ecP256 := &mustECKey(t, elliptic.P256()).PublicKey |
| 45 |
ecP384 := &mustECKey(t, elliptic.P384()).PublicKey |
| 46 |
ecP521 := &mustECKey(t, elliptic.P521()).PublicKey |
| 47 |
edPub, _, err := ed25519.GenerateKey(rand.Reader) |
| 48 |
require.NoError(t, err) |
| 49 |
|
| 50 |
tests := []struct { |
| 51 |
name string |
| 52 |
alg jose.SignatureAlgorithm |
| 53 |
pub crypto.PublicKey |
| 54 |
want bool |
| 55 |
}{ |
| 56 |
{name: "RSA with RS256", alg: jose.RS256, pub: rsaPub, want: true}, |
| 57 |
{name: "RSA with PS512", alg: jose.PS512, pub: rsaPub, want: true}, |
| 58 |
{name: "RSA with ES256", alg: jose.ES256, pub: rsaPub, want: false}, |
| 59 |
{name: "RSA with HS256", alg: jose.HS256, pub: rsaPub, want: false}, |
| 60 |
{name: "P-256 with ES256", alg: jose.ES256, pub: ecP256, want: true}, |
| 61 |
{name: "P-256 with ES384", alg: jose.ES384, pub: ecP256, want: false}, |
| 62 |
{name: "P-384 with ES384", alg: jose.ES384, pub: ecP384, want: true}, |
| 63 |
{name: "P-521 with ES512", alg: jose.ES512, pub: ecP521, want: true}, |
| 64 |
{name: "P-256 with RS256", alg: jose.RS256, pub: ecP256, want: false}, |
| 65 |
{name: "ed25519 with EdDSA", alg: jose.EdDSA, pub: edPub, want: true}, |
| 66 |
{name: "ed25519 with ES256", alg: jose.ES256, pub: edPub, want: false}, |
| 67 |
{name: "opaque key asymmetric alg", alg: jose.ES256, pub: "opaque", want: true}, |
| 68 |
{name: "opaque key symmetric alg", alg: jose.HS256, pub: "opaque", want: false}, |
| 69 |
} |
| 70 |
for _, tt := range tests { |
| 71 |
t.Run(tt.name, func(t *testing.T) { |
| 72 |
assert.Equal(t, tt.want, keyBindingAlgMatchesKey(tt.alg, tt.pub)) |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func mustRSAKey(t *testing.T, bits int) *rsa.PrivateKey { |
| 78 |
t.Helper() |
| 79 |
key, err := rsa.GenerateKey(rand.Reader, bits) |
| 80 |
require.NoError(t, err) |
| 81 |
return key |
| 82 |
} |
| 83 |
|
| 84 |
func TestNilCryptoSigner(t *testing.T) { |
| 85 |
var typedNil *rsa.PrivateKey |
| 86 |
assert.True(t, nilCryptoSigner(nil)) |
| 87 |
assert.True(t, nilCryptoSigner(typedNil)) |
| 88 |
assert.False(t, nilCryptoSigner(mustECKey(t, elliptic.P256()))) |
| 89 |
} |
| 90 |
|
| 91 |
func TestWithKeyBinding(t *testing.T) { |
| 92 |
t.Run("oauth2 only", func(t *testing.T) { |
| 93 |
rp := &relyingParty{oauth2Only: true, oauthConfig: &oauth2.Config{}} |
| 94 |
err := WithKeyBinding(mustECKey(t, elliptic.P256()), jose.ES256)(rp) |
| 95 |
assert.ErrorIs(t, err, ErrInvalidOption) |
| 96 |
}) |
| 97 |
|
| 98 |
t.Run("nil signer", func(t *testing.T) { |
| 99 |
rp := &relyingParty{oauthConfig: &oauth2.Config{}} |
| 100 |
err := WithKeyBinding(nil, jose.ES256)(rp) |
| 101 |
assert.ErrorIs(t, err, ErrInvalidKeyBinding) |
| 102 |
}) |
| 103 |
|
| 104 |
t.Run("alg does not match key", func(t *testing.T) { |
| 105 |
rp := &relyingParty{oauthConfig: &oauth2.Config{}} |
| 106 |
err := WithKeyBinding(mustECKey(t, elliptic.P256()), jose.RS256)(rp) |
| 107 |
assert.ErrorIs(t, err, ErrInvalidKeyBinding) |
| 108 |
}) |
| 109 |
|
| 110 |
t.Run("key too weak", func(t *testing.T) { |
| 111 |
rp := &relyingParty{oauthConfig: &oauth2.Config{}} |
| 112 |
err := WithKeyBinding(mustRSAKey(t, 1024), jose.RS256)(rp) |
| 113 |
assert.ErrorIs(t, err, ErrInvalidKeyBinding) |
| 114 |
}) |
| 115 |
|
| 116 |
t.Run("success adds scope and thumbprint", func(t *testing.T) { |
| 117 |
key := mustECKey(t, elliptic.P256()) |
| 118 |
rp := &relyingParty{oauthConfig: &oauth2.Config{Scopes: []string{"openid"}}} |
| 119 |
require.NoError(t, WithKeyBinding(key, jose.ES256)(rp)) |
| 120 |
|
| 121 |
require.NotNil(t, rp.keyBinding) |
| 122 |
want, err := oidc.JWKThumbprint(&jose.JSONWebKey{Key: key.Public(), Algorithm: string(jose.ES256)}) |
| 123 |
require.NoError(t, err) |
| 124 |
assert.Equal(t, want, rp.KeyBindingThumbprint()) |
| 125 |
assert.Contains(t, rp.oauthConfig.Scopes, oidc.ScopeBoundKey) |
| 126 |
assert.Contains(t, rp.oauthConfig.Scopes, "openid") |
| 127 |
}) |
| 128 |
|
| 129 |
t.Run("does not duplicate scope", func(t *testing.T) { |
| 130 |
rp := &relyingParty{oauthConfig: &oauth2.Config{Scopes: []string{"openid", oidc.ScopeBoundKey}}} |
| 131 |
require.NoError(t, WithKeyBinding(mustECKey(t, elliptic.P256()), jose.ES256)(rp)) |
| 132 |
|
| 133 |
var count int |
| 134 |
for _, s := range rp.oauthConfig.Scopes { |
| 135 |
if s == oidc.ScopeBoundKey { |
| 136 |
count++ |
| 137 |
} |
| 138 |
} |
| 139 |
assert.Equal(t, 1, count) |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
func TestKeyBindingRP(t *testing.T) { |
| 144 |
_, ok := keyBindingRP(&relyingParty{}) |
| 145 |
assert.False(t, ok) |
| 146 |
|
| 147 |
configured, ok := keyBindingRP(newKeyBoundRP(t)) |
| 148 |
assert.True(t, ok) |
| 149 |
assert.NotEmpty(t, configured.KeyBindingThumbprint()) |
| 150 |
} |
| 151 |
|
| 152 |
func TestSignDPoPProof(t *testing.T) { |
| 153 |
t.Run("not configured", func(t *testing.T) { |
| 154 |
_, err := (&relyingParty{}).SignDPoPProof("POST", "https://op.example.com/token", "code") |
| 155 |
assert.ErrorIs(t, err, ErrInvalidKeyBinding) |
| 156 |
}) |
| 157 |
|
| 158 |
tests := []struct { |
| 159 |
name string |
| 160 |
code string |
| 161 |
}{ |
| 162 |
{name: "with code", code: "authorization-code"}, |
| 163 |
{name: "without code", code: ""}, |
| 164 |
} |
| 165 |
for _, tt := range tests { |
| 166 |
t.Run(tt.name, func(t *testing.T) { |
| 167 |
rp := newKeyBoundRP(t) |
| 168 |
const htu = "https://op.example.com/token" |
| 169 |
proof, err := rp.SignDPoPProof(http.MethodPost, htu, tt.code) |
| 170 |
require.NoError(t, err) |
| 171 |
|
| 172 |
jws, err := jose.ParseSigned(proof, []jose.SignatureAlgorithm{jose.ES256}) |
| 173 |
require.NoError(t, err) |
| 174 |
require.Len(t, jws.Signatures, 1) |
| 175 |
|
| 176 |
typ := jws.Signatures[0].Header.ExtraHeaders[jose.HeaderType] |
| 177 |
assert.Equal(t, string(oidc.DPoPProofType), typ) |
| 178 |
require.NotNil(t, jws.Signatures[0].Header.JSONWebKey, "proof must embed the public jwk") |
| 179 |
assert.True(t, jws.Signatures[0].Header.JSONWebKey.IsPublic()) |
| 180 |
|
| 181 |
var claims oidc.DPoPProofClaims |
| 182 |
require.NoError(t, json.Unmarshal(jws.UnsafePayloadWithoutVerification(), &claims)) |
| 183 |
assert.Equal(t, http.MethodPost, claims.HTTPMethod) |
| 184 |
assert.Equal(t, htu, claims.HTTPURI) |
| 185 |
assert.NotEmpty(t, claims.JWTID) |
| 186 |
if tt.code == "" { |
| 187 |
assert.Empty(t, claims.CodeHash) |
| 188 |
} else { |
| 189 |
assert.Equal(t, oidc.CodeHash(tt.code), claims.CodeHash) |
| 190 |
} |
| 191 |
}) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func signKeyBoundIDToken(t *testing.T, opKey crypto.Signer, typ string, cnfKey crypto.PublicKey) string { |
| 196 |
t.Helper() |
| 197 |
signer, err := jose.NewSigner( |
| 198 |
jose.SigningKey{Algorithm: jose.ES256, Key: opKey}, |
| 199 |
(&jose.SignerOptions{}).WithType(jose.ContentType(typ)), |
| 200 |
) |
| 201 |
require.NoError(t, err) |
| 202 |
|
| 203 |
payload := map[string]any{"sub": "user-1"} |
| 204 |
if cnfKey != nil { |
| 205 |
jwkBytes, err := (&jose.JSONWebKey{Key: cnfKey}).MarshalJSON() |
| 206 |
require.NoError(t, err) |
| 207 |
payload["cnf"] = map[string]json.RawMessage{"jwk": jwkBytes} |
| 208 |
} |
| 209 |
raw, err := json.Marshal(payload) |
| 210 |
require.NoError(t, err) |
| 211 |
jws, err := signer.Sign(raw) |
| 212 |
require.NoError(t, err) |
| 213 |
token, err := jws.CompactSerialize() |
| 214 |
require.NoError(t, err) |
| 215 |
return token |
| 216 |
} |
| 217 |
|
| 218 |
func TestVerifyKeyBindingIDToken(t *testing.T) { |
| 219 |
opKey := mustECKey(t, elliptic.P256()) |
| 220 |
boundKey := mustECKey(t, elliptic.P256()) |
| 221 |
otherKey := mustECKey(t, elliptic.P256()) |
| 222 |
|
| 223 |
expectedJKT, err := oidc.JWKThumbprint(&jose.JSONWebKey{Key: boundKey.Public()}) |
| 224 |
require.NoError(t, err) |
| 225 |
|
| 226 |
tests := []struct { |
| 227 |
name string |
| 228 |
token string |
| 229 |
alg jose.SignatureAlgorithm |
| 230 |
wantErr error |
| 231 |
}{ |
| 232 |
{ |
| 233 |
name: "valid", |
| 234 |
token: signKeyBoundIDToken(t, opKey, string(oidc.IDTokenTypeDPoP), boundKey.Public()), |
| 235 |
alg: jose.ES256, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "wrong typ", |
| 239 |
token: signKeyBoundIDToken(t, opKey, "JWT", boundKey.Public()), |
| 240 |
alg: jose.ES256, |
| 241 |
wantErr: ErrKeyBindingIDToken, |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "missing cnf", |
| 245 |
token: signKeyBoundIDToken(t, opKey, string(oidc.IDTokenTypeDPoP), nil), |
| 246 |
alg: jose.ES256, |
| 247 |
wantErr: ErrKeyBindingIDToken, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "cnf key mismatch", |
| 251 |
token: signKeyBoundIDToken(t, opKey, string(oidc.IDTokenTypeDPoP), otherKey.Public()), |
| 252 |
alg: jose.ES256, |
| 253 |
wantErr: ErrKeyBindingConfirmation, |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: "unparseable token", |
| 257 |
token: "not-a-jwt", |
| 258 |
alg: jose.ES256, |
| 259 |
wantErr: ErrKeyBindingIDToken, |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "alg mismatch", |
| 263 |
token: signKeyBoundIDToken(t, opKey, string(oidc.IDTokenTypeDPoP), boundKey.Public()), |
| 264 |
alg: jose.ES384, |
| 265 |
wantErr: ErrKeyBindingIDToken, |
| 266 |
}, |
| 267 |
} |
| 268 |
for _, tt := range tests { |
| 269 |
t.Run(tt.name, func(t *testing.T) { |
| 270 |
err := verifyKeyBindingIDToken(tt.token, tt.alg, expectedJKT) |
| 271 |
if tt.wantErr == nil { |
| 272 |
assert.NoError(t, err) |
| 273 |
return |
| 274 |
} |
| 275 |
assert.ErrorIs(t, err, tt.wantErr) |
| 276 |
}) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestHTU(t *testing.T) { |
| 281 |
tests := []struct { |
| 282 |
name string |
| 283 |
raw string |
| 284 |
wantClaim string |
| 285 |
wantKey string |
| 286 |
}{ |
| 287 |
{name: "plain", raw: "https://op.example.com/token", |
| 288 |
wantClaim: "https://op.example.com/token", wantKey: "https://op.example.com/token"}, |
| 289 |
{name: "strips query", raw: "https://op.example.com/token?tenant=42", |
| 290 |
wantClaim: "https://op.example.com/token", wantKey: "https://op.example.com/token"}, |
| 291 |
{name: "strips empty query", raw: "https://op.example.com/token?", |
| 292 |
wantClaim: "https://op.example.com/token", wantKey: "https://op.example.com/token"}, |
| 293 |
{name: "strips fragment", raw: "https://op.example.com/token#frag", |
| 294 |
wantClaim: "https://op.example.com/token", wantKey: "https://op.example.com/token"}, |
| 295 |
{name: "strips userinfo", raw: "https://user:[email protected]/token", |
| 296 |
wantClaim: "https://op.example.com/token", wantKey: "https://op.example.com/token"}, |
| 297 |
// The claim keeps the spelling the OP published; only the key normalizes. |
| 298 |
{name: "keeps host case in the claim", raw: "https://OP.Example.COM/token", |
| 299 |
wantClaim: "https://OP.Example.COM/token", wantKey: "https://op.example.com/token"}, |
| 300 |
{name: "keeps default https port in the claim", raw: "https://op.example.com:443/token", |
| 301 |
wantClaim: "https://op.example.com:443/token", wantKey: "https://op.example.com/token"}, |
| 302 |
{name: "keeps default http port in the claim", raw: "http://op.example.com:80/token", |
| 303 |
wantClaim: "http://op.example.com:80/token", wantKey: "http://op.example.com/token"}, |
| 304 |
{name: "keeps non-default port", raw: "https://op.example.com:8443/token", |
| 305 |
wantClaim: "https://op.example.com:8443/token", wantKey: "https://op.example.com:8443/token"}, |
| 306 |
{name: "ipv6 literal uppercased", raw: "https://[2001:DB8::1]/token", |
| 307 |
wantClaim: "https://[2001:DB8::1]/token", wantKey: "https://[2001:db8::1]/token"}, |
| 308 |
{name: "keeps ipv6 brackets when default port", raw: "https://[2001:db8::1]:443/token", |
| 309 |
wantClaim: "https://[2001:db8::1]:443/token", wantKey: "https://[2001:db8::1]/token"}, |
| 310 |
{name: "keeps path case and encoding", raw: "https://op.example.com/a%2Fb/Token", |
| 311 |
wantClaim: "https://op.example.com/a%2Fb/Token", wantKey: "https://op.example.com/a%2Fb/Token"}, |
| 312 |
} |
| 313 |
for _, tt := range tests { |
| 314 |
t.Run(tt.name, func(t *testing.T) { |
| 315 |
u, err := url.Parse(tt.raw) |
| 316 |
require.NoError(t, err) |
| 317 |
assert.Equal(t, tt.wantClaim, htuClaim(u), "signed htu claim") |
| 318 |
assert.Equal(t, tt.wantKey, htuKey(u), "comparison key") |
| 319 |
}) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
func TestKeyBindingHTTPClient(t *testing.T) { |
| 324 |
const tokenEndpoint = "https://op.example.com/token" |
| 325 |
rp := newKeyBoundRP(t) |
| 326 |
|
| 327 |
// captureClient returns an http.Client whose transport records the last |
| 328 |
// request |
| 329 |
captureClient := func(seen **http.Request) *http.Client { |
| 330 |
return &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { |
| 331 |
*seen = r |
| 332 |
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil |
| 333 |
})} |
| 334 |
} |
| 335 |
|
| 336 |
// proofHTU extracts the htu claim from the DPoP header of the request |
| 337 |
proofHTU := func(t *testing.T, req *http.Request) string { |
| 338 |
t.Helper() |
| 339 |
jws, err := jose.ParseSigned(req.Header.Get(oidc.DPoPHeader), []jose.SignatureAlgorithm{jose.ES256}) |
| 340 |
require.NoError(t, err) |
| 341 |
var claims oidc.DPoPProofClaims |
| 342 |
require.NoError(t, json.Unmarshal(jws.UnsafePayloadWithoutVerification(), &claims)) |
| 343 |
return claims.HTTPURI |
| 344 |
} |
| 345 |
|
| 346 |
t.Run("signs proof for the token endpoint", func(t *testing.T) { |
| 347 |
var seen *http.Request |
| 348 |
hc, err := keyBindingHTTPClient(captureClient(&seen), rp, "code", tokenEndpoint) |
| 349 |
require.NoError(t, err) |
| 350 |
|
| 351 |
// A query string must not change the pinned htu. |
| 352 |
req, err := http.NewRequest(http.MethodPost, tokenEndpoint+"?foo=bar", nil) |
| 353 |
require.NoError(t, err) |
| 354 |
resp, err := hc.Transport.RoundTrip(req) |
| 355 |
require.NoError(t, err) |
| 356 |
assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 357 |
require.NotNil(t, seen) |
| 358 |
assert.Equal(t, tokenEndpoint, proofHTU(t, seen)) |
| 359 |
}) |
| 360 |
|
| 361 |
t.Run("accepts equivalent variants of the token endpoint", func(t *testing.T) { |
| 362 |
tests := []struct { |
| 363 |
name string |
| 364 |
configured string |
| 365 |
request string |
| 366 |
wantHTU string |
| 367 |
}{ |
| 368 |
{name: "configured endpoint has a query string", configured: tokenEndpoint + "?tenant=42", |
| 369 |
request: tokenEndpoint + "?tenant=42", wantHTU: tokenEndpoint}, |
| 370 |
{name: "configured endpoint has a fragment", configured: tokenEndpoint + "#x", |
| 371 |
request: tokenEndpoint, wantHTU: tokenEndpoint}, |
| 372 |
{name: "request carries userinfo", configured: tokenEndpoint, |
| 373 |
request: "https://user:[email protected]/token", wantHTU: tokenEndpoint}, |
| 374 |
// A default port or a different host case on either side is |
| 375 |
// equivalent under RFC 3986, so the request is allowed - but the |
| 376 |
// proof still carries the endpoint exactly as the OP published it. |
| 377 |
{name: "request has default port", configured: tokenEndpoint, |
| 378 |
request: "https://op.example.com:443/token", wantHTU: tokenEndpoint}, |
| 379 |
{name: "configured has default port", configured: "https://op.example.com:443/token", |
| 380 |
request: tokenEndpoint, wantHTU: "https://op.example.com:443/token"}, |
| 381 |
{name: "host case differs", configured: "https://OP.Example.com/token", |
| 382 |
request: tokenEndpoint, wantHTU: "https://OP.Example.com/token"}, |
| 383 |
} |
| 384 |
for _, tt := range tests { |
| 385 |
t.Run(tt.name, func(t *testing.T) { |
| 386 |
var seen *http.Request |
| 387 |
hc, err := keyBindingHTTPClient(captureClient(&seen), rp, "code", tt.configured) |
| 388 |
require.NoError(t, err) |
| 389 |
req, err := http.NewRequest(http.MethodPost, tt.request, nil) |
| 390 |
require.NoError(t, err) |
| 391 |
_, err = hc.Transport.RoundTrip(req) |
| 392 |
require.NoError(t, err) |
| 393 |
require.NotNil(t, seen) |
| 394 |
assert.Equal(t, tt.wantHTU, proofHTU(t, seen)) |
| 395 |
}) |
| 396 |
} |
| 397 |
}) |
| 398 |
|
| 399 |
t.Run("refuses another endpoint", func(t *testing.T) { |
| 400 |
tests := []struct { |
| 401 |
name string |
| 402 |
request string |
| 403 |
}{ |
| 404 |
{name: "other host", request: "https://evil.example.com/token"}, |
| 405 |
{name: "other path", request: "https://op.example.com/token/../authorize"}, |
| 406 |
{name: "other scheme", request: "http://op.example.com/token"}, |
| 407 |
{name: "other port", request: "https://op.example.com:8443/token"}, |
| 408 |
} |
| 409 |
for _, tt := range tests { |
| 410 |
t.Run(tt.name, func(t *testing.T) { |
| 411 |
var seen *http.Request |
| 412 |
hc, err := keyBindingHTTPClient(captureClient(&seen), rp, "code", tokenEndpoint) |
| 413 |
require.NoError(t, err) |
| 414 |
req, err := http.NewRequest(http.MethodPost, tt.request, nil) |
| 415 |
require.NoError(t, err) |
| 416 |
_, err = hc.Transport.RoundTrip(req) |
| 417 |
assert.ErrorIs(t, err, ErrKeyBindingEndpoint) |
| 418 |
assert.NotErrorIs(t, err, ErrInvalidKeyBinding, "a runtime refusal is not a configuration error") |
| 419 |
assert.Nil(t, seen, "the request must not reach the network") |
| 420 |
}) |
| 421 |
} |
| 422 |
}) |
| 423 |
|
| 424 |
t.Run("refuses redirects", func(t *testing.T) { |
| 425 |
hc, err := keyBindingHTTPClient(nil, rp, "code", tokenEndpoint) |
| 426 |
require.NoError(t, err) |
| 427 |
req, err := http.NewRequest(http.MethodGet, "https://evil.example.com/", nil) |
| 428 |
require.NoError(t, err) |
| 429 |
assert.ErrorIs(t, hc.CheckRedirect(req, nil), ErrKeyBindingEndpoint) |
| 430 |
}) |
| 431 |
|
| 432 |
t.Run("rejects a missing or invalid token endpoint at construction", func(t *testing.T) { |
| 433 |
for _, endpoint := range []string{"", "/token", "op.example.com/token", "://bad"} { |
| 434 |
t.Run(fmt.Sprintf("%q", endpoint), func(t *testing.T) { |
| 435 |
_, err := keyBindingHTTPClient(nil, rp, "code", endpoint) |
| 436 |
assert.ErrorIs(t, err, ErrInvalidKeyBinding) |
| 437 |
}) |
| 438 |
} |
| 439 |
}) |
| 440 |
} |
| 441 |
|
| 442 |
func TestAuthURLKeyBinding(t *testing.T) { |
| 443 |
key := mustECKey(t, elliptic.P256()) |
| 444 |
rp := &relyingParty{oauthConfig: &oauth2.Config{ |
| 445 |
ClientID: "client", |
| 446 |
Endpoint: oauth2.Endpoint{AuthURL: "https://op.example.com/authorize"}, |
| 447 |
Scopes: []string{"openid"}, |
| 448 |
}} |
| 449 |
require.NoError(t, WithKeyBinding(key, jose.ES256)(rp)) |
| 450 |
|
| 451 |
raw := AuthURL("state-1", rp) |
| 452 |
parsed, err := url.Parse(raw) |
| 453 |
require.NoError(t, err) |
| 454 |
query := parsed.Query() |
| 455 |
|
| 456 |
assert.Equal(t, rp.KeyBindingThumbprint(), query.Get(oidc.DPoPJKTParam)) |
| 457 |
assert.Contains(t, query.Get("scope"), oidc.ScopeBoundKey) |
| 458 |
} |