Skip to content

Commit

Permalink
feat: Make sure VP type corresponds with PEX definition
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Feb 18, 2023
1 parent f04345b commit 3dafa3f
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions packages/did-auth-siop-op-authenticator/src/session/OpSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
ResolveOpts,
} from '@sphereon/did-auth-siop'
import { PresentationSignCallBackParams, SubmissionRequirementMatch } from '@sphereon/pex'
import { IVerifiableCredential, IVerifiablePresentation, parseDid, W3CVerifiablePresentation } from '@sphereon/ssi-types'
import {
IVerifiableCredential,
IVerifiablePresentation,
parseDid,
W3CVerifiablePresentation,
} from '@sphereon/ssi-types'
import { SuppliedSigner } from '@sphereon/ssi-sdk-core'
import {
IAuthRequestDetails,
Expand Down Expand Up @@ -74,7 +79,7 @@ export class OpSession {
presentationSignCallback,
wellknownDidVerifyCallback,
},
this.context
this.context,
)
}

Expand All @@ -86,7 +91,7 @@ export class OpSession {
if (typeof args.customApproval === 'string') {
if (args.customApprovals !== undefined && args.customApprovals[args.customApproval] !== undefined) {
return args.customApprovals[args.customApproval](verifiedAuthorizationRequest, this.id).then(() =>
this.sendSiopAuthorizationResponse({ verifiedAuthorizationRequest: verifiedAuthorizationRequest })
this.sendSiopAuthorizationResponse({ verifiedAuthorizationRequest: verifiedAuthorizationRequest }),
)
}
return Promise.reject(new Error(`Custom approval not found for key: ${args.customApproval}`))
Expand All @@ -106,7 +111,7 @@ export class OpSession {
const url = args.stateId ? `${args.redirectUrl}?stateId=${args.stateId}` : args.redirectUrl
return fetch(url)
.then(async (response: Response) =>
response.status >= 400 ? Promise.reject(new Error(await response.text())) : this.op!.parseAuthorizationRequestURI(await response.text())
response.status >= 400 ? Promise.reject(new Error(await response.text())) : this.op!.parseAuthorizationRequestURI(await response.text()),
)
.catch((error: unknown) => Promise.reject(error))
}
Expand Down Expand Up @@ -150,7 +155,7 @@ export class OpSession {
}

return this.op!.verifyAuthorizationRequest(args.requestURI.requestObjectJwt!, options).catch((error: string | undefined) =>
Promise.reject(new Error(error))
Promise.reject(new Error(error)),
)
}

Expand Down Expand Up @@ -194,7 +199,7 @@ export class OpSession {
presentationSignCallback?: PresentationSignCallback
nonce?: string
domain?: string
}
},
): Promise<IMatchedPresentationDefinition[]> {
return await Promise.all(presentationDefs.map(this.mapper(verifiableCredentials, presentationSignCallback, options)))
}
Expand All @@ -205,7 +210,7 @@ export class OpSession {
options?: {
nonce?: string
domain?: string
}
},
) {
return async (presentationDef: PresentationDefinitionWithLocation): Promise<IMatchedPresentationDefinition> => {
const presentationExchange = this.getPresentationExchange(verifiableCredentials)
Expand All @@ -223,11 +228,12 @@ export class OpSession {
presentationDef.definition,
checked.verifiableCredential as IVerifiableCredential[],
options,
presentationSignCallback
presentationSignCallback,
)

return {
location: PresentationLocation.ID_TOKEN,
format: VerifiablePresentationTypeFormat.LDP_VP,
location: PresentationLocation.ID_TOKEN, //TODO: determine whether it needs to be id token or vp_token
format: presentationDef.definition.format && (presentationDef.definition.format.ldp || presentationDef.definition.format.ldp_vp || presentationDef.definition.format.ldp_vc) ? VerifiablePresentationTypeFormat.LDP_VP : VerifiablePresentationTypeFormat.JWT_VP,
presentation: verifiablePresentation as IVerifiablePresentation,
}
}
Expand All @@ -244,7 +250,7 @@ export class OpSession {
identifier: IIdentifier,
verificationMethodSection: DIDDocumentSection = 'authentication',
context: IRequiredContext,
keyId?: string
keyId?: string,
): Promise<IKey> {
const keys = await mapIdentifierKeysToDoc(identifier, verificationMethodSection, context)
if (!keys || keys.length === 0) {
Expand Down Expand Up @@ -295,7 +301,7 @@ export class OpSession {
presentationSignCallback?: PresentationSignCallback
wellknownDidVerifyCallback?: VerifyCallback
},
context: IRequiredContext
context: IRequiredContext,
): Promise<OP> {
if (!identifier.controllerKeyId) {
return Promise.reject(new Error(`No controller key found for identifier: ${identifier.did}`))
Expand All @@ -305,21 +311,21 @@ export class OpSession {
const verifyCallback = wellknownDidVerifyCallback
? wellknownDidVerifyCallback
: async (): Promise<IVerifyCredentialResult> => {
return { verified: true }
}
return { verified: true }
}

const presentationCallback = presentationSignCallback
? presentationSignCallback
: async (args: PresentationSignCallBackParams): Promise<W3CVerifiablePresentation> => {
const presentation: PresentationPayload = args.presentation as PresentationPayload
const format = args.presentationDefinition.format
return (await context.agent.createVerifiablePresentation({
presentation,
keyRef: keyRef.kid,
fetchRemoteContexts: true,
proofFormat: format && (format.ldp || format.ldp_vp) ? 'lds' : 'jwt',
})) as W3CVerifiablePresentation
}
const presentation: PresentationPayload = args.presentation as PresentationPayload
const format = args.presentationDefinition.format
return (await context.agent.createVerifiablePresentation({
presentation,
keyRef: keyRef.kid,
fetchRemoteContexts: true,
proofFormat: format && (format.ldp || format.ldp_vp) ? 'lds' : 'jwt',
})) as W3CVerifiablePresentation
}

const builder = OP.builder()
.withExpiresIn(expiresIn)
Expand All @@ -329,7 +335,7 @@ export class OpSession {
SuppliedSigner(keyRef, context, this.getSigningAlgo(keyRef.type) as unknown as KeyAlgo),
identifier.did,
identifier.controllerKeyId,
this.getSigningAlgo(keyRef.type)
this.getSigningAlgo(keyRef.type),
)
.registration({
registrationBy: {
Expand Down

0 comments on commit 3dafa3f

Please sign in to comment.