Sphereon-Opensource / SIOP-OID4VP

Self Issued OpenID Provider v2 (SIOP) with optional OpenID for Verifiable Presentations (OpenID4VP)
77 stars 25 forks source link

presentationVerification callback not waiting #73

Closed cindy-chin closed 5 months ago

cindy-chin commented 8 months ago

RpBuilder.wothPresentationVerification(presentationVerification) seems like is not being awaiting

cindy-chin commented 8 months ago

I mean the callback result is not being awaited

cindy-chin commented 8 months ago

private static async validatePresentationsAgainstDefinition( definition: IPresentationDefinition, vpPayloads: WrappedVerifiablePresentation[], verifyPresentationCallback: PresentationVerificationCallback | undefined, opts?: { limitDisclosureSignatureSuites?: string[]; restrictToFormats?: Format; restrictToDIDMethods?: string[]; presentationSubmission?: PresentationSubmission; hasher?: Hasher; } ) { const pex = new PEX({ hasher: opts?.hasher });

function filterOutCorrectPresentation() {
  //TODO: add support for multiple VPs here
  return vpPayloads.filter(async (vpw: WrappedVerifiablePresentation) => {
    const presentationSubmission =
      opts?.presentationSubmission ??
      (CredentialMapper.isWrappedW3CVerifiablePresentation(vpw) ? vpw.presentation.presentation_submission : undefined);
    const presentation = vpw.presentation;
    if (!definition) {
      throw new Error(SIOPErrors.NO_PRESENTATION_SUBMISSION);
    } else if (
      !vpw.presentation ||
      (CredentialMapper.isWrappedW3CVerifiablePresentation(vpw) &&
        (!vpw.presentation.verifiableCredential || vpw.presentation.verifiableCredential.length === 0))
    ) {
      throw new Error(SIOPErrors.NO_VERIFIABLE_PRESENTATION_NO_CREDENTIALS);
    }
    // The verifyPresentationCallback function is mandatory for RP only,
    // So the behavior here is to bypass it if not present
    if (verifyPresentationCallback) {
      try {
        await verifyPresentationCallback(vpw.original as W3CVerifiablePresentation, presentationSubmission);
      } catch (error: unknown) {
        throw new Error(SIOPErrors.VERIFIABLE_PRESENTATION_SIGNATURE_NOT_VALID);
      }
    }
    // console.log(`Presentation (filter): ${JSON.stringify(presentation)}`);

    const evaluationResults = pex.evaluatePresentation(definition, vpw.original, {
      ...opts,
      presentationSubmission,
    });
    const submission = evaluationResults.value;
    if (!presentation || !submission) {
      throw new Error(SIOPErrors.NO_PRESENTATION_SUBMISSION);
    }
    return submission && submission.definition_id === definition.id;
  });
}

const checkedPresentations: WrappedVerifiablePresentation[] = filterOutCorrectPresentation();

if (checkedPresentations.length !== 1) {
  throw new Error(`${SIOPErrors.COULD_NOT_FIND_VCS_MATCHING_PD}`);
}
const checkedPresentation = checkedPresentations[0];
const presentation = checkedPresentation.presentation;
// console.log(`Presentation (checked): ${JSON.stringify(checkedPresentation.presentation)}`);
if (
  !checkedPresentation.presentation ||
  (CredentialMapper.isWrappedW3CVerifiablePresentation(checkedPresentation) &&
    (!checkedPresentation.presentation.verifiableCredential || checkedPresentation.presentation.verifiableCredential.length === 0))
) {
  throw new Error(SIOPErrors.NO_VERIFIABLE_PRESENTATION_NO_CREDENTIALS);
}
const presentationSubmission =
  opts?.presentationSubmission ?? (CredentialMapper.isW3cPresentation(presentation) ? presentation.presentation_submission : undefined);
const evaluationResults = pex.evaluatePresentation(definition, checkedPresentation.original, {
  ...opts,
  presentationSubmission,
});
PresentationExchange.assertValidPresentationSubmission(evaluationResults.value);
await PresentationExchange.validatePresentationAgainstDefinition(definition, checkedPresentation, {
  ...opts,
  presentationSubmission,
  hasher: opts?.hasher,
});

}

i am not very sure, but i think problem maybe around this function, maybe the filterOutCorrectPresentation() should be awaited?

nklomp commented 7 months ago

Is this still an issue @cindy-chin ? If so could you clarify which callback is not being awaited according to you?

nklomp commented 5 months ago

Closing; no response