Skip to content

Commit

Permalink
Auto-detect whether to include vc in PD constraint JSON paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Oct 1, 2024
1 parent eab6f87 commit bba0c08
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @digitalbazaar/oid4-client Changelog

## 4.1.0 - 2024-10-dd

### Added
- Auto-detect whether to include `vc` in the JSON paths when
computing presentation definition constraints from a VPR.

## 4.0.0 - 2024-09-20

### Changed
Expand Down
13 changes: 12 additions & 1 deletion lib/oid4vp.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export async function toVpr({

// converts a VPR to partial "authorization request"
export function fromVpr({
verifiablePresentationRequest, strict = false, prefixJwtVcPath = false
verifiablePresentationRequest, strict = false, prefixJwtVcPath
} = {}) {
try {
let {query} = verifiablePresentationRequest;
Expand Down Expand Up @@ -571,6 +571,17 @@ function _matchesInputDescriptor({

// exported for testing purposes only
export function _fromQueryByExampleQuery({credentialQuery, prefixJwtVcPath}) {
// determine `prefixJwtVcPath` default:
// if `credentialQuery` does NOT specify any `acceptedCryptosuites` but it
// does specify `acceptedEnvelopes: ['application/jwt']`, then default
// `prefixJwtVcPath` to `true`
if(prefixJwtVcPath === undefined &&
!credentialQuery.acceptedCryptosuites?.length > 0 &&
(credentialQuery.acceptedEnvelopes?.length === 1 &&
credentialQuery.acceptedEnvelopes?.[0] === 'application/jwt')) {
prefixJwtVcPath = true;
}

const fields = [];
const inputDescriptor = {
id: uuid(),
Expand Down
105 changes: 102 additions & 3 deletions tests/unit/oid4vp.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2022-2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2022-2024 Digital Bazaar, Inc. All rights reserved.
*/
import {_fromQueryByExampleQuery} from '../../lib/oid4vp.js';
import chai from 'chai';
Expand All @@ -8,8 +8,32 @@ chai.should();
const {expect} = chai;

describe('OID4VP', () => {
describe('constructor', () => {
it('should map a QueryByExample to a Presentation Definition', async () => {
describe('QueryByExample => Presentation Definition', () => {
it('should NOT include "vc" prefix in paths', async () => {
const presentation_definition = _fromQueryByExampleQuery({
credentialQuery: {
reason: 'Please present your Driver\'s License to complete the ' +
'verification process.',
example: {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/vdl/v1',
'https://w3id.org/vdl/aamva/v1'
],
type: [
'Iso18013DriversLicenseCredential'
]
}
},
prefixJwtVcPath: false
});
expect(presentation_definition.constraints.fields[0].path).to.eql(
['$[\'@context\']']);
expect(presentation_definition.constraints.fields[1].path).to.eql(
['$[\'type\']']);
});

it('should include "vc" prefix in paths', async () => {
const presentation_definition = _fromQueryByExampleQuery({
credentialQuery: {
reason: 'Please present your Driver\'s License to complete the ' +
Expand All @@ -36,5 +60,80 @@ describe('OID4VP', () => {
'$[\'vc\'][\'type\']'
]);
});

it('auto-detect to NOT include "vc" w/unspecified security', async () => {
const presentation_definition = _fromQueryByExampleQuery({
credentialQuery: {
reason: 'Please present your Driver\'s License to complete the ' +
'verification process.',
example: {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/vdl/v1',
'https://w3id.org/vdl/aamva/v1'
],
type: [
'Iso18013DriversLicenseCredential'
]
}
}
});
expect(presentation_definition.constraints.fields[0].path).to.eql(
['$[\'@context\']']);
expect(presentation_definition.constraints.fields[1].path).to.eql(
['$[\'type\']']);
});

it('auto-detect to NOT include "vc" w/acceptedCryptosuites', async () => {
const presentation_definition = _fromQueryByExampleQuery({
credentialQuery: {
reason: 'Please present your Driver\'s License to complete the ' +
'verification process.',
example: {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/vdl/v1',
'https://w3id.org/vdl/aamva/v1'
],
type: [
'Iso18013DriversLicenseCredential'
]
},
acceptedCryptosuites: ['bbs-2023']
}
});
expect(presentation_definition.constraints.fields[0].path).to.eql(
['$[\'@context\']']);
expect(presentation_definition.constraints.fields[1].path).to.eql(
['$[\'type\']']);
});

it('auto-detect to include "vc" w/acceptedEnvelopes', async () => {
const presentation_definition = _fromQueryByExampleQuery({
credentialQuery: {
reason: 'Please present your Driver\'s License to complete the ' +
'verification process.',
example: {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/vdl/v1',
'https://w3id.org/vdl/aamva/v1'
],
type: [
'Iso18013DriversLicenseCredential'
]
},
acceptedEnvelopes: ['application/jwt']
}
});
expect(presentation_definition.constraints.fields[0].path).to.eql([
'$[\'@context\']',
'$[\'vc\'][\'@context\']'
]);
expect(presentation_definition.constraints.fields[1].path).to.eql([
'$[\'type\']',
'$[\'vc\'][\'type\']'
]);
});
});
});

0 comments on commit bba0c08

Please sign in to comment.