Skip to content

Commit

Permalink
fix: mapp additional W3C spec fields to and from JWT VC (#75)
Browse files Browse the repository at this point in the history
* Explicitly map `credentialStatus`, `termsOfUse` and `evidence` between between JWT and W3C VC representations.

* tests: extended test coverage

fixes #73
  • Loading branch information
rado0x54 authored May 28, 2021
1 parent a87d5cc commit e0482dc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
67 changes: 67 additions & 0 deletions src/__tests__/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,38 @@ describe('credential', () => {
})
})

describe('other W3C fields', () => {
it('uses evidence from vc', () => {
const result = normalizeCredential({ vc: { evidence: 'foo'} })
expect(result).toMatchObject({ evidence: 'foo' })
})

it('uses evidence from vc, keeping originals', () => {
const result = normalizeCredential({ vc: { evidence: 'foo' } }, false)
expect(result).toMatchObject({ evidence: 'foo', vc: { evidence: 'foo'} })
})

it('uses credentialStatus from vc', () => {
const result = normalizeCredential({ vc: { credentialStatus: 'foo'} })
expect(result).toMatchObject({ credentialStatus: 'foo' })
})

it('uses credentialStatus from vc, keeping originals', () => {
const result = normalizeCredential({ vc: { credentialStatus: 'foo' } }, false)
expect(result).toMatchObject({ credentialStatus: 'foo', vc: { credentialStatus: 'foo'} })
})

it('uses termsOfUse from vc', () => {
const result = normalizeCredential({ vc: { termsOfUse: 'foo'} })
expect(result).toMatchObject({ termsOfUse: 'foo' })
})

it('uses termsOfUse from vc, keeping originals', () => {
const result = normalizeCredential({ vc: { termsOfUse: 'foo' } }, false)
expect(result).toMatchObject({ termsOfUse: 'foo', vc: { termsOfUse: 'foo'} })
})
})

describe('JWT payload', () => {
it('rejects unknown JSON string payload', () => {
expect(() => {
Expand Down Expand Up @@ -924,6 +956,41 @@ describe('credential', () => {
expect(input.issuer.id).toEqual('did:example:567')
})
})

describe('other fields W3C fields', () => {
it('maps evidence to vc', () => {
const result = transformCredentialInput({ evidence: 'foo' })
expect(result).toMatchObject({ vc: { evidence: 'foo' } })
expect(result).not.toHaveProperty('evidence')
})

it('maps evidence to vc, keeping originals', () => {
const result = transformCredentialInput({ evidence: 'foo' }, false)
expect(result).toMatchObject({ evidence: 'foo', vc: { evidence: 'foo' } })
})

it('maps credentialStatus to vc', () => {
const result = transformCredentialInput({ credentialStatus: 'foo' })
expect(result).toMatchObject({ vc: { credentialStatus: 'foo' } })
expect(result).not.toHaveProperty('credentialStatus')
})

it('maps credentialStatus to vc, keeping originals', () => {
const result = transformCredentialInput({ credentialStatus: 'foo' }, false)
expect(result).toMatchObject({ credentialStatus: 'foo', vc: { credentialStatus: 'foo' } })
})

it('maps termsOfUse to vc', () => {
const result = transformCredentialInput({ termsOfUse: 'foo' })
expect(result).toMatchObject({ vc: { termsOfUse: 'foo' } })
expect(result).not.toHaveProperty('termsOfUse')
})

it('maps termsOfUse to vc, keeping originals', () => {
const result = transformCredentialInput({ termsOfUse: 'foo' }, false)
expect(result).toMatchObject({ termsOfUse: 'foo', vc: { termsOfUse: 'foo' } })
})
})
})
})

Expand Down
36 changes: 34 additions & 2 deletions src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ function normalizeJwtCredentialPayload(
delete result.vc?.type
}

result.evidence = input.vc?.evidence
if (removeOriginalFields) {
delete result.vc?.evidence
}

result.credentialStatus = input.vc?.credentialStatus
if (removeOriginalFields) {
delete result.vc?.credentialStatus
}

result.termsOfUse = input.vc?.termsOfUse
if (removeOriginalFields) {
delete result.vc?.termsOfUse
}

const contextArray: string[] = [
...asArray(input.context),
...asArray(input['@context']),
Expand Down Expand Up @@ -236,9 +251,10 @@ export function transformCredentialInput(
delete credentialSubject.id
}
}
result.vc.credentialSubject = credentialSubject

result.vc.credentialStatus = input.credentialStatus
if (removeOriginalFields) {
delete result.credentialSubject
delete result.credentialStatus
}

const contextEntries = [
Expand Down Expand Up @@ -304,6 +320,22 @@ export function transformCredentialInput(
}
}

// additional W3C VC fields to map:
result.vc.credentialSubject = credentialSubject
if (removeOriginalFields) {
delete result.credentialSubject
}

result.vc.evidence = input.evidence
if (removeOriginalFields) {
delete result.evidence
}

result.vc.termsOfUse = input.termsOfUse
if (removeOriginalFields) {
delete result.termsOfUse
}

return result as JwtCredentialPayload
}

Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface JwtCredentialPayload {
'@context': string[] | string
type: string[] | string
credentialSubject: JwtCredentialSubject
credentialStatus?: CredentialStatus
evidence?: any
termsOfUse?: any
[x: string]: any
}
nbf?: number
Expand Down Expand Up @@ -66,6 +69,8 @@ interface FixedCredentialPayload {
[x: string]: any
}
credentialStatus?: CredentialStatus
evidence?: any
termsOfUse?: any
}

/**
Expand Down

0 comments on commit e0482dc

Please sign in to comment.