Skip to content

Commit

Permalink
reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed Nov 28, 2023
1 parent fe31286 commit 62817ed
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions auth/api/iam/openid4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 62817ed

Please sign in to comment.