Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions docs/arch/17-token-exchange-delegation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1184,10 +1184,16 @@ spec:

Beyond cryptographic and claim validation, `JWTBearerGrantPolicy.SubjectBindings`
is an exact-match allowlist: an assertion's `sub` must have a configured
binding, and the request's `resource` parameter (required, exactly one)
must be one of that binding's `allowed_resources`. There is no wildcard
subject or resource — every accepted assertion subject and resource pair is
named explicitly.
binding, and the requested resource must be one of that binding's
`allowed_resources`. There is no wildcard subject or resource — every
accepted assertion subject and resource pair is named explicitly.

When the assertion carries a `resource` claim (a string or an array), the
`resource` parameter must name one of its values, or the grant fails with
`invalid_target`. Without the parameter, a string claim is used; an array
claim, or no claim, fails with `invalid_request`. The resolved resource then
goes through the subject binding and, for an ID-JAG, the client audience
check.
- **Synthetic subject and client identity.** The issued token's `sub` is
`<assertion issuer>#<assertion subject>` (`session.New`'s first argument,
matching the delegated-token subject qualification pattern in [Accepted
Expand Down
73 changes: 73 additions & 0 deletions pkg/authserver/server/tokenexchange/id_jag_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,79 @@ func TestIDJAGHandler_ClientGrantAndAudience(t *testing.T) {
}
}

func TestIDJAGHandler_ResourceClaim(t *testing.T) {
t.Parallel()

const otherResource = "https://other.example.com"
tests := []struct {
name string
claim any // nil leaves the assertion without a resource claim
omitResource bool
clientAudience []string // nil keeps idJAGTestResource
wantErrIs error
}{
{name: "requested resource equal to the claim is accepted", claim: idJAGTestResource},
{name: "requested resource different from the claim is refused", claim: otherResource, wantErrIs: server.ErrInvalidTarget},
{name: "omitted resource uses a single-valued claim", claim: idJAGTestResource, omitResource: true},
{
name: "one-element array claim with omitted resource is refused",
claim: []any{idJAGTestResource},
omitResource: true,
wantErrIs: fosite.ErrInvalidRequest,
},
{name: "omitted resource without a claim is refused", omitResource: true, wantErrIs: fosite.ErrInvalidRequest},
{
name: "defaulted resource outside the client audiences is refused",
claim: idJAGTestResource,
omitResource: true,
clientAudience: []string{otherResource},
wantErrIs: server.ErrInvalidTarget,
},
{name: "array claim naming the requested resource is accepted", claim: []any{otherResource, idJAGTestResource}},
{
name: "array claim not naming the requested resource is refused",
claim: []any{otherResource, "https://third.example.com"},
wantErrIs: server.ErrInvalidTarget,
},
{
name: "array claim with omitted resource is refused as ambiguous",
claim: []any{otherResource, idJAGTestResource},
omitResource: true,
wantErrIs: fosite.ErrInvalidRequest,
},
{name: "non-string claim is refused", claim: 42.0, wantErrIs: fosite.ErrInvalidGrant},
{name: "empty array claim is refused", claim: []any{}, wantErrIs: fosite.ErrInvalidGrant},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tj := newTestJWKS(t)
claims := validIDJAGClaims()
if tt.claim != nil {
claims.Extra["resource"] = tt.claim
}
consumer := &recordingAssertionConsumer{}
h := newIDJAGTestHandler(t, &testJWTBearerAssertionValidator{claims: claims}, consumer)
req := newIDJAGRequest(t, tj, idJAGTestClientID)
if tt.omitResource {
req.Form.Del("resource")
}
if tt.clientAudience != nil {
req.Client = &fosite.DefaultClient{ID: idJAGTestClientID, Audience: tt.clientAudience}
}

err := h.HandleTokenEndpointRequest(t.Context(), req)
if tt.wantErrIs == nil {
require.NoError(t, err)
assert.Equal(t, fosite.Arguments{idJAGTestResource}, req.GetGrantedAudience())
return
}
assert.ErrorIs(t, err, tt.wantErrIs)
assert.Empty(t, consumer.key, "a refused redemption must not consume the assertion")
})
}
}

func TestIDJAGHandler_IssuesBoundSession(t *testing.T) {
t.Parallel()

Expand Down
76 changes: 70 additions & 6 deletions pkg/authserver/server/tokenexchange/jwt_bearer_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,17 @@ func validateJWTBearerPolicy(form url.Values, claims *ValidatedClaims, policy *J
if claims.IssuedAt.IsZero() || claims.Expiry.IsZero() || claims.Expiry.Sub(claims.IssuedAt) > policy.maxAssertionAge {
return "", errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The JWT bearer assertion exceeds the configured maximum age."))
}
resources, present := form["resource"]
if !present || len(resources) != 1 || resources[0] == "" {
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"Exactly one resource parameter is required for the JWT-bearer grant."))
requested, err := resolveJWTBearerResource(form, claims)
if err != nil {
return "", err
}
if err := server.ValidateAudienceURI(resources[0]); err != nil {
if err := server.ValidateAudienceURI(requested); err != nil {
return "", errorsx.WithStack(err)
}
for _, binding := range policy.SubjectBindings {
if binding.Subject == claims.Subject {
for _, resource := range binding.AllowedResources {
if resource == resources[0] {
if resource == requested {
return resource, nil
}
}
Expand All @@ -254,6 +253,71 @@ func validateJWTBearerPolicy(form url.Values, claims *ValidatedClaims, policy *J
"The JWT bearer assertion subject is not configured for this grant."))
}

// resolveJWTBearerResource picks the resource to issue for. When the assertion
// carries a resource claim, the request must name one of its values. An
// omitted resource parameter falls back to a string claim; with an array claim
// it is ambiguous, and with no claim there is no default.
func resolveJWTBearerResource(form url.Values, claims *ValidatedClaims) (string, error) {
claimed, err := assertionResourceClaim(claims)
if err != nil {
return "", err
}
resources, present := form["resource"]
if !present {
if single, ok := claims.Extra["resource"].(string); ok {
return single, nil
}
if claimed == nil {
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"A resource parameter is required because the assertion carries no resource claim."))
}
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"A resource parameter is required because the assertion's resource claim is an array."))
}
if len(resources) != 1 || resources[0] == "" {
return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint(
"Exactly one resource parameter is required for the JWT-bearer grant."))
}
if claimed != nil && !slices.Contains(claimed, resources[0]) {
return "", errorsx.WithStack(server.ErrInvalidTarget.WithHint(
"The requested resource is not one the assertion was issued for."))
}
return resources[0], nil
}

// assertionResourceClaim returns the assertion's resource claim as a list, or
// nil when the claim is absent. The claim is a string or an array of strings;
// any other shape, or an empty value, is refused rather than treated as absent.
func assertionResourceClaim(claims *ValidatedClaims) ([]string, error) {
raw, ok := claims.Extra["resource"]
if !ok {
return nil, nil
}
malformed := errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The assertion's resource claim is malformed."))
switch v := raw.(type) {
case string:
if v == "" {
return nil, malformed
}
return []string{v}, nil
case []any:
if len(v) == 0 {
return nil, malformed
}
values := make([]string, 0, len(v))
for _, e := range v {
s, ok := e.(string)
if !ok || s == "" {
return nil, malformed
}
values = append(values, s)
}
return values, nil
default:
return nil, malformed
}
}

// PopulateTokenEndpointResponse issues only an access token.
func (h *JWTBearerHandler) PopulateTokenEndpointResponse(
ctx context.Context, requester fosite.AccessRequester, responder fosite.AccessResponder,
Expand Down