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

Fix checkCredential should check if JWT #472

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
38 changes: 20 additions & 18 deletions packages/credential-sdk/src/vc/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function checkCredentialJSONLD(credential) {
* @param {object} credential - An object that could be a VerifiableCredential.
* @throws {Error}
*/
export function checkCredentialRequired(credential) {
export function checkCredentialRequired(credential, isJWT) {
// Ensure first context is DEFAULT_CONTEXT_V1_URL
if (credential['@context'][0] !== DEFAULT_CONTEXT_V1_URL) {
throw new Error(
Expand All @@ -186,21 +186,23 @@ export function checkCredentialRequired(credential) {
throw new Error('"credentialSubject" property is required.');
}

// Ensure issuer is valid
const issuer = getId(credential.issuer);
if (!issuer) {
throw new Error(
`"issuer" must be an object with ID property or a string. Got: ${credential.issuer}`,
);
} else if (!issuer.includes(':')) {
throw new Error('"issuer" id must be in URL format.');
}
// Ensure issuer and issue date is valid, only for non-jwt as that is defined in the header
if (!isJWT) {
const issuer = getId(credential.issuer);
if (!issuer) {
throw new Error(
`"issuer" must be an object with ID property or a string. Got: ${credential.issuer}`,
);
} else if (!issuer.includes(':')) {
throw new Error('"issuer" id must be in URL format.');
}

// Ensure there is an issuance date, if exists
if (!credential.issuanceDate) {
throw new Error('"issuanceDate" property is required.');
} else {
ensureValidDatetime(credential.issuanceDate);
// Ensure there is an issuance date, if exists
if (!credential.issuanceDate) {
throw new Error('"issuanceDate" property is required.');
} else {
ensureValidDatetime(credential.issuanceDate);
}
}
}

Expand Down Expand Up @@ -229,8 +231,8 @@ export function checkCredentialOptional(credential) {
* @param {object} credential - An object that could be a VerifiableCredential.
* @throws {Error}
*/
export function checkCredential(credential) {
checkCredentialRequired(credential);
export function checkCredential(credential, isJWT) {
checkCredentialRequired(credential, isJWT);
checkCredentialOptional(credential);
checkCredentialJSONLD(credential);
}
Expand Down Expand Up @@ -295,7 +297,7 @@ export async function verifyCredential(
}

// Check credential is valid
checkCredential(credential);
checkCredential(credential, isJWT);

// Check expiration date
if (verifyDates && 'expirationDate' in credential) {
Expand Down
Loading