Skip to content

Commit 39ff3a8

Browse files
committed
chore: address feedback
Signed-off-by: Timo Glastra <[email protected]>
1 parent 89f2765 commit 39ff3a8

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

packages/core/src/crypto/JwsService.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,15 @@ export class JwsService {
211211
}
212212

213213
private buildProtected(options: JwsProtectedHeaderOptions) {
214-
const x5cJwkCount = [options.jwk, options.x5c].filter(Boolean).length
215-
216214
// FIXME: checking for kid starting with '#' is not good.
217215
// but now we don't really limit that kid (did key reference)
218216
// cannot be combined with x5c/jwk
219217
if (options.kid?.startsWith('did:')) {
220-
if (x5cJwkCount > 0) {
218+
if (options.jwk || options.x5c) {
221219
throw new CredoError("When 'kid' is a did, 'jwk' and 'x5c' cannot be provided.")
222220
}
223-
} else if (x5cJwkCount > 1 || (x5cJwkCount === 0 && !options.kid)) {
224-
throw new CredoError("Header must contain one of 'kid' with a did value, 'x5c', or 'jwk'.")
221+
} else if ((options.jwk && options.x5c) || (!options.jwk && !options.x5c && !options.kid)) {
222+
throw new CredoError("Header must contain one of 'x5c', 'jwk' or 'kid' with a did value.")
225223
}
226224

227225
return {
@@ -250,17 +248,15 @@ export class JwsService {
250248
trustedCertificates: trustedCertificatesFromOptions = [],
251249
} = options
252250

253-
const x5cJwkCount = [protectedHeader.jwk, protectedHeader.x5c].filter(Boolean)
254-
255251
// FIXME: checking for kid starting with '#' is not good.
256252
// but now we don't really limit that kid (did key reference)
257253
// cannot be combined with x5c/jwk
258254
if (typeof protectedHeader.kid === 'string' && protectedHeader.kid?.startsWith('did:')) {
259-
if (x5cJwkCount.length > 0) {
255+
if (protectedHeader.jwk || protectedHeader.x5c) {
260256
throw new CredoError("When 'kid' is a did, 'jwk' and 'x5c' cannot be provided.")
261257
}
262-
} else if (x5cJwkCount.length > 1) {
263-
throw new CredoError("Header must contain one of 'kid' with a did value, 'x5c', or 'jwk'.")
258+
} else if (protectedHeader.jwk && protectedHeader.x5c) {
259+
throw new CredoError("Header must contain one of 'x5c', 'jwk' or 'kid' with a did value.")
264260
}
265261

266262
if (protectedHeader.x5c) {

packages/core/src/crypto/__tests__/JwsService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe('JwsService', () => {
172172
x5c: [didJwsz6MkfCertificate.toString('base64url')],
173173
},
174174
})
175-
).rejects.toThrow("Header must contain one of 'kid' with a did value, 'x5c', or 'jwk'.")
175+
).rejects.toThrow("Header must contain one of 'x5c', 'jwk' or 'kid' with a did value.")
176176

177177
await expect(
178178
jwsService.createJwsCompact(agentContext, {
@@ -279,7 +279,7 @@ describe('JwsService', () => {
279279
signature: '',
280280
},
281281
})
282-
).rejects.toThrow("Header must contain one of 'kid' with a did value, 'x5c', or 'jwk'.")
282+
).rejects.toThrow("Header must contain one of 'x5c', 'jwk' or 'kid' with a did value.")
283283

284284
await expect(
285285
jwsService.verifyJws(agentContext, {

0 commit comments

Comments
 (0)