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

DCKM-435: remove optional attributes from pex proof requests #238

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/wasm/src/services/pex/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ import {PEX} from '@sphereon/pex';

const pex: PEX = new PEX();


function removeOptionalAttribute(presentationDefinition) {
/**
* @sphereon/pex is not able to handle optional attributes in the presentation definition
* https://github.com/Sphereon-Opensource/PEX/issues/150
* Any optional attribute in the presentation definition will cause the library to throw an error
* This function removes the optional attribute from the presentation definition
* This is a temporary workaround until the issue is fixed in the @sphereon/pex library
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an issue in that repository we can link to/track?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue for that Sphereon-Opensource/PEX#150

**/
export function removeOptionalAttribute(presentationDefinition) {
presentationDefinition.input_descriptors.forEach(inputDescriptor => {
if (inputDescriptor.constraints && inputDescriptor.constraints.fields) {
inputDescriptor.constraints.fields = inputDescriptor.constraints.fields.filter(field => field.optional !== true);
inputDescriptor.constraints.fields =
inputDescriptor.constraints.fields.filter(
field => field.optional === undefined,
);
}
});

Expand Down
112 changes: 105 additions & 7 deletions packages/wasm/src/services/pex/tests/pex-service.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {pexService} from '../service';
import {pexService, removeOptionalAttribute} from '../service';

describe('Pex Examples', () => {
it('expect to filter credentials based on a minimum value numeric filter', () => {
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Pex Examples', () => {
expect(results.verifiableCredential.length).toBe(1);
});

it('expect to remove optional attribute', () => {
it('should fix PEX bug related to optional attributes', () => {
const credentials = [
{
'@context': [
Expand Down Expand Up @@ -312,9 +312,9 @@ describe('Pex Examples', () => {
id: 'income_test',
input_descriptors: [
{
id: 'ProofIncome-Merit-7',
name: 'Test - released between 1995 and 2005',
purpose: 'Filter credentials based released date',
id: 'Credential 1',
name: 'optional field',
purpose: 'optional field',
constraints: {
fields: [
{
Expand All @@ -323,12 +323,13 @@ describe('Pex Examples', () => {
{
path: ['$.type[*]'],
filter: {
const: 'BasicCredential',
const: '',
},
optional: true,
},
{
path: ['$.expirationDate'],
optional: true,
optional: false,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have tests with both scenarios? Also, do we have tests that ensure the field isn't returned in the presentation if it's optional and the user deselects it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added better tests and also documentation about this function. For now, the optional attribute is used to reveal the attribute automatically and the user can't deselect it since there is no UI implication. For example, in the expiration date. But I can see potential issues with an attribute from the credential subject being marked as optional, since it will be always revealed and the user can't see that. I've just created a jira for it https://dock-team.atlassian.net/browse/DCKA-2504

Two scenarios I can think about:

  • Expiration date being shared automatically:
    The user can't see that it will be shared

  • Credential subject attributes being shared automatically
    The user can't override that

We can handle that in the DCKM-439

],
},
Expand All @@ -343,4 +344,101 @@ describe('Pex Examples', () => {

expect(results.verifiableCredential).toBeTruthy();
});

describe('removeOptionalAttribute', () => {
const getFieldsWithOptionalAttributes = template => {
return template.input_descriptors[0].constraints.fields.filter(
field => field.optional !== undefined,
).length;
};

it('should remove optional attributes', () => {
let template = {
id: 'income_test',
input_descriptors: [
{
id: 'Credential 1',
name: 'optional field',
purpose: 'optional field',
constraints: {
fields: [
{
path: ['$.credentialSubject.id'],
},
{
path: ['$.type[*]'],
filter: {
const: '',
},
optional: true,
},
{
path: ['$.expirationDate'],
optional: false,
},
],
},
},
],
};

expect(getFieldsWithOptionalAttributes(template)).toBe(2);
expect(
getFieldsWithOptionalAttributes(removeOptionalAttribute(template)),
).toBe(0);

template = {
id: 'income_test',
input_descriptors: [
{
id: 'Credential 1',
name: 'optional field',
purpose: 'optional field',
constraints: {
fields: [
{
path: ['$.credentialSubject.id'],
},
{
path: ['$.type[*]'],
filter: {
const: '',
},
optional: true,
},
],
},
},
],
};

expect(getFieldsWithOptionalAttributes(template)).toBe(1);
expect(
getFieldsWithOptionalAttributes(removeOptionalAttribute(template)),
).toBe(0);

template = {
id: 'income_test',
input_descriptors: [
{
id: 'Credential 1',
name: 'optional field',
purpose: 'optional field',
constraints: {
fields: [
{
path: ['$.credentialSubject.id'],
},
],
},
},
],
};

expect(getFieldsWithOptionalAttributes(template)).toBe(0);
expect(
getFieldsWithOptionalAttributes(removeOptionalAttribute(template)),
).toBe(0);
});
});
});
Loading