-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
key_to_jwk.ts
25 lines (23 loc) · 808 Bytes
/
key_to_jwk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import crypto, { isCryptoKey } from './webcrypto.js'
import type { JWKConvertFunction } from '../interfaces.d'
import type { JWK } from '../../types.d'
import invalidKeyInput from './invalid_key_input.js'
import { encode as base64url } from './base64url.js'
const keyToJWK: JWKConvertFunction = async (key: unknown): Promise<JWK> => {
if (key instanceof Uint8Array) {
return {
kty: 'oct',
k: base64url(key),
}
}
if (!isCryptoKey(key)) {
throw new TypeError(invalidKeyInput(key, 'CryptoKey', 'Uint8Array'))
}
if (!key.extractable) {
throw new TypeError('non-extractable CryptoKey cannot be exported as a JWK')
}
// @deno-expect-error
const { ext, key_ops, alg, use, ...jwk } = await crypto.subtle.exportKey('jwk', key)
return <JWK>jwk
}
export default keyToJWK