Skip to content
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

chore: unwrap single-element array in extractPresentationsFromVpToken #168

Merged
merged 9 commits into from
Nov 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,12 @@ export class AuthorizationResponse {
let nonce: string | undefined = this._payload.nonce
if (this._payload?.vp_token) {
const presentations = this.payload.vp_token ? await extractPresentationsFromVpToken(this.payload.vp_token, opts) : []
if (!presentations || (Array.isArray(presentations) && presentations.length === 0)) {
return Promise.reject('missing presentation(s)')
nklomp marked this conversation as resolved.
Show resolved Hide resolved
}
const presentationsArray = Array.isArray(presentations) ? presentations : [presentations]


// We do not verify them, as that is done elsewhere. So we simply can take the first nonce
nonce = presentationsArray
// FIXME toWrappedVerifiablePresentation() does not extract the nonce yet from mdocs.
Expand Down
29 changes: 20 additions & 9 deletions packages/siop-oid4vp/lib/authorization-response/OpenID4VP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export const verifyPresentations = async (
authorizationResponse: AuthorizationResponse,
verifyOpts: VerifyAuthorizationResponseOpts,
): Promise<VerifiedOpenID4VPSubmission | null> => {
const presentations = authorizationResponse.payload.vp_token
? await extractPresentationsFromVpToken(authorizationResponse.payload.vp_token, { hasher: verifyOpts.hasher })
: []
if (!authorizationResponse.payload.vp_token || Array.isArray(authorizationResponse.payload.vp_token) && authorizationResponse.payload.vp_token.length === 0) {
return Promise.reject('the payload is missing a vp_token')
nklomp marked this conversation as resolved.
Show resolved Hide resolved
}

const presentations = await extractPresentationsFromVpToken(authorizationResponse.payload.vp_token, { hasher: verifyOpts.hasher })
const presentationDefinitions = verifyOpts.presentationDefinitions
? Array.isArray(verifyOpts.presentationDefinitions)
? verifyOpts.presentationDefinitions
Expand Down Expand Up @@ -103,8 +105,10 @@ export const verifyPresentations = async (
return null
}

if (!presentations || (Array.isArray(presentations) && presentations.length === 0)) {
return Promise.reject('missing presentation(s)')
nklomp marked this conversation as resolved.
Show resolved Hide resolved
}
const presentationsArray = Array.isArray(presentations) ? presentations : [presentations]

const presentationsWithoutMdoc = presentationsArray.filter((p) => p.format !== 'mso_mdoc')
const nonces = new Set(presentationsWithoutMdoc.map(extractNonceFromWrappedVerifiablePresentation))
if (presentationsWithoutMdoc.length > 0 && nonces.size !== 1) {
Expand Down Expand Up @@ -135,11 +139,14 @@ export const extractPresentationsFromVpToken = async (
vpToken: Array<W3CVerifiablePresentation | CompactSdJwtVc | string> | W3CVerifiablePresentation | CompactSdJwtVc | string,
opts?: { hasher?: Hasher },
): Promise<WrappedVerifiablePresentation[] | WrappedVerifiablePresentation> => {
if (Array.isArray(vpToken)) {
return vpToken.map((vp) => CredentialMapper.toWrappedVerifiablePresentation(vp, { hasher: opts?.hasher }))
const tokens = Array.isArray(vpToken) ? vpToken : [vpToken];
const wrappedTokens = tokens.map(vp =>
CredentialMapper.toWrappedVerifiablePresentation(vp, { hasher: opts?.hasher })
);

return tokens.length === 1 ? wrappedTokens[0] : wrappedTokens;
}
return CredentialMapper.toWrappedVerifiablePresentation(vpToken, { hasher: opts?.hasher })
}

export const createPresentationSubmission = async (
verifiablePresentations: W3CVerifiablePresentation[],
opts?: { presentationDefinitions: (PresentationDefinitionWithLocation | IPresentationDefinition)[] },
Expand Down Expand Up @@ -279,7 +286,11 @@ export const assertValidVerifiablePresentations = async (args: {
hasher?: Hasher
}
}) => {
const presentationsArray = Array.isArray(args.presentations) ? args.presentations : [args.presentations]
const {presentations} = args
if (!presentations || (Array.isArray(presentations) && presentations.length === 0)) {
throw Error('missing presentation(s)')
nklomp marked this conversation as resolved.
Show resolved Hide resolved
}
const presentationsArray = Array.isArray(presentations) ? presentations : [presentations]
if (
(!args.presentationDefinitions || args.presentationDefinitions.filter((a) => a.definition).length === 0) &&
(!presentationsArray || (Array.isArray(presentationsArray) && presentationsArray.filter((vp) => vp.presentation).length === 0))
Expand Down