-
Notifications
You must be signed in to change notification settings - Fork 23
/
mod.ts
283 lines (261 loc) · 8.2 KB
/
mod.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { decodeBase64Url, encodeBase64Url } from "./deps.ts";
import {
create as createSignature,
verify as verifySignature,
} from "./signature.ts";
import { type Algorithm, verify as verifyAlgorithm } from "./algorithm.ts";
import {
decoder,
encoder,
isArray,
isDefined,
isNotNumber,
isNotString,
isNotTrue,
isNumber,
isObject,
isString,
isUndefined,
} from "./util.ts";
/**
* JWT §1: JWTs encode claims to be transmitted as a JSON [RFC7159] object [...].
* JWT §4.1: The following Claim Names are registered in the IANA
* "JSON Web Token Claims" registry established by Section 10.1. None of the
* claims defined below are intended to be mandatory to use or implement in all
* cases, but rather they provide a starting point for a set of useful,
* interoperable claims.
* Applications using JWTs should define which specific claims they use and when
* they are required or optional.
*/
export interface Payload {
iss?: string;
sub?: string;
aud?: string[] | string;
exp?: number;
nbf?: number;
iat?: number;
jti?: string;
[key: string]: unknown;
}
/**
* JWS §4.1.1: The "alg" value is a case-sensitive ASCII string containing a
* StringOrURI value. This Header Parameter MUST be present and MUST be
* understood and processed by implementations.
*/
export interface Header {
alg: Algorithm;
[key: string]: unknown;
}
/**
* With `expLeeway` and `nbfLeeway` implementers may provide for some small
* leeway to account for clock skew (JWT §4.1.4). The default is 1 second.
* By passing the option `audience`, this application tries to identify the
* recipient with a value in the `aud` claim. If the values don't match, an
* `Error` is thrown.
*/
export type VerifyOptions = {
expLeeway?: number;
nbfLeeway?: number;
ignoreExp?: boolean;
ignoreNbf?: boolean;
audience?: string | string[] | RegExp;
predicates?: (<P extends Payload>(payload: P) => boolean)[];
};
function isExpired(exp: number, leeway: number): boolean {
return exp + leeway < Date.now() / 1000;
}
function isTooEarly(nbf: number, leeway: number): boolean {
return nbf - leeway > Date.now() / 1000;
}
function is3Tuple(arr: unknown[]): arr is [unknown, unknown, Uint8Array] {
return arr.length === 3;
}
function hasInvalidTimingClaims(...claimValues: unknown[]): boolean {
return claimValues.some((claimValue) =>
isDefined(claimValue) && isNotNumber(claimValue)
);
}
export function validateTimingClaims(
payload: Payload,
{ expLeeway = 1, nbfLeeway = 1, ignoreExp, ignoreNbf }: VerifyOptions = {},
): void {
if (hasInvalidTimingClaims(payload.exp, payload.nbf)) {
throw new Error(`The jwt has an invalid 'exp' or 'nbf' claim.`);
}
if (
isNumber(payload.exp) && isNotTrue(ignoreExp) &&
isExpired(payload.exp, expLeeway)
) {
throw RangeError("The jwt is expired.");
}
if (
isNumber(payload.nbf) && isNotTrue(ignoreNbf) &&
isTooEarly(payload.nbf, nbfLeeway)
) {
throw RangeError("The jwt is used too early.");
}
}
function hasValidAudClaim(claimValue: unknown): claimValue is Payload["aud"] {
if (isUndefined(claimValue) || isString(claimValue)) return true;
else return isArray(claimValue) && claimValue.every(isString);
}
export function validateAudClaim(
aud: unknown,
audience: Required<VerifyOptions>["audience"],
): void {
if (hasValidAudClaim(aud)) {
if (isUndefined(aud)) {
throw new Error("The jwt has no 'aud' claim.");
}
const audArray = isString(aud) ? [aud] : aud;
const audienceArrayOrRegex = isString(audience) ? [audience] : audience;
if (
!audArray.some((audString) =>
isArray(audienceArrayOrRegex)
? audienceArrayOrRegex.includes(audString)
: audienceArrayOrRegex.test(audString)
)
) {
throw new Error(
"The identification with the value in the 'aud' claim has failed.",
);
}
} else {
throw new Error(`The jwt has an invalid 'aud' claim.`);
}
}
/**
* Takes a `jwt` and returns a 3-tuple `[unknown, unknown, Uint8Array]` if the
* jwt has a valid _serialization_. Otherwise it throws an `Error`. This function
* does **not** verify the digital signature.
*/
export function decode<PayloadType extends Payload | unknown = unknown>(
jwt: string,
): [unknown, PayloadType, Uint8Array] {
try {
const arr = jwt
.split(".")
.map(decodeBase64Url)
.map((uint8Array, index) =>
index === 0 || index === 1
? JSON.parse(decoder.decode(uint8Array))
: uint8Array
);
if (is3Tuple(arr)) return arr as [unknown, PayloadType, Uint8Array];
else throw new Error();
} catch {
throw Error("The serialization of the jwt is invalid.");
}
}
/** It does **not** verify the digital signature. */
export function validate(
// deno-lint-ignore no-explicit-any
[header, payload, signature]: [any, any, Uint8Array],
options?: VerifyOptions,
): {
header: Header;
payload: Payload;
signature: Uint8Array;
} {
if (isNotString(header?.alg)) {
throw new Error(`The jwt's 'alg' header parameter value must be a string.`);
}
/*
* JWT §7.2: Verify that the resulting octet sequence is a UTF-8-encoded
* representation of a completely valid JSON object conforming to RFC 7159;
* let the JWT Claims Set be this JSON object.
*/
if (isObject(payload)) {
validateTimingClaims(payload, options);
if (isDefined(options?.audience)) {
validateAudClaim(payload.aud, options!.audience);
}
return {
header,
payload,
signature,
};
} else {
throw new Error(`The jwt claims set is not a JSON object.`);
}
}
/**
* Takes jwt, `CryptoKey` and `VerifyOptions` and returns the `Payload` of the
* jwt if the jwt is valid. Otherwise it throws an `Error`.
*/
export async function verify<PayloadType extends Payload>(
jwt: string,
key: CryptoKey | null,
options?: VerifyOptions,
): Promise<PayloadType> {
const { header, payload, signature } = validate(decode(jwt), options);
if (verifyAlgorithm(header.alg, key)) {
if (
!(await verifySignature(
signature,
key,
header.alg,
jwt.slice(0, jwt.lastIndexOf(".")),
))
) {
throw new Error(
"The jwt's signature does not match the verification signature.",
);
}
if (!(options?.predicates || []).every((predicate) => predicate(payload))) {
throw new Error("The payload does not satisfy all passed predicates.");
}
return payload as PayloadType;
} else {
throw new Error(
`The jwt's alg '${header.alg}' does not match the key's algorithm.`,
);
}
}
/**
* JWT §3: JWTs represent a set of claims as a JSON object that is encoded in
* a JWS and/or JWE structure. This JSON object is the JWT Claims Set.
* JSW §7.1: The JWS Compact Serialization represents digitally signed or MACed
* content as a compact, URL-safe string. This string is:
* BASE64URL(UTF8(JWS Protected Header)) || '.' ||
* BASE64URL(JWS Payload) || '.' ||
* BASE64URL(JWS Signature)
*/
function createSigningInput(header: Header, payload: Payload): string {
return `${encodeBase64Url(encoder.encode(JSON.stringify(header)))}.${
encodeBase64Url(encoder.encode(JSON.stringify(payload)))
}`;
}
/**
* Takes `Header`, `Payload` and `CryptoKey` and returns the url-safe encoded
* jwt.
*/
export async function create(
header: Header,
payload: Payload,
key: CryptoKey | null,
): Promise<string> {
if (isObject(payload)) {
if (verifyAlgorithm(header.alg, key)) {
const signingInput = createSigningInput(header, payload);
const signature = await createSignature(header.alg, key, signingInput);
return `${signingInput}.${signature}`;
} else {
throw new Error(
`The jwt's alg '${header.alg}' does not match the key's algorithm.`,
);
}
} else {
throw new Error(`The jwt claims set is not a JSON object.`);
}
}
/**
* This helper function simplifies setting a `NumericDate`. It takes either a
* `Date` object or a `number` (in seconds) and returns the `number` of seconds
* from 1970-01-01T00:00:00Z UTC until the specified UTC date/time.
*/
export function getNumericDate(exp: number | Date): number {
return Math.round(
(exp instanceof Date ? exp.getTime() : Date.now() + exp * 1000) / 1000,
);
}