diff --git a/packages/browser-wallet/CHANGELOG.md b/packages/browser-wallet/CHANGELOG.md index a609f8b9..edf3f56f 100644 --- a/packages/browser-wallet/CHANGELOG.md +++ b/packages/browser-wallet/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added +- Validation of required attributes when adding a credential. - Display contract address of issuer in verifiable credential details. ### Fixed diff --git a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx index 59caea2d..c33e0baa 100644 --- a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx +++ b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx @@ -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( diff --git a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/da.ts b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/da.ts index a7498ace..06607d84 100644 --- a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/da.ts +++ b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/da.ts @@ -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, }, }; diff --git a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/en.ts b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/en.ts index d9c6fe68..c4c243d0 100644 --- a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/en.ts +++ b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/i18n/en.ts @@ -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 }})', }, };