-
Notifications
You must be signed in to change notification settings - Fork 23
Client-side multi-scope token flow #4178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevenvegt
wants to merge
5
commits into
4144-1-scope-parsing-and-config
Choose a base branch
from
4144-3-client-side-flow
base: 4144-1-scope-parsing-and-config
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+332
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cabd34a
Initialize branch for PR
stevenvegt b64927e
Add PresentationDefinitionResolver with remote PD support
stevenvegt 747fd27
Add local PD fallback and scope policy enforcement to resolver
stevenvegt 08e0232
Wire PresentationDefinitionResolver into client-side token flow
stevenvegt 0b190e6
Apply self-review fixes to PresentationDefinitionResolver
stevenvegt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Copyright (C) 2026 Nuts community | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| package iam | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/nuts-foundation/nuts-node/auth/oauth" | ||
| "github.com/nuts-foundation/nuts-node/core" | ||
| nutsHttp "github.com/nuts-foundation/nuts-node/http" | ||
| "github.com/nuts-foundation/nuts-node/policy" | ||
| "github.com/nuts-foundation/nuts-node/vcr/pe" | ||
| ) | ||
|
|
||
| // ResolvedPresentationDefinition contains a resolved PD and the scope to use in the token request. | ||
| type ResolvedPresentationDefinition struct { | ||
| PresentationDefinition pe.PresentationDefinition | ||
| // Scope is the scope string to include in the token request. | ||
| // When resolved remotely, this is the original scope string (remote AS handles scope policy). | ||
| // When resolved locally, this depends on the configured scope policy. | ||
| Scope string | ||
| } | ||
|
|
||
| // PresentationDefinitionResolver resolves a PresentationDefinition for a given scope string. | ||
| // It uses the remote AS's PD endpoint when available, falling back to local policy resolution. | ||
| type PresentationDefinitionResolver struct { | ||
| httpClient HTTPClient | ||
| policyBackend policy.PDPBackend | ||
| strictMode bool | ||
| } | ||
|
|
||
| // Resolve resolves a PresentationDefinition for the given scope string. | ||
| // If the remote AS metadata advertises a PD endpoint, the PD is fetched remotely | ||
| // and the full scope string is returned (remote AS handles scope policy). | ||
| // If no PD endpoint is available, the local policy backend is used and scope policy is enforced. | ||
| func (r *PresentationDefinitionResolver) Resolve(ctx context.Context, scope string, metadata oauth.AuthorizationServerMetadata) (*ResolvedPresentationDefinition, error) { | ||
| if metadata.PresentationDefinitionEndpoint != "" { | ||
| return r.resolveRemote(ctx, scope, metadata) | ||
| } | ||
| return r.resolveLocal(ctx, scope) | ||
| } | ||
|
|
||
| func (r *PresentationDefinitionResolver) resolveRemote(ctx context.Context, scope string, metadata oauth.AuthorizationServerMetadata) (*ResolvedPresentationDefinition, error) { | ||
| parsedURL, err := core.ParsePublicURL(metadata.PresentationDefinitionEndpoint, r.strictMode) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| pdURL := nutsHttp.AddQueryParams(*parsedURL, map[string]string{ | ||
| "scope": scope, | ||
| }) | ||
| pd, err := r.httpClient.PresentationDefinition(ctx, pdURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &ResolvedPresentationDefinition{ | ||
| PresentationDefinition: *pd, | ||
| Scope: scope, | ||
| }, nil | ||
| } | ||
|
|
||
| func (r *PresentationDefinitionResolver) resolveLocal(ctx context.Context, scope string) (*ResolvedPresentationDefinition, error) { | ||
| if r.policyBackend == nil { | ||
| return nil, fmt.Errorf("local PD resolution requires a policy backend, but none is configured") | ||
| } | ||
| match, err := r.policyBackend.FindCredentialProfile(ctx, scope) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("local PD resolution failed: %w", err) | ||
| } | ||
| if match.ScopePolicy == policy.ScopePolicyProfileOnly && len(match.OtherScopes) > 0 { | ||
| return nil, oauth.OAuth2Error{ | ||
| Code: oauth.InvalidScope, | ||
| Description: "scope policy 'profile-only' does not allow additional scopes", | ||
| } | ||
| } | ||
| // Select the organization PD (default for current single-VP flow). | ||
| // TODO: When #4080 adds two-VP support, this resolver will need to return multiple PDs. | ||
| pd, ok := match.WalletOwnerMapping[pe.WalletOwnerOrganization] | ||
| if !ok { | ||
| return nil, fmt.Errorf("no organization presentation definition for scope %q", match.CredentialProfileScope) | ||
| } | ||
| // For passthrough and dynamic, forward all scopes to the remote AS. | ||
| // The client does not evaluate dynamic scopes — the server handles PDP evaluation at token-grant time (PR #4179). | ||
| resolvedScope := scope | ||
| if match.ScopePolicy == policy.ScopePolicyProfileOnly { | ||
| resolvedScope = match.CredentialProfileScope | ||
| } | ||
| return &ResolvedPresentationDefinition{ | ||
| PresentationDefinition: pd, | ||
| Scope: resolvedScope, | ||
| }, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is what the PDPBackend is for, don't need another abstraction layer (just implement another PDPBackend?)