Skip to content

Commit

Permalink
Merge pull request #352 from Concordium/check-required-attributes
Browse files Browse the repository at this point in the history
Check required attributes
  • Loading branch information
orhoj authored Aug 24, 2023
2 parents 1ad0470 + 04cf855 commit cda5a3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/browser-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Validation of required attributes when adding a credential.
- Display contract address of issuer in verifiable credential details.

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
() => setError(t('error.schema')),
[schemas.loading]
);

useEffect(() => {
if (schema) {
// Ensure that all attributes required by the schema are in the attributes. If not, then
// the credential should not be allowed to be added.
const missingRequiredAttributeKeys = [];
const requiredAttributes = schema.properties.credentialSubject.properties.attributes.required;
for (const requiredAttribute of requiredAttributes) {
if (!Object.keys(credential.credentialSubject.attributes).includes(requiredAttribute)) {
missingRequiredAttributeKeys.push(requiredAttribute);
}
}

if (missingRequiredAttributeKeys.length > 0) {
setError(t('error.attribute', { attributeKeys: missingRequiredAttributeKeys }));
}
}
}, [schema?.properties.credentialSubject.properties.attributes.required]);

useEffect(() => () => controller.abort(), []);

const localization = useAsyncMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const t: typeof en = {
// We don't translate these because they are mainly for bug reporting.
metadata: en.error.metadata,
schema: en.error.schema,
attribute: en.error.attribute,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const t = {
'We are unable to add the web3Id credential to the wallet due to the following issue, please report this to the issuer:',
metadata: 'We are unable to load the metadata for the credential.',
schema: 'We are unable to load the schema specification for the credential.',
attribute: 'The received credential is missing one or more required attributes ({{ attributeKeys }})',
},
};

Expand Down

0 comments on commit cda5a3e

Please sign in to comment.