Skip to content

Commit

Permalink
fix: respect JWK ext for symmetric keys
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 7, 2022
1 parent 84b56ee commit 20557fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/key/import.ts
Expand Up @@ -261,7 +261,7 @@ export async function importJWK(
octAsKeyObject ??= jwk.ext !== true

if (octAsKeyObject) {
return asKeyObject({ ...jwk, alg, ext: false })
return asKeyObject({ ...jwk, alg, ext: jwk.ext ?? false })
}

return decodeBase64URL(jwk.k)
Expand Down
19 changes: 16 additions & 3 deletions test/jwk/jwk2key.test.mjs
Expand Up @@ -80,6 +80,13 @@ test('oct JWK (ext: true)', async (t) => {
196, 31, 242, 115, 77, 179, 107, 193, 17, 146, 114,
],
)

const k = await importJWK(oct, 'HS256', true)
t.true('type' in k)
t.is(k.type, 'secret')
if ('extractable' in k) {
t.is(k.extractable, true)
}
})

test('oct JWK (ext: false)', async (t) => {
Expand All @@ -89,10 +96,13 @@ test('oct JWK (ext: false)', async (t) => {
ext: false,
}

const k = await importJWK(oct, 'HS256')
const k = await importJWK(oct, 'HS256', true)

t.true('type' in k)
t.is(k.type, 'secret')
if ('extractable' in k) {
t.is(k.extractable, false)
}
})

test('oct JWK (ext missing)', async (t) => {
Expand All @@ -101,10 +111,13 @@ test('oct JWK (ext missing)', async (t) => {
kty: 'oct',
}

const k = await importJWK(oct, 'HS256')
const k = await importJWK(oct, 'HS256', true)

t.true('type' in k)
t.is(k.type, 'secret')
if ('extractable' in k) {
t.is(k.extractable, false)
}
})

async function testKeyImportExport(t, jwk) {
Expand Down Expand Up @@ -221,7 +234,7 @@ test('Uin8tArray can be transformed to a JWK', async (t) => {
)
})

conditional({ webcrypto: 0 })('secret key object can be transformed to a JWK', async (t) => {
test('secret KeyLike can be transformed to a JWK', async (t) => {
const keylike = await importJWK(
{
ext: true,
Expand Down

0 comments on commit 20557fc

Please sign in to comment.