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

feat: Pass issuer to verifyJWS #41

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
exports[`atTime before DID available for v0 1`] = `"did:3:kjzl6cwe1jw14ah8wjy8grgzl52sl18sbyirgi9bqy9yzu28kbxvjmhip99r14k?version-id=0#7wYNHm3nGoNA3Kv"`;

exports[`atTime ok before rotation 1`] = `"did:3:kjzl6cwe1jw14ah8wjy8grgzl52sl18sbyirgi9bqy9yzu28kbxvjmhip99r14k?version-id=0#7wYNHm3nGoNA3Kv"`;

exports[`issuer includes signer as controller 1`] = `"did:3:kjzl6cwe1jw14ah8wjy8grgzl52sl18sbyirgi9bqy9yzu28kbxvjmhip99r14k?version-id=0#7wYNHm3nGoNA3Kv"`;

exports[`issuer same as signer 1`] = `"did:3:kjzl6cwe1jw14ah8wjy8grgzl52sl18sbyirgi9bqy9yzu28kbxvjmhip99r14k?version-id=0#7wYNHm3nGoNA3Kv"`;
56 changes: 56 additions & 0 deletions src/__tests__/jws-verification-behavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ const VERSION_NEXT = {
},
}

const COMPOSITE_ISSUER_EMPTY = {
didResolutionMetadata: {
contentType: 'application/did+json',
},
didDocument: {
id: 'did:composite:foo',
controller: [],
},
didDocumentMetadata: {},
}

// v0: did:3:kjzl6cwe1jw14ah8wjy8grgzl52sl18sbyirgi9bqy9yzu28kbxvjmhip99r14k?version-id=0#7wYNHm3nGoNA3Kv
const jwsV0 =
'eyJraWQiOiJkaWQ6MzpranpsNmN3ZTFqdzE0YWg4d2p5OGdyZ3psNTJzbDE4c2J5aXJnaTlicXk5eXp1MjhrYnh2am1oaXA5OXIxNGs_dmVyc2lvbi1pZD0wIzd3WU5IbTNuR29OQTNLdiIsImFsZyI6IkVTMjU2SyJ9.eyJoZWxsbyI6IndvcmxkIn0.cHVXU7QW2pvBJpVIBCLhnkCQ0k4Up3cNRqiyeryRbPOSrZdAoQmWy1OfzNFzgY90nol26KJxHWnknSyu5sY__Q'
Expand Down Expand Up @@ -217,3 +228,48 @@ describe('atTime', () => {
expect(kid).toMatchSnapshot()
})
})

describe('issuer', () => {
const SIGNER_DID = VERSION_0_VANILLA.didDocument.id
const did = new DID()

beforeEach(() => {
did.resolve = jest.fn((didUrl: string) => {
if (didUrl === COMPOSITE_ISSUER_EMPTY.didDocument.id) {
return Promise.resolve(COMPOSITE_ISSUER_EMPTY)
}
return Promise.resolve(VERSION_0_VANILLA)
})
})

test('same as signer', async () => {
const { kid } = await did.verifyJWS(jwsV0, { issuer: SIGNER_DID })
expect(kid).toMatchSnapshot()
})
test('includes signer as controller', async () => {
const fauxResolve = jest.fn((didUrl: string) => {
if (didUrl === COMPOSITE_ISSUER_EMPTY.didDocument.id) {
const included = {
...COMPOSITE_ISSUER_EMPTY,
didDocument: {
...COMPOSITE_ISSUER_EMPTY.didDocument,
controller: [SIGNER_DID],
},
}
return Promise.resolve(included)
}
return Promise.resolve(VERSION_0_VANILLA)
})
did.resolve = fauxResolve
const issuer = COMPOSITE_ISSUER_EMPTY.didDocument.id
const { kid } = await did.verifyJWS(jwsV0, { issuer: issuer })
expect(kid).toMatchSnapshot()
expect(fauxResolve).toBeCalledWith(issuer)
})
test('does not include signer as controller', async () => {
const issuer = COMPOSITE_ISSUER_EMPTY.didDocument.id
await expect(did.verifyJWS(jwsV0, { issuer: issuer })).rejects.toThrow(
/invalid_jws: not a valid verificationMethod for issuer/
)
})
})
19 changes: 19 additions & 0 deletions src/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
decodeBase64,
encodeBase64Url,
randomString,
didWithTime,
extractControllers,
} from './utils'

export interface AuthenticateOptions {
Expand Down Expand Up @@ -46,6 +48,11 @@ export interface VerifyJWSOptions {
* If true, timestamp checking is disabled.
*/
disableTimecheck?: boolean

/**
* DID that issued the signature.
*/
issuer?: string
}

export interface VerifyJWSResult {
Expand Down Expand Up @@ -236,6 +243,18 @@ export class DID {
}
}

const signerDid = didResolutionResult.didDocument?.id
if (options.issuer && options.issuer !== signerDid) {
const issuerUrl = didWithTime(options.issuer, options.atTime)
Comment on lines +246 to +248
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe break this and the timecheckEnabled conditional into helper functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I would like to accumulate a little more weirdness like this, and extract verifyJWS into own verification service. It clearly does not belong here as a method of DID object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, ideally most of this logic could move into the did-jwt package. Which btw should be renamed did-jose at some point: decentralized-identity/did-jwt#170

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really want to accumulate weirdness here nevertheless to guide the further changes.

const issuerResolution = await this.resolve(issuerUrl)
const controllerProperty = issuerResolution.didDocument?.controller
const controllers = extractControllers(controllerProperty)
const signerIsController = signerDid ? controllers.includes(signerDid) : false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, ok I see why now 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhums :)

if (!signerIsController) {
throw new Error(`invalid_jws: not a valid verificationMethod for issuer: ${kid}`)
}
}

const publicKeys = didResolutionResult.didDocument?.verificationMethod || []
// verifyJWS will throw an error if the signature is invalid
verifyJWS(jws, publicKeys)
Expand Down
31 changes: 31 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,34 @@ export function fromDagJWS(jws: DagJWS): string {
if (jws.signatures.length > 1) throw new Error('Cant convert to compact jws')
return `${jws.signatures[0].protected}.${jws.payload}.${jws.signatures[0].signature}`
}

/**
* Make DID URL from DID and timestamp (= versionTime query)
*/
export function didWithTime(did: string, atTime?: number): string {
if (atTime) {
const versionTime = new Date(atTime).toISOString().split('.')[0] + 'Z'
return `${did}?versionTime=${versionTime}`
} else {
return did
}
}

/**
* `controller` field of DID Document can be one or multiple strings, if defined.
* Here we transform it into array of strings.
* Potentially it can be an empty array.
*/
export function extractControllers(
controllerProperty: string | Array<string> | undefined
): Array<string> {
if (controllerProperty) {
if (Array.isArray(controllerProperty)) {
return controllerProperty
} else {
return [controllerProperty]
}
} else {
return []
}
}