Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
fix: clean up and check challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys committed Sep 25, 2023
1 parent 8315d66 commit ad0ecb4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
32 changes: 15 additions & 17 deletions sdk/core/src/CommonNetworkMember/BaseNetworkMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,6 @@ export abstract class BaseNetworkMember {
registryUrl,
resolveLegacyElemLocally,
resolveKeyLocally,
// metricsUrl: metricsUrl,
// component: eventComponent,
// beforeDocumentLoader: options.otherOptions?.beforeDocumentLoader,
},
platformCryptographyTools,
)
Expand Down Expand Up @@ -1011,6 +1008,7 @@ export abstract class BaseNetworkMember {
platformCryptographyTools: IPlatformCryptographyTools,
options: StaticValidateOptions,
vp: unknown,
verifierDid?: string,
challenge?: string,
didDocuments?: any,
): Promise<PresentationValidationOutput> {
Expand All @@ -1022,9 +1020,6 @@ export abstract class BaseNetworkMember {
registryUrl,
resolveLegacyElemLocally,
resolveKeyLocally,
// metricsUrl: metricsUrl,
// component: eventComponent,
// beforeDocumentLoader: options.otherOptions?.beforeDocumentLoader,
},
platformCryptographyTools,
)
Expand All @@ -1034,17 +1029,20 @@ export abstract class BaseNetworkMember {
if (response.result === true) {
const vpChallenge = response.data.proof.challenge

// After validating the VP we need to validate the VP's challenge token
// to ensure that it was issued from the correct DID and that it hasn't expired.
// try {
// Util.isJWT(vpChallenge) && (await this._holderService.verifyPresentationChallenge(vpChallenge, this.did))
// } catch (error) {
// return {
// isValid: false,
// suppliedPresentation: response.data,
// errors: [error],
// }
// }
if (verifierDid) {
// After validating the VP we need to validate the VP's challenge token
// to ensure that it was issued from the correct DID and that it hasn't expired.
try {
Util.isJWT(vpChallenge) &&
(await HolderService.verifyPresentationChallenge(affinity, vpChallenge, verifierDid))
} catch (error) {
return {
isValid: false,
suppliedPresentation: response.data,
errors: [error],
}
}
}

return {
isValid: true,
Expand Down
28 changes: 28 additions & 0 deletions sdk/core/src/services/HolderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@ export default class HolderService {
throw new Error('Token expired or invalid expiration')
}
}

static async verifyPresentationChallenge(affinityService: any, challenge: string, expectedIssuer: string) {
const token = Affinity.fromJwt(challenge)

const { payload } = token

const strippedExpectedIssuer = stripParamsFromDidUrl(expectedIssuer)
const strippedPayloadIssuer = stripParamsFromDidUrl(payload.iss)
if (strippedExpectedIssuer !== strippedPayloadIssuer) {
throw new Error('Token not issued by expected issuer.')
}

const did = DidDocumentService.keyIdToDid(expectedIssuer)
const didDocument = await affinityService.resolveDid(did)
const publicKey = DidDocumentService.getPublicKey(strippedExpectedIssuer, didDocument, payload.kid)

const digestService = new DigestService()
const { digest: tokenDigest, signature } = digestService.getTokenDigest(token)
const isSignatureVerified = KeysService.verify(tokenDigest, publicKey, signature)

if (!isSignatureVerified) {
throw new Error('Signature on token is invalid')
}

if (payload.exp < Date.now()) {
throw new Error('Token expired or invalid expiration')
}
}
async verifyCredentialOfferRequest(credentialOfferRequestToken: string) {
try {
await this._affinityService.validateJWT(credentialOfferRequestToken)
Expand Down
7 changes: 6 additions & 1 deletion sdk/core/test/unit/CommonNetworkMember.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,12 @@ describe('CommonNetworkMember', () => {
resolveKeyLocally: true,
}

const resultStaticMethod = await AffinidiWallet.verifyPresentation(testPlatformTools, staticOptions, vp)
const resultStaticMethod = await AffinidiWallet.verifyPresentation(
testPlatformTools,
staticOptions,
vp,
requesterCommonNetworkMember.did,
)
expect(resultStaticMethod.isValid).to.eq(true)
})

Expand Down

0 comments on commit ad0ecb4

Please sign in to comment.