Skip to content

Commit 072e83d

Browse files
committed
refactor: cleanup NODE-ED25519 workerd workarounds
Both workerd and the live service now support the Ed25519 and X25519 identifiers.
1 parent 9b234dd commit 072e83d

File tree

8 files changed

+22
-83
lines changed

8 files changed

+22
-83
lines changed

src/jwks/remote.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import fetchJwks from '../runtime/fetch_jwks.js'
2-
import { isCloudflareWorkers } from '../runtime/env.js'
32

43
import type { KeyLike, JWSHeaderParameters, FlattenedJWSInput } from '../types.d'
54
import { JWKSInvalid, JWKSNoMatchingKey } from '../util/errors.js'
65

76
import { isJWKSLike, LocalJWKSet } from './local.js'
87

8+
function isCloudflareWorkers() {
9+
return (
10+
// @ts-ignore
11+
typeof WebSocketPair !== 'undefined' ||
12+
// @ts-ignore
13+
(typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
14+
// @ts-ignore
15+
(typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel')
16+
)
17+
}
18+
919
/** Options for the remote JSON Web Key Set. */
1020
export interface RemoteJWKSetOptions {
1121
/**

src/lib/crypto_key.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { isCloudflareWorkers } from '../runtime/env.js'
2-
31
function unusable(name: string | number, prop = 'algorithm.name') {
42
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`)
53
}
@@ -73,10 +71,6 @@ export function checkSigCryptoKey(key: CryptoKey, alg: string, ...usages: KeyUsa
7371
}
7472
case 'EdDSA': {
7573
if (key.algorithm.name !== 'Ed25519' && key.algorithm.name !== 'Ed448') {
76-
if (isCloudflareWorkers()) {
77-
if (isAlgorithm(key.algorithm, 'NODE-ED25519')) break
78-
throw unusable('Ed25519, Ed448, or NODE-ED25519')
79-
}
8074
throw unusable('Ed25519 or Ed448')
8175
}
8276
break

src/runtime/browser/asn1.ts

+7-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isCloudflareWorkers } from './env.js'
21
import crypto, { isCryptoKey } from './webcrypto.js'
32
import type { PEMExportFunction, PEMImportFunction } from '../interfaces.d'
43
import invalidKeyInput from '../../lib/invalid_key_input.js'
@@ -143,31 +142,13 @@ const genericImport = async (
143142
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value')
144143
}
145144

146-
try {
147-
return await crypto.subtle.importKey(
148-
keyFormat,
149-
keyData,
150-
algorithm,
151-
options?.extractable ?? false,
152-
keyUsages,
153-
)
154-
} catch (err) {
155-
if (
156-
algorithm.name === 'Ed25519' &&
157-
(<Error>err)?.name === 'NotSupportedError' &&
158-
isCloudflareWorkers()
159-
) {
160-
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
161-
return await crypto.subtle.importKey(
162-
keyFormat,
163-
keyData,
164-
algorithm,
165-
options?.extractable ?? false,
166-
keyUsages,
167-
)
168-
}
169-
throw err
170-
}
145+
return crypto.subtle.importKey(
146+
keyFormat,
147+
keyData,
148+
algorithm,
149+
options?.extractable ?? false,
150+
keyUsages,
151+
)
171152
}
172153

173154
export const fromPKCS8: PEMImportFunction = (pem, alg, options?) => {

src/runtime/browser/env.ts

-10
This file was deleted.

src/runtime/browser/generate.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isCloudflareWorkers } from './env.js'
21
import crypto from './webcrypto.js'
32
import { JOSENotSupported } from '../../util/errors.js'
43
import random from './random.js'
@@ -149,21 +148,7 @@ export async function generateKeyPair(alg: string, options?: GenerateKeyPairOpti
149148
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value')
150149
}
151150

152-
try {
153-
return <{ publicKey: CryptoKey; privateKey: CryptoKey }>(
154-
await crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
155-
)
156-
} catch (err) {
157-
if (
158-
algorithm.name === 'Ed25519' &&
159-
(<Error>err)?.name === 'NotSupportedError' &&
160-
isCloudflareWorkers()
161-
) {
162-
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
163-
return <{ publicKey: CryptoKey; privateKey: CryptoKey }>(
164-
await crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
165-
)
166-
}
167-
throw err
168-
}
151+
return <Promise<{ publicKey: CryptoKey; privateKey: CryptoKey }>>(
152+
crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages)
153+
)
169154
}

src/runtime/browser/jwk_to_key.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isCloudflareWorkers } from './env.js'
21
import crypto from './webcrypto.js'
32
import type { JWKImportFunction } from '../interfaces.d'
43
import { JOSENotSupported } from '../../util/errors.js'
@@ -150,18 +149,6 @@ const parse: JWKImportFunction = async (jwk: JWK): Promise<CryptoKey> => {
150149
const keyData: JWK = { ...jwk }
151150
delete keyData.alg
152151
delete keyData.use
153-
try {
154-
return await crypto.subtle.importKey('jwk', keyData, ...rest)
155-
} catch (err) {
156-
if (
157-
algorithm.name === 'Ed25519' &&
158-
(<Error>err)?.name === 'NotSupportedError' &&
159-
isCloudflareWorkers()
160-
) {
161-
rest[0] = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
162-
return await crypto.subtle.importKey('jwk', keyData, ...rest)
163-
}
164-
throw err
165-
}
152+
return crypto.subtle.importKey('jwk', keyData, ...rest)
166153
}
167154
export default parse

src/runtime/browser/subtle_dsa.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isCloudflareWorkers } from './env.js'
21
import { JOSENotSupported } from '../../util/errors.js'
32

43
export default function subtleDsa(alg: string, algorithm: KeyAlgorithm | EcKeyAlgorithm) {
@@ -22,10 +21,6 @@ export default function subtleDsa(alg: string, algorithm: KeyAlgorithm | EcKeyAl
2221
case 'ES512':
2322
return { hash, name: 'ECDSA', namedCurve: (<EcKeyAlgorithm>algorithm).namedCurve }
2423
case 'EdDSA':
25-
if (isCloudflareWorkers() && algorithm.name === 'NODE-ED25519') {
26-
return { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' }
27-
}
28-
2924
return { name: algorithm.name }
3025
default:
3126
throw new JOSENotSupported(

src/runtime/node/env.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)