-
Notifications
You must be signed in to change notification settings - Fork 23
feat(openid4vci): migrate to v1.0 spec #4057
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
JorisHeadease
wants to merge
19
commits into
master
Choose a base branch
from
feature/openid4vci-v1
base: master
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.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
de75f43
feat(openid4vci): align error codes with v1.0 Section 8.3.1.2
JorisHeadease 8c02553
feat(openid4vci): update types and issuer for v1.0 metadata and offer
JorisHeadease 15654bb
feat(openid4vci): update holder and API handler for v1.0
JorisHeadease a8b0af6
feat(openid4vci): update OpenAPI spec and remove stale VP metadata
JorisHeadease 6ee11b2
fix(openid4vci): align wire formats with v1.0 spec review
JorisHeadease a09c50a
feat(openid4vci): align auth module with v1.0 spec
JorisHeadease 1aa3837
fix(openid4vci): harden input validation and add missing tests
JorisHeadease 77b932d
Merge remote-tracking branch 'origin/master' into feature/openid4vci-v1
JorisHeadease 906dd0f
fix(openid4vci): restore PreAuthorizedGrantAnonymousAccessSupported i…
JorisHeadease e0dfecb
docs(openid4vci): improve OpenAPI spec v1.0 accuracy
JorisHeadease 301be91
fix(openid4vci): correct holder error code for unsupported format
JorisHeadease a0b4a31
test(openid4vci): fix auth header bug and add missing test coverage
JorisHeadease 1d66b58
refactor(openid4vci): restore original error comments and simplify de…
JorisHeadease c0e8347
fix(openid4vci): restore JSON deep copy and remove resolved TODO
JorisHeadease 9cae8c8
fix(openid4vci): harden validation and fix spec compliance issues
JorisHeadease 355330a
refactor(openid4vci): clean up CredentialRequest and rename Id to ID
JorisHeadease dfc9f6c
fix(openid4vci): use json.RawMessage for CredentialResponseEntry
JorisHeadease 276bf0a
refactor(openid4vci): unify duplicate types across packages
JorisHeadease 2474568
qlty fmt
qltysh[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,15 +34,17 @@ import ( | |
| "github.com/nuts-foundation/nuts-node/core" | ||
| "github.com/nuts-foundation/nuts-node/crypto" | ||
| nutsHttp "github.com/nuts-foundation/nuts-node/http" | ||
| "github.com/nuts-foundation/nuts-node/vcr/openid4vci" | ||
| "github.com/nuts-foundation/nuts-node/vdr/resolver" | ||
| ) | ||
|
|
||
| var timeFunc = time.Now | ||
|
|
||
| // jwtTypeOpenID4VCIProof defines the OpenID4VCI JWT-subtype (used as typ claim in the JWT). | ||
| const jwtTypeOpenID4VCIProof = "openid4vci-proof+jwt" | ||
|
|
||
| func (r Wrapper) RequestOpenid4VCICredentialIssuance(ctx context.Context, request RequestOpenid4VCICredentialIssuanceRequestObject) (RequestOpenid4VCICredentialIssuanceResponseObject, error) { | ||
| if request.Body == nil { | ||
| // why did oapi-codegen generate a pointer for the body?? | ||
| return nil, core.InvalidInputError("missing request body") | ||
| } | ||
| walletDID, err := did.ParseDID(request.Body.WalletDid) | ||
| if err != nil { | ||
| return nil, core.InvalidInputError("invalid wallet DID") | ||
|
|
@@ -52,11 +54,6 @@ func (r Wrapper) RequestOpenid4VCICredentialIssuance(ctx context.Context, reques | |
| } else if !owned { | ||
| return nil, core.InvalidInputError("wallet DID does not belong to the subject") | ||
| } | ||
|
|
||
| if request.Body == nil { | ||
| // why did oapi-codegen generate a pointer for the body?? | ||
| return nil, core.InvalidInputError("missing request body") | ||
| } | ||
| // Parse the issuer | ||
| issuer := request.Body.Issuer | ||
| if issuer == "" { | ||
|
|
@@ -81,8 +78,12 @@ func (r Wrapper) RequestOpenid4VCICredentialIssuance(ctx context.Context, reques | |
|
|
||
| // Read and parse the authorization details | ||
| authorizationDetails := []byte("[]") | ||
| var credentialConfigID string | ||
| if len(request.Body.AuthorizationDetails) > 0 { | ||
| authorizationDetails, _ = json.Marshal(request.Body.AuthorizationDetails) | ||
| if id, ok := request.Body.AuthorizationDetails[0]["credential_configuration_id"].(string); ok { | ||
| credentialConfigID = id | ||
| } | ||
| } | ||
| // Generate the state and PKCE | ||
| state := crypto.GenerateNonce() | ||
|
|
@@ -100,9 +101,11 @@ func (r Wrapper) RequestOpenid4VCICredentialIssuance(ctx context.Context, reques | |
| PKCEParams: pkceParams, | ||
| // OpenID4VCI issuers may use multiple Authorization Servers | ||
| // We must use the token_endpoint that corresponds to the same Authorization Server used for the authorization_endpoint | ||
| TokenEndpoint: authzServerMetadata.TokenEndpoint, | ||
| IssuerURL: authzServerMetadata.Issuer, | ||
| IssuerCredentialEndpoint: credentialIssuerMetadata.CredentialEndpoint, | ||
| TokenEndpoint: authzServerMetadata.TokenEndpoint, | ||
| IssuerURL: authzServerMetadata.Issuer, | ||
| IssuerCredentialEndpoint: credentialIssuerMetadata.CredentialEndpoint, | ||
| IssuerNonceEndpoint: credentialIssuerMetadata.NonceEndpoint, | ||
| IssuerCredentialConfigurationID: credentialConfigID, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to store session: %w", err) | ||
|
|
@@ -129,8 +132,6 @@ func (r Wrapper) RequestOpenid4VCICredentialIssuance(ctx context.Context, reques | |
| } | ||
|
|
||
| func (r Wrapper) handleOpenID4VCICallback(ctx context.Context, authorizationCode string, oauthSession *OAuthSession) (CallbackResponseObject, error) { | ||
| // extract callback URI at calling app from OAuthSession | ||
| // this is the URI where the user-agent will be redirected to | ||
| appCallbackURI := oauthSession.redirectURI() | ||
|
|
||
| baseURL := r.subjectToBaseURL(*oauthSession.OwnSubject) | ||
|
|
@@ -142,31 +143,49 @@ func (r Wrapper) handleOpenID4VCICallback(ctx context.Context, authorizationCode | |
| } | ||
|
|
||
| // use code to request access token from remote token endpoint | ||
| response, err := r.auth.IAMClient().AccessToken(ctx, authorizationCode, oauthSession.TokenEndpoint, checkURL.String(), *oauthSession.OwnSubject, clientID, oauthSession.PKCEParams.Verifier, false) | ||
| tokenResponse, err := r.auth.IAMClient().AccessToken(ctx, authorizationCode, oauthSession.TokenEndpoint, checkURL.String(), *oauthSession.OwnSubject, clientID, oauthSession.PKCEParams.Verifier, false) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.AccessDenied, fmt.Sprintf("error while fetching the access_token from endpoint: %s, error: %s", oauthSession.TokenEndpoint, err.Error())), appCallbackURI) | ||
| } | ||
|
|
||
| // make proof and collect credential | ||
| proofJWT, err := r.openid4vciProof(ctx, *oauthSession.OwnDID, oauthSession.IssuerURL, response.Get(oauth.CNonceParam)) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error building proof to fetch the credential from endpoint %s, error: %s", oauthSession.IssuerCredentialEndpoint, err.Error())), appCallbackURI) | ||
| // fetch nonce from the Nonce Endpoint (v1.0 Section 7) | ||
| var nonce string | ||
| if oauthSession.IssuerNonceEndpoint != "" { | ||
| nonce, err = r.auth.IAMClient().RequestNonce(ctx, oauthSession.IssuerNonceEndpoint) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error fetching nonce from %s: %s", oauthSession.IssuerNonceEndpoint, err.Error())), appCallbackURI) | ||
| } | ||
| } | ||
| credentials, err := r.auth.IAMClient().VerifiableCredentials(ctx, oauthSession.IssuerCredentialEndpoint, response.AccessToken, proofJWT) | ||
|
|
||
| // build proof and request credential | ||
| credentialResponse, err := r.requestCredentialWithProof(ctx, oauthSession, tokenResponse.AccessToken, nonce) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while fetching the credential from endpoint %s, error: %s", oauthSession.IssuerCredentialEndpoint, err.Error())), appCallbackURI) | ||
| } | ||
| // validate credential | ||
| // TODO: check that issued credential is bound to DID that requested it (OwnDID)??? | ||
| credential, err := vc.ParseVerifiableCredential(credentials.Credential) | ||
| // on invalid_nonce: fetch a fresh nonce and retry once | ||
| var oidcErr openid4vci.Error | ||
| if errors.As(err, &oidcErr) && oidcErr.Code == openid4vci.InvalidNonce && oauthSession.IssuerNonceEndpoint != "" { | ||
| nonce, err = r.auth.IAMClient().RequestNonce(ctx, oauthSession.IssuerNonceEndpoint) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error fetching nonce for retry from %s: %s", oauthSession.IssuerNonceEndpoint, err.Error())), appCallbackURI) | ||
| } | ||
| credentialResponse, err = r.requestCredentialWithProof(ctx, oauthSession, tokenResponse.AccessToken, nonce) | ||
| } | ||
|
Comment on lines
+163
to
+171
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while fetching the credential from endpoint %s, error: %s", oauthSession.IssuerCredentialEndpoint, err.Error())), appCallbackURI) | ||
| } | ||
| } | ||
| if len(credentialResponse.Credentials) == 0 { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, "credential response does not contain any credentials"), appCallbackURI) | ||
| } | ||
|
|
||
| credentialJSON := string(credentialResponse.Credentials[0].Credential) | ||
| credential, err := vc.ParseVerifiableCredential(credentialJSON) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while parsing the credential: %s, error: %s", credentials.Credential, err.Error())), appCallbackURI) | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while parsing the credential: %s, error: %s", credentialJSON, err.Error())), appCallbackURI) | ||
| } | ||
| err = r.vcr.Verifier().Verify(*credential, true, true, nil) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while verifying the credential from issuer: %s, error: %s", credential.Issuer.String(), err.Error())), appCallbackURI) | ||
| } | ||
| // store credential in wallet | ||
| err = r.vcr.Wallet().Put(ctx, *credential) | ||
| if err != nil { | ||
| return nil, withCallbackURI(oauthError(oauth.ServerError, fmt.Sprintf("error while storing credential with id: %s, error: %s", credential.ID, err.Error())), appCallbackURI) | ||
|
|
@@ -176,18 +195,22 @@ func (r Wrapper) handleOpenID4VCICallback(ctx context.Context, authorizationCode | |
| }, nil | ||
| } | ||
|
|
||
| func (r Wrapper) requestCredentialWithProof(ctx context.Context, oauthSession *OAuthSession, accessToken string, nonce string) (*openid4vci.CredentialResponse, error) { | ||
| proofJWT, err := r.openid4vciProof(ctx, *oauthSession.OwnDID, oauthSession.IssuerURL, nonce) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error building proof: %w", err) | ||
| } | ||
| return r.auth.IAMClient().VerifiableCredentials(ctx, oauthSession.IssuerCredentialEndpoint, accessToken, oauthSession.IssuerCredentialConfigurationID, proofJWT) | ||
| } | ||
|
|
||
| func (r *Wrapper) openid4vciProof(ctx context.Context, holderDid did.DID, audience string, nonce string) (string, error) { | ||
| kid, _, err := r.keyResolver.ResolveKey(holderDid, nil, resolver.AssertionMethod) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to resolve key for did (%s): %w", holderDid.String(), err) | ||
| } | ||
| headers := map[string]interface{}{ | ||
| "typ": jwtTypeOpenID4VCIProof, // MUST be openid4vci-proof+jwt, which explicitly types the proof JWT as recommended in Section 3.11 of [RFC8725]. | ||
| "kid": kid, // JOSE Header containing the key ID. If the Credential shall be bound to a DID, the kid refers to a DID URL which identifies a particular key in the DID Document that the Credential shall be bound to. | ||
| } | ||
| if err != nil { | ||
| // can't fail or would have failed before | ||
| return "", err | ||
| "typ": openid4vci.JWTTypeOpenID4VCIProof, // MUST be openid4vci-proof+jwt, which explicitly types the proof JWT as recommended in Section 3.11 of [RFC8725]. | ||
| "kid": kid, // JOSE Header containing the key ID. If the Credential shall be bound to a DID, the kid refers to a DID URL which identifies a particular key in the DID Document that the Credential shall be bound to. | ||
| } | ||
| claims := map[string]interface{}{ | ||
| jwt.IssuerKey: holderDid.String(), | ||
|
|
||
Oops, something went wrong.
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.
We can also model it in OpenAPI and have a struct for it?