-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
verify.ts
186 lines (158 loc) · 5.38 KB
/
verify.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { decode as base64url } from '../../runtime/base64url.ts'
import verify from '../../runtime/verify.ts'
import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.ts'
import { concat, encoder, decoder } from '../../lib/buffer_utils.ts'
import isDisjoint from '../../lib/is_disjoint.ts'
import isObject from '../../lib/is_object.ts'
import checkKeyType from '../../lib/check_key_type.ts'
import validateCrit from '../../lib/validate_crit.ts'
import validateAlgorithms from '../../lib/validate_algorithms.ts'
import type {
FlattenedVerifyResult,
KeyLike,
FlattenedJWSInput,
JWSHeaderParameters,
VerifyOptions,
GetKeyFunction,
ResolvedKey,
} from '../../types.d.ts'
/**
* Interface for Flattened JWS Verification dynamic key resolution. No token components have been
* verified at the time of this function call.
*
* @see [createRemoteJWKSet](../functions/jwks_remote.createRemoteJWKSet.md#function-createremotejwkset) to verify using a remote JSON Web Key Set.
*/
export interface FlattenedVerifyGetKey
extends GetKeyFunction<JWSHeaderParameters | undefined, FlattenedJWSInput> {}
/**
* Verifies the signature and format of and afterwards decodes the Flattened JWS.
*
* @param jws Flattened JWS.
* @param key Key to verify the JWS with. See
* {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
* @param options JWS Verify options.
*/
export function flattenedVerify(
jws: FlattenedJWSInput,
key: KeyLike | Uint8Array,
options?: VerifyOptions,
): Promise<FlattenedVerifyResult>
/**
* @param jws Flattened JWS.
* @param getKey Function resolving a key to verify the JWS with. See
* {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
* @param options JWS Verify options.
*/
export function flattenedVerify<T extends KeyLike = KeyLike>(
jws: FlattenedJWSInput,
getKey: FlattenedVerifyGetKey,
options?: VerifyOptions,
): Promise<FlattenedVerifyResult & ResolvedKey<T>>
export async function flattenedVerify(
jws: FlattenedJWSInput,
key: KeyLike | Uint8Array | FlattenedVerifyGetKey,
options?: VerifyOptions,
) {
if (!isObject(jws)) {
throw new JWSInvalid('Flattened JWS must be an object')
}
if (jws.protected === undefined && jws.header === undefined) {
throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members')
}
if (jws.protected !== undefined && typeof jws.protected !== 'string') {
throw new JWSInvalid('JWS Protected Header incorrect type')
}
if (jws.payload === undefined) {
throw new JWSInvalid('JWS Payload missing')
}
if (typeof jws.signature !== 'string') {
throw new JWSInvalid('JWS Signature missing or incorrect type')
}
if (jws.header !== undefined && !isObject(jws.header)) {
throw new JWSInvalid('JWS Unprotected Header incorrect type')
}
let parsedProt: JWSHeaderParameters = {}
if (jws.protected) {
try {
const protectedHeader = base64url(jws.protected)
parsedProt = JSON.parse(decoder.decode(protectedHeader))
} catch {
throw new JWSInvalid('JWS Protected Header is invalid')
}
}
if (!isDisjoint(parsedProt, jws.header)) {
throw new JWSInvalid(
'JWS Protected and JWS Unprotected Header Parameter names must be disjoint',
)
}
const joseHeader: JWSHeaderParameters = {
...parsedProt,
...jws.header,
}
const extensions = validateCrit(
JWSInvalid,
new Map([['b64', true]]),
options?.crit,
parsedProt,
joseHeader,
)
let b64: boolean = true
if (extensions.has('b64')) {
b64 = parsedProt.b64!
if (typeof b64 !== 'boolean') {
throw new JWSInvalid(
'The "b64" (base64url-encode payload) Header Parameter must be a boolean',
)
}
}
const { alg } = joseHeader
if (typeof alg !== 'string' || !alg) {
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid')
}
const algorithms = options && validateAlgorithms('algorithms', options.algorithms)
if (algorithms && !algorithms.has(alg)) {
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed')
}
if (b64) {
if (typeof jws.payload !== 'string') {
throw new JWSInvalid('JWS Payload must be a string')
}
} else if (typeof jws.payload !== 'string' && !(jws.payload instanceof Uint8Array)) {
throw new JWSInvalid('JWS Payload must be a string or an Uint8Array instance')
}
let resolvedKey = false
if (typeof key === 'function') {
key = await key(parsedProt, jws)
resolvedKey = true
}
checkKeyType(alg, key, 'verify')
const data = concat(
encoder.encode(jws.protected ?? ''),
encoder.encode('.'),
typeof jws.payload === 'string' ? encoder.encode(jws.payload) : jws.payload,
)
const signature = base64url(jws.signature)
const verified = await verify(alg, key, signature, data)
if (!verified) {
throw new JWSSignatureVerificationFailed()
}
let payload: Uint8Array
if (b64) {
payload = base64url(jws.payload)
} else if (typeof jws.payload === 'string') {
payload = encoder.encode(jws.payload)
} else {
payload = jws.payload
}
const result: FlattenedVerifyResult = { payload }
if (jws.protected !== undefined) {
result.protectedHeader = parsedProt
}
if (jws.header !== undefined) {
result.unprotectedHeader = jws.header
}
if (resolvedKey) {
return { ...result, key }
}
return result
}