diff --git a/packages/ssi-types/__tests__/wrapped-claims.test.ts b/packages/ssi-types/__tests__/wrapped-claims.test.ts index 995ea6762..c829cad6a 100644 --- a/packages/ssi-types/__tests__/wrapped-claims.test.ts +++ b/packages/ssi-types/__tests__/wrapped-claims.test.ts @@ -1,5 +1,13 @@ import * as fs from 'fs' -import { CredentialMapper, ICredential, IVerifiableCredential, IVerifiablePresentation, OriginalType, WrappedVerifiablePresentation } from '../src' +import { + CredentialMapper, + ICredential, + ICredentialSubject, + IVerifiableCredential, + IVerifiablePresentation, + OriginalType, + WrappedVerifiablePresentation, +} from '../src' function getFile(path: string) { return fs.readFileSync(path, 'utf-8') @@ -14,9 +22,7 @@ describe('Wrapped VC claims', () => { const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0] jwtVc['exp' as keyof IVerifiableCredential] = (+new Date()).toString() const vc = CredentialMapper.toWrappedVerifiableCredential(jwtVc) - expect(vc.credential.credentialSubject.expirationDate).toEqual( - new Date(parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string)).toISOString() - ) + expect(vc.credential.expirationDate).toEqual(new Date(parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string)).toISOString()) }) it('should set expiration date if exp is present in JWT vc as number', () => { @@ -24,19 +30,20 @@ describe('Wrapped VC claims', () => { jwtVc['exp' as keyof IVerifiableCredential] = new Date().valueOf() const vc = CredentialMapper.toWrappedVerifiableCredential(jwtVc) - expect(vc.credential.credentialSubject.expirationDate).toEqual(new Date(jwtVc['exp' as keyof IVerifiableCredential] as string).toISOString()) + expect(vc.credential.expirationDate).toEqual(new Date(jwtVc['exp' as keyof IVerifiableCredential] as string).toISOString()) }) it('should throw an error if expiration date and exp are different in JWT vc', () => { const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0] + const subject = jwtVc['vc' as keyof IVerifiableCredential].credentialSubject jwtVc['exp' as keyof IVerifiableCredential] = (+new Date()).toString() - ;(jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate = (+new Date( + ;(jwtVc['vc' as keyof IVerifiableCredential]).expirationDate = (+new Date( (jwtVc['exp' as keyof IVerifiableCredential] as string) + 2 )).toString() expect(() => CredentialMapper.toWrappedVerifiableCredential(jwtVc)).toThrowError( `Inconsistent expiration dates between JWT claim (${new Date( parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string) - ).toISOString()}) and VC value (${(jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate})` + ).toISOString()}) and VC value (${(jwtVc['vc' as keyof IVerifiableCredential]).expirationDate})` ) }) @@ -81,18 +88,18 @@ describe('Wrapped VC claims', () => { it('should set credentialSubject.id if sub is present in JWT vc', () => { const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0] - jwtVc['sub' as keyof IVerifiableCredential] = (jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id + const subject = jwtVc.vc.credentialSubject + jwtVc['sub' as keyof IVerifiableCredential] = subject.id const vc = CredentialMapper.toWrappedVerifiableCredential(jwtVc) - expect(vc.credential.credentialSubject.id).toEqual(jwtVc['sub' as keyof IVerifiableCredential]) + expect(!Array.isArray(vc.credential.credentialSubject) && vc.credential.credentialSubject.id).toEqual(jwtVc['sub' as keyof IVerifiableCredential]) }) it('should throw an error if credentialSubject.id and sub are different in JWT vc', () => { const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0] jwtVc['sub' as keyof IVerifiableCredential] = 'did:test:123' + const subject = jwtVc.vc.credentialSubject expect(() => CredentialMapper.toWrappedVerifiableCredential(jwtVc)).toThrowError( - `Inconsistent credential subject ids between JWT claim (${jwtVc['sub' as keyof IVerifiableCredential]}) and VC value (${ - (jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id - })` + `Inconsistent credential subject ids between JWT claim (${jwtVc['sub' as keyof IVerifiableCredential]}) and VC value (${subject.id})` ) }) diff --git a/packages/ssi-types/src/mapper/credential-mapper.ts b/packages/ssi-types/src/mapper/credential-mapper.ts index 39f609383..9b962188a 100644 --- a/packages/ssi-types/src/mapper/credential-mapper.ts +++ b/packages/ssi-types/src/mapper/credential-mapper.ts @@ -188,20 +188,15 @@ export class CredentialMapper { // Since this is a credential, we delete any proof just to be sure (should not occur on JWT, but better safe than sorry) delete credential.proof - const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject] if (decoded.exp) { - for (let i = 0; i < subjects.length; i++) { - const expDate = subjects[i].expirationDate - const jwtExp = parseInt(decoded.exp.toString()) - // fix seconds to millisecs for the date - const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString() - if (expDate && expDate !== expDateAsStr) { - throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`) - } - Array.isArray(credential.credentialSubject) - ? (credential.credentialSubject[i].expirationDate = expDateAsStr) - : (credential.credentialSubject.expirationDate = expDateAsStr) + const expDate = credential.expirationDate + const jwtExp = parseInt(decoded.exp.toString()) + // fix seconds to millisecs for the date + const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString() + if (expDate && expDate !== expDateAsStr) { + throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`) } + credential.expirationDate = expDateAsStr } if (decoded.nbf) { @@ -232,6 +227,7 @@ export class CredentialMapper { } if (decoded.sub) { + const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject] for (let i = 0; i < subjects.length; i++) { const csId = subjects[i].id if (csId && csId !== decoded.sub) {