Skip to content

Commit 19e525f

Browse files
committed
refactor: clarify when alg is used and required on key imports
1 parent 018dfab commit 19e525f

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

src/key/generate_key_pair.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface GenerateKeyPairOptions {
2424
modulusLength?: number
2525

2626
/**
27-
* (Web Cryptography API specific) The value to use as
27+
* (Only effective in Web Crypto API runtimes) The value to use as
2828
* [SubtleCrypto.generateKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey)
2929
* `extractable` argument. Default is false.
3030
*/
@@ -35,7 +35,7 @@ export interface GenerateKeyPairOptions {
3535
* Generates a private and a public key for a given JWA algorithm identifier. This can only generate
3636
* asymmetric key pairs. For symmetric secrets use the `generateSecret` function.
3737
*
38-
* Note: Under Web Cryptography API runtime the `privateKey` is generated with `extractable` set to
38+
* Note: Under Web Crypto API runtime the `privateKey` is generated with `extractable` set to
3939
* `false` by default.
4040
*
4141
* @example Usage

src/key/generate_secret.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { KeyLike } from '../types.d'
44

55
export interface GenerateSecretOptions {
66
/**
7-
* (Web Cryptography API specific) The value to use as
7+
* (Only effective in Web Crypto API runtimes) The value to use as
88
* [SubtleCrypto.generateKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey)
99
* `extractable` argument. Default is false.
1010
*/
@@ -14,8 +14,8 @@ export interface GenerateSecretOptions {
1414
/**
1515
* Generates a symmetric secret key for a given JWA algorithm identifier.
1616
*
17-
* Note: Under Web Cryptography API runtime the secret key is generated with `extractable` set to
18-
* `false` by default.
17+
* Note: Under Web Crypto API runtime the secret key is generated with `extractable` set to `false`
18+
* by default.
1919
*
2020
* @example Usage
2121
*

src/key/import.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { JWK, KeyLike } from '../types.d'
88

99
export interface PEMImportOptions {
1010
/**
11-
* (Web Cryptography API specific) The value to use as
11+
* (Only effective in Web Crypto API runtimes) The value to use as
1212
* [SubtleCrypto.importKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey)
1313
* `extractable` argument. Default is false.
1414
*/
@@ -32,7 +32,8 @@ export interface PEMImportOptions {
3232
* ```
3333
*
3434
* @param pem PEM-encoded SPKI string
35-
* @param alg JSON Web Algorithm identifier to be used with the imported key.
35+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
36+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
3637
*/
3738
export async function importSPKI(
3839
spki: string,
@@ -69,7 +70,8 @@ export async function importSPKI(
6970
* ```
7071
*
7172
* @param pem X.509 certificate string
72-
* @param alg JSON Web Algorithm identifier to be used with the imported key.
73+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
74+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
7375
*/
7476
export async function importX509(
7577
x509: string,
@@ -100,7 +102,8 @@ export async function importX509(
100102
* ```
101103
*
102104
* @param pem PEM-encoded PKCS#8 string
103-
* @param alg JSON Web Algorithm identifier to be used with the imported key.
105+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
106+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
104107
*/
105108
export async function importPKCS8(
106109
pkcs8: string,
@@ -145,8 +148,9 @@ export async function importPKCS8(
145148
* ```
146149
*
147150
* @param jwk JSON Web Key.
148-
* @param alg JSON Web Algorithm identifier to be used with the imported key. Default is the "alg"
149-
* property on the JWK.
151+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
152+
* with the imported key. Default is the "alg" property on the JWK, its presence is only enforced
153+
* in Web Crypto API runtimes.
150154
* @param octAsKeyObject Forces a symmetric key to be imported to a KeyObject or CryptoKey. Default
151155
* is true unless JWK "ext" (Extractable) is true.
152156
*/
@@ -161,10 +165,6 @@ export async function importJWK(
161165

162166
alg ||= jwk.alg
163167

164-
if (typeof alg !== 'string' || !alg) {
165-
throw new TypeError('"alg" argument is required when "jwk.alg" is not present')
166-
}
167-
168168
switch (jwk.kty) {
169169
case 'oct':
170170
if (typeof jwk.k !== 'string' || !jwk.k) {

src/runtime/browser/jwk_to_key.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function subtleMapping(jwk: JWK): {
132132
}
133133

134134
const parse: JWKImportFunction = async (jwk: JWK): Promise<CryptoKey> => {
135+
if (!jwk.alg) {
136+
throw new TypeError('"alg" argument is required when "jwk.alg" is not present')
137+
}
138+
135139
const { algorithm, keyUsages } = subtleMapping(jwk)
136140
const rest: [RsaHashedImportParams | EcKeyAlgorithm | Algorithm, boolean, KeyUsage[]] = [
137141
algorithm,

tap/jwk.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,28 @@ export default (QUnit: QUnit, lib: typeof jose) => {
8888
})
8989
}
9090
}
91+
92+
if (env.isNodeCrypto || env.isElectron) {
93+
test('alg argument and jwk.alg is ignored', async (t) => {
94+
const oct = {
95+
k: 'FyCq1CKBflh3I5gikEjpYrdOXllzxB_yc02za8ERknI',
96+
kty: 'oct',
97+
}
98+
await lib.importJWK(oct)
99+
t.ok(1)
100+
})
101+
} else {
102+
test('alg argument must be present if jwk does not have alg', async (t) => {
103+
const oct = {
104+
k: 'FyCq1CKBflh3I5gikEjpYrdOXllzxB_yc02za8ERknI',
105+
kty: 'oct',
106+
}
107+
await t.rejects(
108+
lib.importJWK(oct),
109+
'"alg" argument is required when "jwk.alg" is not present',
110+
)
111+
await lib.importJWK(oct, 'HS256')
112+
await lib.importJWK({ ...oct, alg: 'HS256' })
113+
})
114+
}
91115
}

test/jwk/jwk2key.test.mjs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@ test('JWK kty must be recognized', async (t) => {
3939
})
4040
})
4141

42-
test('alg argument must be present if jwk does not have alg', async (t) => {
43-
const oct = {
44-
k: 'FyCq1CKBflh3I5gikEjpYrdOXllzxB_yc02za8ERknI',
45-
kty: 'oct',
46-
}
47-
await t.throwsAsync(importJWK(oct), {
48-
instanceOf: TypeError,
49-
message: '"alg" argument is required when "jwk.alg" is not present',
50-
})
51-
await t.notThrowsAsync(importJWK(oct, 'HS256'))
52-
await t.notThrowsAsync(importJWK({ ...oct, alg: 'HS256' }))
53-
})
54-
5542
test('oct JWK must have "k"', async (t) => {
5643
await t.throwsAsync(importJWK({ kty: 'oct' }, 'HS256'), {
5744
instanceOf: TypeError,

0 commit comments

Comments
 (0)