Skip to content

Commit

Permalink
fix: Tests to allow multiple subjects for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Sep 28, 2022
1 parent 6300ccc commit 110d78e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
31 changes: 19 additions & 12 deletions packages/ssi-types/__tests__/wrapped-claims.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -14,29 +22,28 @@ 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', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]

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 = <ICredentialSubject>jwtVc['vc' as keyof IVerifiableCredential].credentialSubject
jwtVc['exp' as keyof IVerifiableCredential] = (+new Date()).toString()
;(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate = (+new Date(
;(<ICredential>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 (${(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate})`
).toISOString()}) and VC value (${(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).expirationDate})`
)
})

Expand Down Expand Up @@ -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] = (<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id
const subject = <ICredentialSubject>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 = <ICredentialSubject>jwtVc.vc.credentialSubject
expect(() => CredentialMapper.toWrappedVerifiableCredential(jwtVc)).toThrowError(
`Inconsistent credential subject ids between JWT claim (${jwtVc['sub' as keyof IVerifiableCredential]}) and VC value (${
(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id
})`
`Inconsistent credential subject ids between JWT claim (${jwtVc['sub' as keyof IVerifiableCredential]}) and VC value (${subject.id})`
)
})

Expand Down
20 changes: 8 additions & 12 deletions packages/ssi-types/src/mapper/credential-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 110d78e

Please sign in to comment.