From 62817ed054888963dd92693c104ea1e6291864b0 Mon Sep 17 00:00:00 2001 From: Wout Slakhorst Date: Tue, 28 Nov 2023 13:42:55 +0100 Subject: [PATCH] reduce duplicate code --- auth/api/iam/openid4vp.go | 42 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/auth/api/iam/openid4vp.go b/auth/api/iam/openid4vp.go index 7475264763..c112b62d40 100644 --- a/auth/api/iam/openid4vp.go +++ b/auth/api/iam/openid4vp.go @@ -54,33 +54,21 @@ func (r *Wrapper) handleAuthorizeRequestFromHolder(ctx context.Context, verifier // GET authorization server metadata for wallet walletID, ok := params[clientIDParam] if !ok { - return nil, oauth.OAuth2Error{ - Code: oauth.InvalidRequest, - Description: "missing client_id parameter", - } + return nil, oauthError(oauth.InvalidRequest, "missing client_id parameter") } // the walletDID must be a did:web walletDID, err := did.ParseDID(walletID) if err != nil || walletDID.Method != "web" { - return nil, oauth.OAuth2Error{ - Code: oauth.InvalidRequest, - Description: "invalid client_id parameter", - } + return nil, oauthError(oauth.InvalidRequest, "invalid client_id parameter") } metadata, err := r.auth.RelyingParty().AuthorizationServerMetadata(ctx, *walletDID) if err != nil { - return nil, oauth.OAuth2Error{ - Code: oauth.ServerError, - Description: "failed to get authorization server metadata (holder)", - } + return nil, oauthError(oauth.ServerError, "failed to get authorization server metadata (holder)") } // own generic endpoint ownURL, err := didweb.DIDToURL(verifier) if err != nil { - return nil, oauth.OAuth2Error{ - Code: oauth.ServerError, - Description: "failed to translate own did to URL", - } + return nil, oauthError(oauth.ServerError, "failed to translate own did to URL") } // generate presentation_definition_uri based on own presentation_definition endpoint + scope pdURL := ownURL.JoinPath("presentation_definition") @@ -99,19 +87,13 @@ func (r *Wrapper) handleAuthorizeRequestFromHolder(ctx context.Context, verifier // &nonce=n-0S6_WzA2Mj HTTP/1.1 walletURL, err := url.Parse(metadata.AuthorizationEndpoint) if err != nil || len(metadata.AuthorizationEndpoint) == 0 { - return nil, oauth.OAuth2Error{ - Code: oauth.InvalidRequest, - Description: "invalid authorization_endpoint (holder)", - } + return nil, oauthError(oauth.InvalidRequest, "invalid authorization_endpoint (holder)") } nonce := crypto.GenerateNonce() callbackURL := ownURL callbackURL.Path, err = url.JoinPath(callbackURL.Path, "response") if err != nil { - return nil, oauth.OAuth2Error{ - Code: oauth.ServerError, - Description: "failed to construct redirect path", - } + return nil, oauthError(oauth.ServerError, "failed to construct redirect path") } redirectURL := AddQueryParams(*walletURL, map[string]string{ @@ -131,10 +113,7 @@ func (r *Wrapper) handleAuthorizeRequestFromHolder(ctx context.Context, verifier } // use nonce to store authorization request in session store if err = r.storageEngine.GetSessionDatabase().GetStore(sessionExpiry, openID4VCContext, verifier.String(), sessionStoreType).Put(nonce, openid4vpRequest); err != nil { - return nil, oauth.OAuth2Error{ - Code: oauth.ServerError, - Description: "failed to store client state", - } + return nil, oauthError(oauth.ServerError, "failed to store client state") } return HandleAuthorizeRequest302Response{ @@ -381,3 +360,10 @@ func assertParamNotPresent(params map[string]string, param ...string) error { } return nil } + +func oauthError(code oauth.ErrorCode, description string) oauth.OAuth2Error { + return oauth.OAuth2Error{ + Code: code, + Description: description, + } +}