deno.land / x / jose@v5.2.4 / lib / check_key_type.ts

check_key_type.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { withAlg as invalidKeyInput } from './invalid_key_input.ts'import isKeyLike, { types } from '../runtime/is_key_like.ts'
const symmetricTypeCheck = (alg: string, key: unknown) => { if (key instanceof Uint8Array) return
if (!isKeyLike(key)) { throw new TypeError(invalidKeyInput(alg, key, ...types, 'Uint8Array')) }
if (key.type !== 'secret') { throw new TypeError( `${types.join(' or ')} instances for symmetric algorithms must be of type "secret"`, ) }}
const asymmetricTypeCheck = (alg: string, key: unknown, usage: string) => { if (!isKeyLike(key)) { throw new TypeError(invalidKeyInput(alg, key, ...types)) }
if (key.type === 'secret') { throw new TypeError( `${types.join(' or ')} instances for asymmetric algorithms must not be of type "secret"`, ) }
if (usage === 'sign' && key.type === 'public') { throw new TypeError( `${types.join(' or ')} instances for asymmetric algorithm signing must be of type "private"`, ) }
if (usage === 'decrypt' && key.type === 'public') { throw new TypeError( `${types.join( ' or ', )} instances for asymmetric algorithm decryption must be of type "private"`, ) }
// KeyObject allows this but CryptoKey does not. if ((<CryptoKey>key).algorithm && usage === 'verify' && key.type === 'private') { throw new TypeError( `${types.join(' or ')} instances for asymmetric algorithm verifying must be of type "public"`, ) }
// KeyObject allows this but CryptoKey does not. if ((<CryptoKey>key).algorithm && usage === 'encrypt' && key.type === 'private') { throw new TypeError( `${types.join( ' or ', )} instances for asymmetric algorithm encryption must be of type "public"`, ) }}
const checkKeyType = (alg: string, key: unknown, usage: string): void => { const symmetric = alg.startsWith('HS') || alg === 'dir' || alg.startsWith('PBES2') || /^A\d{3}(?:GCM)?KW$/.test(alg)
if (symmetric) { symmetricTypeCheck(alg, key) } else { asymmetricTypeCheck(alg, key, usage) }}
export default checkKeyType
jose

Version Info

Tagged at
a month ago