nish 0.35.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/crypto/jwt.ts
CHANGED
|
@@ -4,28 +4,38 @@
|
|
|
4
4
|
* RFC 7518 (the HS, RS and ES algorithms), RFC 8037 (EdDSA) and the RFC 8725
|
|
5
5
|
* best current practice.
|
|
6
6
|
*
|
|
7
|
-
* import {
|
|
7
|
+
* import { Secret } from "nish:secret";
|
|
8
|
+
* import { JWT_ALG_HS256, JWT_MAX_TOKEN, JWT_OK, JwtKey, JwtVerifier, jwtHmacKey, jwtSignString } from "nish/crypto/jwt";
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
* const
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
10
|
+
* // secretKey: at least 32 random bytes, still the caller's to wipe; now: seconds since 1970
|
|
11
|
+
* const roundTrip = (secretKey: Secret<u8[]>, token: string, now: i64): string | null => {
|
|
12
|
+
* const key: JwtKey | null = jwtHmacKey(JWT_ALG_HS256, secretKey); // make once; null for a short key
|
|
13
|
+
* if (key === null) {
|
|
14
|
+
* return null;
|
|
15
|
+
* }
|
|
16
|
+
* const v = new JwtVerifier(JWT_MAX_TOKEN); // make once per thread
|
|
17
|
+
* v.issuer = "https://issuer.example";
|
|
18
|
+
* v.audience = "api";
|
|
19
|
+
* v.leeway = toI64(30);
|
|
20
|
+
* if (v.verifyString(key, token, now) === JWT_OK) {
|
|
21
|
+
* // v.claims[0 .. v.claimsLen) is the payload's JSON; v.exp, v.nbf, v.iat are read
|
|
22
|
+
* }
|
|
23
|
+
* const minted: string | null = jwtSignString(key, "{\"sub\":\"ana\"}");
|
|
24
|
+
* key.wipe();
|
|
25
|
+
* return minted;
|
|
26
|
+
* };
|
|
18
27
|
*
|
|
19
28
|
* **The key decides the algorithm, never the token.** A `JwtKey` is made for
|
|
20
29
|
* one algorithm, and a token verifies only when its header's `alg` is that
|
|
21
|
-
* algorithm's name, spelled exactly. `none` names no key, so a token that
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
30
|
+
* algorithm's name, spelled exactly. `none` names no key, so a token that says
|
|
31
|
+
* it is unsigned is refused like any other mismatch (`JWT_ERR_ALG`); an `HS256`
|
|
32
|
+
* header in front of an RSA key is refused before any MAC is taken, so an RSA
|
|
33
|
+
* public key can never be used as an HMAC secret (RFC 8725 §2.1, §3.1).
|
|
34
|
+
*
|
|
35
|
+
* The header must hold `alg` exactly once, as a string; `typ` and `kid` at most
|
|
36
|
+
* once, as strings; and no `crit` and no `b64` (RFC 7515 §4.1.11, RFC 7797),
|
|
37
|
+
* since this verifier understands no extension and a header that names one must
|
|
38
|
+
* be refused. Other header members are skipped.
|
|
29
39
|
*
|
|
30
40
|
* **Nothing in the payload is read before the signature holds.** The verifier
|
|
31
41
|
* reads the header, decodes the signature and checks it, and only then decodes
|
|
@@ -167,7 +177,12 @@ const JWT_MAX_LEEWAY: i64 = 2147483647
|
|
|
167
177
|
const JWT_MAX_NOW: i64 = 1073741824 * 1073741824 * 4
|
|
168
178
|
const JWT_DOT: i32 = 46
|
|
169
179
|
|
|
170
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* The `alg` header value for `alg`, `""` for no algorithm this module knows.
|
|
182
|
+
*
|
|
183
|
+
* @param alg A `JWT_ALG_` constant.
|
|
184
|
+
* @returns The name RFC 7518 or RFC 8037 spells it with, such as `"HS256"`.
|
|
185
|
+
*/
|
|
171
186
|
export const jwtAlgName = (alg: i32): string => {
|
|
172
187
|
if (alg === JWT_ALG_HS256) {
|
|
173
188
|
return "HS256"
|
|
@@ -236,21 +251,35 @@ const jwtLength = (a: u8[]): i32 => toI32(a.length)
|
|
|
236
251
|
* each worker its own.
|
|
237
252
|
*/
|
|
238
253
|
export class JwtKey {
|
|
239
|
-
/** An HS key's pads, keyed once; `null` for any other. */
|
|
254
|
+
/** @internal An HS key's pads, keyed once; `null` for any other. */
|
|
240
255
|
readonly hmac: HmacKey | null
|
|
241
|
-
/** An RS key; `null` for any other. */
|
|
256
|
+
/** @internal An RS key; `null` for any other. */
|
|
242
257
|
readonly rsa: RsaPublicKey | null
|
|
243
|
-
/** An ES key's uncompressed point or an EdDSA key's 32 bytes; empty for HS and RS. */
|
|
258
|
+
/** @internal An ES key's uncompressed point or an EdDSA key's 32 bytes; empty for HS and RS. */
|
|
244
259
|
readonly pub: u8[]
|
|
245
|
-
/** An ES256 or EdDSA signing key's private scalar or seed; empty for any other. */
|
|
260
|
+
/** @internal An ES256 or EdDSA signing key's private scalar or seed; empty for any other. */
|
|
246
261
|
readonly priv: Secret<u8[]>
|
|
247
|
-
/** The one algorithm this key verifies, `JWT_ALG_*`. */
|
|
262
|
+
/** @internal The one algorithm this key verifies, `JWT_ALG_*`. */
|
|
248
263
|
readonly alg: i32
|
|
249
|
-
/** Whether `jwtSign` signs with this key. */
|
|
264
|
+
/** @internal Whether `jwtSign` signs with this key. */
|
|
250
265
|
readonly canSign: boolean
|
|
251
|
-
/** Whether the key is in use: `wipe()` clears it, and a wiped key signs and verifies nothing. */
|
|
266
|
+
/** @internal Whether the key is in use: `wipe()` clears it, and a wiped key signs and verifies nothing. */
|
|
252
267
|
live: boolean
|
|
253
268
|
|
|
269
|
+
/**
|
|
270
|
+
* A key from its parts, checked for nothing: the `jwt…Key` functions are
|
|
271
|
+
* the way to make one, and this exists for them and for tests that build a
|
|
272
|
+
* key no function would. `hmac`, `rsa` and `pub` are kept as given; `priv`
|
|
273
|
+
* is copied, and stays the caller's to wipe.
|
|
274
|
+
*
|
|
275
|
+
* @param alg The one algorithm the key verifies, a `JWT_ALG_` constant.
|
|
276
|
+
* @param hmac An HS key's keyed pads, or `null`.
|
|
277
|
+
* @param rsa An RS key, or `null`.
|
|
278
|
+
* @param pub An ES key's uncompressed point or an EdDSA key's 32 bytes;
|
|
279
|
+
* empty for any other.
|
|
280
|
+
* @param priv An ES256 private scalar or an EdDSA seed; empty for any other.
|
|
281
|
+
* @param canSign Whether `jwtSign` signs with the key.
|
|
282
|
+
*/
|
|
254
283
|
constructor(
|
|
255
284
|
alg: i32,
|
|
256
285
|
hmac: HmacKey | null,
|
|
@@ -301,6 +330,10 @@ const jwtPublicKey = (
|
|
|
301
330
|
* a key at least as long as the hash, so a shorter one answers `null`, as does
|
|
302
331
|
* an `alg` that is not HS. The pads are keyed once, here; `key` stays the
|
|
303
332
|
* caller's to wipe.
|
|
333
|
+
*
|
|
334
|
+
* @param alg `JWT_ALG_HS256`, `JWT_ALG_HS384` or `JWT_ALG_HS512`.
|
|
335
|
+
* @param key The shared secret: at least 32, 48 or 64 bytes for the three.
|
|
336
|
+
* @returns A new key that verifies and signs, or `null`.
|
|
304
337
|
*/
|
|
305
338
|
export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
|
|
306
339
|
let hash: i32 = 0
|
|
@@ -327,6 +360,10 @@ export const jwtHmacKey = (alg: i32, key: Secret<u8[]>): JwtKey | null => {
|
|
|
327
360
|
* An RS256, RS384 or RS512 key from the DER of a PKCS#1 `RSAPublicKey`
|
|
328
361
|
* (`rsaParsePublicKey`'s input). A key that does not parse, a modulus below
|
|
329
362
|
* 2048 bits (RFC 7518 §3.3) or an `alg` that is not RS answers `null`.
|
|
363
|
+
*
|
|
364
|
+
* @param alg `JWT_ALG_RS256`, `JWT_ALG_RS384` or `JWT_ALG_RS512`.
|
|
365
|
+
* @param der The public key's DER, nothing before it and nothing after it.
|
|
366
|
+
* @returns A new key that verifies only, or `null`.
|
|
330
367
|
*/
|
|
331
368
|
export const jwtRsaKey = (alg: i32, der: u8[]): JwtKey | null => {
|
|
332
369
|
if (alg !== JWT_ALG_RS256 && alg !== JWT_ALG_RS384 && alg !== JWT_ALG_RS512) {
|
|
@@ -378,6 +415,11 @@ const jwtDerInteger = (out: u8[], v: u8[]): void => {
|
|
|
378
415
|
* An RS key from its big-endian modulus and exponent, as a JWK's `n` and `e`
|
|
379
416
|
* spell them once base64url-decoded (RFC 7518 §6.3.1). The same checks as
|
|
380
417
|
* `jwtRsaKey`; parts too long for any key this module takes answer `null`.
|
|
418
|
+
*
|
|
419
|
+
* @param alg `JWT_ALG_RS256`, `JWT_ALG_RS384` or `JWT_ALG_RS512`.
|
|
420
|
+
* @param n The modulus, big-endian; leading zero bytes are dropped.
|
|
421
|
+
* @param e The public exponent, big-endian; leading zero bytes are dropped.
|
|
422
|
+
* @returns A new key that verifies only, or `null`.
|
|
381
423
|
*/
|
|
382
424
|
export const jwtRsaKeyParts = (alg: i32, n: u8[], e: u8[]): JwtKey | null => {
|
|
383
425
|
if (toI32(n.length) > 2048 || toI32(e.length) > 8 || toI32(n.length) === 0 || toI32(e.length) === 0) {
|
|
@@ -398,6 +440,10 @@ export const jwtRsaKeyParts = (alg: i32, n: u8[], e: u8[]): JwtKey | null => {
|
|
|
398
440
|
* An ES256 or ES384 verifying key from its uncompressed point, `04 ‖ x ‖ y`
|
|
399
441
|
* (65 or 97 bytes). A point of the wrong length or form answers `null`; one
|
|
400
442
|
* off the curve makes every verification fail.
|
|
443
|
+
*
|
|
444
|
+
* @param alg `JWT_ALG_ES256` or `JWT_ALG_ES384`.
|
|
445
|
+
* @param point The public point, copied into the key.
|
|
446
|
+
* @returns A new key that verifies only, or `null`.
|
|
401
447
|
*/
|
|
402
448
|
export const jwtEcKey = (alg: i32, point: u8[]): JwtKey | null => {
|
|
403
449
|
let size: i32 = 0
|
|
@@ -412,7 +458,13 @@ export const jwtEcKey = (alg: i32, point: u8[]): JwtKey | null => {
|
|
|
412
458
|
return jwtPublicKey(alg, null, null, jwtCopy(point), false)
|
|
413
459
|
}
|
|
414
460
|
|
|
415
|
-
/**
|
|
461
|
+
/**
|
|
462
|
+
* An EdDSA (Ed25519) verifying key from its 32 bytes (RFC 8037 §2's `x`);
|
|
463
|
+
* another length answers `null`.
|
|
464
|
+
*
|
|
465
|
+
* @param pub The public key, copied into the key.
|
|
466
|
+
* @returns A new key that verifies only, or `null`.
|
|
467
|
+
*/
|
|
416
468
|
export const jwtEd25519Key = (pub: u8[]): JwtKey | null => {
|
|
417
469
|
if (toI32(pub.length) !== 32) {
|
|
418
470
|
return null
|
|
@@ -424,6 +476,9 @@ export const jwtEd25519Key = (pub: u8[]): JwtKey | null => {
|
|
|
424
476
|
* An ES256 key that signs and verifies, from its 32-byte private scalar. A
|
|
425
477
|
* scalar `p256PublicKey` refuses answers `null`. The key keeps a copy;
|
|
426
478
|
* `priv` stays the caller's to wipe.
|
|
479
|
+
*
|
|
480
|
+
* @param priv The private scalar, big-endian.
|
|
481
|
+
* @returns A new key that signs and verifies, or `null`.
|
|
427
482
|
*/
|
|
428
483
|
export const jwtEs256SigningKey = (priv: Secret<u8[]>): JwtKey | null => {
|
|
429
484
|
const pub: u8[] | null = p256PublicKey(priv)
|
|
@@ -437,6 +492,9 @@ export const jwtEs256SigningKey = (priv: Secret<u8[]>): JwtKey | null => {
|
|
|
437
492
|
* An EdDSA key that signs and verifies, from its 32-byte seed (RFC 8037 §2's
|
|
438
493
|
* `d`). A seed of another length answers `null`. The key keeps a copy; `seed`
|
|
439
494
|
* stays the caller's to wipe.
|
|
495
|
+
*
|
|
496
|
+
* @param seed The private seed.
|
|
497
|
+
* @returns A new key that signs and verifies, or `null`.
|
|
440
498
|
*/
|
|
441
499
|
export const jwtEd25519SigningKey = (seed: Secret<u8[]>): JwtKey | null => {
|
|
442
500
|
const pub: u8[] | null = ed25519PublicKey(seed)
|
|
@@ -612,22 +670,22 @@ const jwtUtf8Byte = (cp: i32, width: i32, k: i32): i32 => {
|
|
|
612
670
|
*/
|
|
613
671
|
class JwtJson {
|
|
614
672
|
/** The current integer's value, negated, when `integral` holds. */
|
|
615
|
-
value: i64
|
|
616
|
-
src: u8[]
|
|
617
|
-
readonly stack: u8[]
|
|
618
|
-
pos: i32
|
|
619
|
-
end: i32
|
|
620
|
-
depth: i32
|
|
621
|
-
want: i32
|
|
622
|
-
kind: i32
|
|
623
|
-
failed: i32
|
|
673
|
+
private value: i64
|
|
674
|
+
private src: u8[]
|
|
675
|
+
private readonly stack: u8[]
|
|
676
|
+
private pos: i32
|
|
677
|
+
private end: i32
|
|
678
|
+
private depth: i32
|
|
679
|
+
private want: i32
|
|
680
|
+
private kind: i32
|
|
681
|
+
private failed: i32
|
|
624
682
|
/** The current token's bytes, `src[tokStart, tokEnd)`; a string's and a key's include their quotes. */
|
|
625
|
-
tokStart: i32
|
|
626
|
-
tokEnd: i32
|
|
683
|
+
private tokStart: i32
|
|
684
|
+
private tokEnd: i32
|
|
627
685
|
/** The current key's or string's length once decoded. */
|
|
628
|
-
textBytes: i32
|
|
686
|
+
private textBytes: i32
|
|
629
687
|
/** Whether the current number has no fraction or exponent and fits an `i64`. */
|
|
630
|
-
integral: boolean
|
|
688
|
+
private integral: boolean
|
|
631
689
|
|
|
632
690
|
/** A reader for values nested at most `maxDepth` containers deep; `reset` gives it a window. */
|
|
633
691
|
constructor(src: u8[], maxDepth: i32) {
|
|
@@ -646,7 +704,7 @@ class JwtJson {
|
|
|
646
704
|
this.integral = false
|
|
647
705
|
}
|
|
648
706
|
|
|
649
|
-
/** Starts over on `src[off, off + len)`; a window outside `src` fails the first `next()`. */
|
|
707
|
+
/** @internal Starts over on `src[off, off + len)`; a window outside `src` fails the first `next()`. */
|
|
650
708
|
reset(src: u8[], off: i32, len: i32): void {
|
|
651
709
|
this.src = src
|
|
652
710
|
this.pos = off
|
|
@@ -659,19 +717,19 @@ class JwtJson {
|
|
|
659
717
|
}
|
|
660
718
|
|
|
661
719
|
/** The byte at `i`, or `-1` past the window. */
|
|
662
|
-
at(i: i32): i32 {
|
|
720
|
+
private at(i: i32): i32 {
|
|
663
721
|
return i >= 0 && i < this.end && i < toI32(this.src.length) ? toI32(this.src[i]) : -1
|
|
664
722
|
}
|
|
665
723
|
|
|
666
724
|
/** Fails the reader for good. */
|
|
667
|
-
fail(): i32 {
|
|
725
|
+
private fail(): i32 {
|
|
668
726
|
this.failed = JWT_JSON_ERR
|
|
669
727
|
this.kind = JWT_JSON_ERR
|
|
670
728
|
return JWT_JSON_ERR
|
|
671
729
|
}
|
|
672
730
|
|
|
673
731
|
/** Steps over the four blanks RFC 8259 §2 allows. */
|
|
674
|
-
skipBlanks(): void {
|
|
732
|
+
private skipBlanks(): void {
|
|
675
733
|
let b: i32 = this.at(this.pos)
|
|
676
734
|
while (b === JWT_CH_SPACE || b === JWT_CH_TAB || b === JWT_CH_NEWLINE || b === JWT_CH_RETURN) {
|
|
677
735
|
this.pos += 1
|
|
@@ -679,7 +737,7 @@ class JwtJson {
|
|
|
679
737
|
}
|
|
680
738
|
}
|
|
681
739
|
|
|
682
|
-
/** The token after the current one, or `JWT_JSON_ERR`, answered again on every later call. */
|
|
740
|
+
/** @internal The token after the current one, or `JWT_JSON_ERR`, answered again on every later call. */
|
|
683
741
|
next(): i32 {
|
|
684
742
|
if (this.failed !== 0) {
|
|
685
743
|
return this.failed
|
|
@@ -715,7 +773,7 @@ class JwtJson {
|
|
|
715
773
|
}
|
|
716
774
|
|
|
717
775
|
/** `b`, the closer of the innermost container, popped. */
|
|
718
|
-
readClose(b: i32): i32 {
|
|
776
|
+
private readClose(b: i32): i32 {
|
|
719
777
|
if (this.depth === 0) {
|
|
720
778
|
return this.fail()
|
|
721
779
|
}
|
|
@@ -733,12 +791,12 @@ class JwtJson {
|
|
|
733
791
|
}
|
|
734
792
|
|
|
735
793
|
/** What follows a complete value: nothing at the top, else a comma or a closer. */
|
|
736
|
-
afterValue(): void {
|
|
794
|
+
private afterValue(): void {
|
|
737
795
|
this.want = this.depth === 0 ? JWT_JSON_WANT_NOTHING : JWT_JSON_WANT_COMMA_OR_CLOSE
|
|
738
796
|
}
|
|
739
797
|
|
|
740
798
|
/** A member's name, its blanks and its colon. */
|
|
741
|
-
readKey(): i32 {
|
|
799
|
+
private readKey(): i32 {
|
|
742
800
|
if (this.at(this.pos) !== JWT_CH_QUOTE || !this.scanString()) {
|
|
743
801
|
return this.fail()
|
|
744
802
|
}
|
|
@@ -753,7 +811,7 @@ class JwtJson {
|
|
|
753
811
|
}
|
|
754
812
|
|
|
755
813
|
/** A value at the cursor: an opener, a string, a number or a literal. */
|
|
756
|
-
readValue(): i32 {
|
|
814
|
+
private readValue(): i32 {
|
|
757
815
|
const b: i32 = this.at(this.pos)
|
|
758
816
|
if (b === JWT_CH_OPEN_BRACE || b === JWT_CH_OPEN_BRACKET) {
|
|
759
817
|
if (this.depth >= toI32(this.stack.length)) {
|
|
@@ -786,7 +844,7 @@ class JwtJson {
|
|
|
786
844
|
}
|
|
787
845
|
|
|
788
846
|
/** Whether `word` is at the cursor, stepped over if so. */
|
|
789
|
-
scanWord(word: string): boolean {
|
|
847
|
+
private scanWord(word: string): boolean {
|
|
790
848
|
const n: i32 = toI32(word.length)
|
|
791
849
|
for (let i: i32 = 0; i < n; i += 1) {
|
|
792
850
|
if (this.at(this.pos + i) !== toI32(word.charCodeAt(i))) {
|
|
@@ -800,7 +858,7 @@ class JwtJson {
|
|
|
800
858
|
}
|
|
801
859
|
|
|
802
860
|
/** The digits at the cursor, accumulated negated into `value`; how many, and `integral` cleared on overflow. */
|
|
803
|
-
scanDigits(bound: i64): i32 {
|
|
861
|
+
private scanDigits(bound: i64): i32 {
|
|
804
862
|
const ten: i64 = toI64(10)
|
|
805
863
|
let n: i32 = 0
|
|
806
864
|
let b: i32 = this.at(this.pos)
|
|
@@ -826,7 +884,7 @@ class JwtJson {
|
|
|
826
884
|
* `i64` for its sign; accumulating negated reaches -2^63, where a positive
|
|
827
885
|
* number stops at 2^63 - 1.
|
|
828
886
|
*/
|
|
829
|
-
scanNumber(): boolean {
|
|
887
|
+
private scanNumber(): boolean {
|
|
830
888
|
this.tokStart = this.pos
|
|
831
889
|
const negative: boolean = this.at(this.pos) === JWT_CH_MINUS
|
|
832
890
|
if (negative) {
|
|
@@ -871,7 +929,7 @@ class JwtJson {
|
|
|
871
929
|
* The code point of the escape at `i` (a backslash), a surrogate pair
|
|
872
930
|
* joined, or `-1` for one RFC 8259 §7 refuses or a lone surrogate.
|
|
873
931
|
*/
|
|
874
|
-
escapeCodePoint(i: i32): i32 {
|
|
932
|
+
private escapeCodePoint(i: i32): i32 {
|
|
875
933
|
const c: i32 = this.at(i + 1)
|
|
876
934
|
if (c === JWT_CH_QUOTE || c === JWT_CH_BACKSLASH || c === 47) {
|
|
877
935
|
return c
|
|
@@ -913,7 +971,7 @@ class JwtJson {
|
|
|
913
971
|
}
|
|
914
972
|
|
|
915
973
|
/** The four hex digits at `i` as a number, or `-1`. */
|
|
916
|
-
hex4(i: i32): i32 {
|
|
974
|
+
private hex4(i: i32): i32 {
|
|
917
975
|
let v: i32 = 0
|
|
918
976
|
for (let k: i32 = 0; k < 4; k += 1) {
|
|
919
977
|
const d: i32 = jwtHexValue(this.at(i + k))
|
|
@@ -926,7 +984,7 @@ class JwtJson {
|
|
|
926
984
|
}
|
|
927
985
|
|
|
928
986
|
/** How many bytes the escape at `i` takes, given the code point it decodes to. */
|
|
929
|
-
escapeLength(i: i32, cp: i32): i32 {
|
|
987
|
+
private escapeLength(i: i32, cp: i32): i32 {
|
|
930
988
|
if (this.at(i + 1) !== 117) {
|
|
931
989
|
return 2
|
|
932
990
|
}
|
|
@@ -938,7 +996,7 @@ class JwtJson {
|
|
|
938
996
|
* RFC 3629 §4 (no overlong form, no surrogate, nothing past U+10FFFF), or
|
|
939
997
|
* `-1`.
|
|
940
998
|
*/
|
|
941
|
-
utf8Tail(i: i32): i32 {
|
|
999
|
+
private utf8Tail(i: i32): i32 {
|
|
942
1000
|
const b: i32 = this.at(i)
|
|
943
1001
|
let n: i32 = 0
|
|
944
1002
|
let lo: i32 = 0x80
|
|
@@ -970,7 +1028,7 @@ class JwtJson {
|
|
|
970
1028
|
}
|
|
971
1029
|
|
|
972
1030
|
/** The string at the cursor, checked, stepped over, its decoded length in `textBytes`. */
|
|
973
|
-
scanString(): boolean {
|
|
1031
|
+
private scanString(): boolean {
|
|
974
1032
|
this.tokStart = this.pos
|
|
975
1033
|
let i: i32 = this.pos + 1
|
|
976
1034
|
let decoded: i32 = 0
|
|
@@ -1009,11 +1067,11 @@ class JwtJson {
|
|
|
1009
1067
|
}
|
|
1010
1068
|
|
|
1011
1069
|
/** Whether the current token is a key or a string. */
|
|
1012
|
-
isText(): boolean {
|
|
1070
|
+
private isText(): boolean {
|
|
1013
1071
|
return this.kind === JWT_JSON_KEY || this.kind === JWT_JSON_STRING
|
|
1014
1072
|
}
|
|
1015
1073
|
|
|
1016
|
-
/** The current key's or string's length once decoded, or `-1`. */
|
|
1074
|
+
/** @internal The current key's or string's length once decoded, or `-1`. */
|
|
1017
1075
|
stringLength(): i32 {
|
|
1018
1076
|
return this.isText() ? this.textBytes : -1
|
|
1019
1077
|
}
|
|
@@ -1023,6 +1081,7 @@ class JwtJson {
|
|
|
1023
1081
|
* `false` when the token is neither. Under Node, `charCodeAt` is a UTF-16
|
|
1024
1082
|
* code unit, so only an ASCII name answers as it does natively, as with
|
|
1025
1083
|
* `nish/json-tokenizer`'s `keyEquals`.
|
|
1084
|
+
* @internal
|
|
1026
1085
|
*/
|
|
1027
1086
|
keyEquals(name: string): boolean {
|
|
1028
1087
|
const n: i32 = toI32(name.length)
|
|
@@ -1059,6 +1118,7 @@ class JwtJson {
|
|
|
1059
1118
|
* Writes the current key's or string's decoded bytes to `out` from `at` and
|
|
1060
1119
|
* answers how many; `-1`, writing nothing, when the token is neither or
|
|
1061
1120
|
* `out` has too little room.
|
|
1121
|
+
* @internal
|
|
1062
1122
|
*/
|
|
1063
1123
|
stringInto(out: u8[], at: i32): i32 {
|
|
1064
1124
|
if (!this.isText() || at < 0 || at > toI32(out.length) - this.textBytes) {
|
|
@@ -1090,12 +1150,12 @@ class JwtJson {
|
|
|
1090
1150
|
return o - at
|
|
1091
1151
|
}
|
|
1092
1152
|
|
|
1093
|
-
/** Whether the current token is a number without fraction or exponent that fits an `i64`. */
|
|
1153
|
+
/** @internal Whether the current token is a number without fraction or exponent that fits an `i64`. */
|
|
1094
1154
|
isInteger(): boolean {
|
|
1095
1155
|
return this.kind === JWT_JSON_NUMBER && this.integral
|
|
1096
1156
|
}
|
|
1097
1157
|
|
|
1098
|
-
/** The current number as an `i64` when `isInteger()` holds, else `0`. */
|
|
1158
|
+
/** @internal The current number as an `i64` when `isInteger()` holds, else `0`. */
|
|
1099
1159
|
numberI64(): i64 {
|
|
1100
1160
|
if (!this.isInteger()) {
|
|
1101
1161
|
return toI64(0)
|
|
@@ -1107,6 +1167,7 @@ class JwtJson {
|
|
|
1107
1167
|
* Steps over the value the current token starts: after a key, its value;
|
|
1108
1168
|
* after an opener, everything through its closer; after a scalar or a
|
|
1109
1169
|
* closer, nothing. Answers the kind of the last token read, or `JWT_JSON_ERR`.
|
|
1170
|
+
* @internal
|
|
1110
1171
|
*/
|
|
1111
1172
|
skipValue(): i32 {
|
|
1112
1173
|
if (this.failed !== 0) {
|
|
@@ -1188,6 +1249,8 @@ const jwtVerifyPublic = (key: JwtKey, src: u8[], off: i32, len: i32, sig: u8[],
|
|
|
1188
1249
|
* fields, read on every `verify`. After `JWT_OK`, `claims[0 .. claimsLen)`
|
|
1189
1250
|
* is the payload's JSON for the caller's own claims, and `exp`, `nbf` and
|
|
1190
1251
|
* `iat` hold the time claims when `hasExp`, `hasNbf` and `hasIat` say so.
|
|
1252
|
+
* `claims` and `sig` are the verifier's own buffers, not copies: the next
|
|
1253
|
+
* `verify` overwrites them.
|
|
1191
1254
|
*/
|
|
1192
1255
|
export class JwtVerifier {
|
|
1193
1256
|
/** Seconds of clock skew allowed to `exp`, `nbf` and `iat`; held to `[0, 2^31 - 1]`. */
|
|
@@ -1202,23 +1265,46 @@ export class JwtVerifier {
|
|
|
1202
1265
|
typ: string | null
|
|
1203
1266
|
/** The decoded payload of the last token, `claimsLen` bytes of it valid after `JWT_OK`. */
|
|
1204
1267
|
readonly claims: u8[]
|
|
1268
|
+
/** The last token's `exp`, in seconds since 1970; meaningful only when `hasExp`. */
|
|
1205
1269
|
exp: i64
|
|
1270
|
+
/** The last token's `nbf`, in seconds since 1970; meaningful only when `hasNbf`. */
|
|
1206
1271
|
nbf: i64
|
|
1272
|
+
/** The last token's `iat`, in seconds since 1970; meaningful only when `hasIat`. */
|
|
1207
1273
|
iat: i64
|
|
1208
|
-
readonly part: u8[]
|
|
1274
|
+
private readonly part: u8[]
|
|
1275
|
+
/**
|
|
1276
|
+
* The last token's signature, decoded, at the front; `JWT_MAX_SIGNATURE`
|
|
1277
|
+
* bytes long, whatever the signature's own length. It is written only once
|
|
1278
|
+
* the header passes, so after a refusal before that it still holds an
|
|
1279
|
+
* earlier token's bytes.
|
|
1280
|
+
*/
|
|
1209
1281
|
readonly sig: u8[]
|
|
1210
|
-
readonly text: u8[]
|
|
1211
|
-
readonly tok: JwtJson
|
|
1282
|
+
private readonly text: u8[]
|
|
1283
|
+
private readonly tok: JwtJson
|
|
1212
1284
|
/** The largest token taken, in bytes. */
|
|
1213
1285
|
readonly maxToken: i32
|
|
1286
|
+
/** How many bytes of `claims` are the payload: its length after `JWT_OK`, 0 after any refusal. */
|
|
1214
1287
|
claimsLen: i32
|
|
1215
1288
|
/** Whether a token without `exp` is refused; on by default. */
|
|
1216
1289
|
requireExp: boolean
|
|
1290
|
+
/**
|
|
1291
|
+
* Whether the last token's payload carried `exp`. Cleared at the start of
|
|
1292
|
+
* each `verify` and set as the claims are read, so a token refused for its
|
|
1293
|
+
* claims may leave it set.
|
|
1294
|
+
*/
|
|
1217
1295
|
hasExp: boolean
|
|
1296
|
+
/** Whether the last token's payload carried `nbf`, as `hasExp`. */
|
|
1218
1297
|
hasNbf: boolean
|
|
1298
|
+
/** Whether the last token's payload carried `iat`, as `hasExp`. */
|
|
1219
1299
|
hasIat: boolean
|
|
1220
1300
|
|
|
1221
|
-
/**
|
|
1301
|
+
/**
|
|
1302
|
+
* A verifier for tokens of up to `maxToken` bytes; outside
|
|
1303
|
+
* `[16, JWT_MAX_TOKEN_LIMIT]` panics. Every buffer is allocated here, once.
|
|
1304
|
+
*
|
|
1305
|
+
* @param maxToken The longest token taken, in bytes; `JWT_MAX_TOKEN` is the
|
|
1306
|
+
* usual choice.
|
|
1307
|
+
*/
|
|
1222
1308
|
constructor(maxToken: i32) {
|
|
1223
1309
|
if (maxToken < 16 || maxToken > JWT_MAX_TOKEN_LIMIT) {
|
|
1224
1310
|
panic("JwtVerifier: maxToken outside [16, JWT_MAX_TOKEN_LIMIT]")
|
|
@@ -1247,8 +1333,13 @@ export class JwtVerifier {
|
|
|
1247
1333
|
|
|
1248
1334
|
/**
|
|
1249
1335
|
* `verify` on the bytes of `token`, copied into the verifier's own buffer
|
|
1250
|
-
* first
|
|
1251
|
-
*
|
|
1336
|
+
* first; a token longer than `maxToken` is refused before the copy.
|
|
1337
|
+
*
|
|
1338
|
+
* @param key The key to verify with; it alone decides the algorithm.
|
|
1339
|
+
* @param token The compact token; a character above U+007F answers
|
|
1340
|
+
* `JWT_ERR_FORMAT`.
|
|
1341
|
+
* @param now The current time, in seconds since 1970.
|
|
1342
|
+
* @returns `JWT_OK`, or the reason the token was refused, as `verify`.
|
|
1252
1343
|
*/
|
|
1253
1344
|
verifyString(key: JwtKey, token: string, now: i64): i32 {
|
|
1254
1345
|
if (toI32(token.length) > this.maxToken) {
|
|
@@ -1270,8 +1361,28 @@ export class JwtVerifier {
|
|
|
1270
1361
|
|
|
1271
1362
|
/**
|
|
1272
1363
|
* Verifies the token `src[off .. off + len)` under `key` and checks its
|
|
1273
|
-
* claims at `now` (seconds since 1970): `JWT_OK`, or the first
|
|
1274
|
-
*
|
|
1364
|
+
* claims at `now` (seconds since 1970): `JWT_OK`, or the first `JWT_ERR_*`
|
|
1365
|
+
* that applies, in this order:
|
|
1366
|
+
* - the window (`JWT_ERR_SIZE`);
|
|
1367
|
+
* - the three parts and the header's base64url (`JWT_ERR_FORMAT`);
|
|
1368
|
+
* - the header's members in the order written (`JWT_ERR_HEADER`,
|
|
1369
|
+
* `JWT_ERR_ALG`), then a missing `alg` and, when the verifier names a
|
|
1370
|
+
* `typ`, a missing or other one;
|
|
1371
|
+
* - signature text too long for any signature (`JWT_ERR_SIGNATURE`);
|
|
1372
|
+
* - the signature's base64url (`JWT_ERR_FORMAT`);
|
|
1373
|
+
* - its length, the key's fit and the signature itself (`JWT_ERR_SIGNATURE`);
|
|
1374
|
+
* - the payload's base64url (`JWT_ERR_FORMAT`);
|
|
1375
|
+
* - the payload's members in the order written and, while `requireExp` is set
|
|
1376
|
+
* (the default), a missing `exp` (`JWT_ERR_CLAIMS`);
|
|
1377
|
+
* - and then `exp`, `nbf`, `iat`, `iss`, `aud` and `sub`, in that order.
|
|
1378
|
+
*
|
|
1379
|
+
* @param key The key to verify with; it alone decides the algorithm, and a
|
|
1380
|
+
* wiped one verifies nothing.
|
|
1381
|
+
* @param src The bytes that hold the token; they are read, not kept.
|
|
1382
|
+
* @param off Where the token starts in `src`.
|
|
1383
|
+
* @param len The token's length in bytes.
|
|
1384
|
+
* @param now The current time, in seconds since 1970; held to `[0, 2^62]`.
|
|
1385
|
+
* @returns `JWT_OK`, or the reason the token was refused.
|
|
1275
1386
|
*/
|
|
1276
1387
|
verify(key: JwtKey, src: u8[], off: i32, len: i32, now: i64): i32 {
|
|
1277
1388
|
this.claimsLen = 0
|
|
@@ -1340,7 +1451,7 @@ export class JwtVerifier {
|
|
|
1340
1451
|
}
|
|
1341
1452
|
|
|
1342
1453
|
/** The header in `part[0 .. len)` checked against `key`'s algorithm and the verifier's `typ`. */
|
|
1343
|
-
checkHeader(key: JwtKey, len: i32): i32 {
|
|
1454
|
+
private checkHeader(key: JwtKey, len: i32): i32 {
|
|
1344
1455
|
const tok: JwtJson = this.tok
|
|
1345
1456
|
tok.reset(this.part, toI32(0), len)
|
|
1346
1457
|
if (tok.next() !== JWT_JSON_OBJECT_BEGIN) {
|
|
@@ -1401,7 +1512,7 @@ export class JwtVerifier {
|
|
|
1401
1512
|
}
|
|
1402
1513
|
|
|
1403
1514
|
/** The payload in `claims[0 .. len)` parsed and checked at `now`. */
|
|
1404
|
-
checkClaims(len: i32, now: i64): i32 {
|
|
1515
|
+
private checkClaims(len: i32, now: i64): i32 {
|
|
1405
1516
|
const tok: JwtJson = this.tok
|
|
1406
1517
|
tok.reset(this.claims, toI32(0), len)
|
|
1407
1518
|
if (tok.next() !== JWT_JSON_OBJECT_BEGIN) {
|
|
@@ -1523,6 +1634,12 @@ export class JwtVerifier {
|
|
|
1523
1634
|
* or its `kid` does not fit `out`. This reads the header **before** any
|
|
1524
1635
|
* signature is checked, so it only chooses which key to verify with; the
|
|
1525
1636
|
* `kid` proves nothing until `verify` answers `JWT_OK` under that key.
|
|
1637
|
+
*
|
|
1638
|
+
* @param src The bytes that hold the token.
|
|
1639
|
+
* @param off Where the token starts in `src`.
|
|
1640
|
+
* @param len The token's length in bytes.
|
|
1641
|
+
* @param out Where the `kid` is written, unescaped.
|
|
1642
|
+
* @returns The `kid`'s length in bytes, or -1.
|
|
1526
1643
|
*/
|
|
1527
1644
|
peekKid(src: u8[], off: i32, len: i32, out: u8[]): i32 {
|
|
1528
1645
|
if (len < 0 || len > this.maxToken || off < 0 || off > toI32(src.length) - len) {
|
|
@@ -1617,6 +1734,11 @@ const jwtBytesOf = (text: string): u8[] => {
|
|
|
1617
1734
|
* object, or when `kid` is empty, longer than 256 bytes, or holds a byte that
|
|
1618
1735
|
* would need a JSON escape. The claims are signed as given: this function
|
|
1619
1736
|
* neither adds nor checks any claim.
|
|
1737
|
+
*
|
|
1738
|
+
* @param key The key to sign with; a wiped one answers `null`.
|
|
1739
|
+
* @param kid The key id to name in the header, or `null` for none.
|
|
1740
|
+
* @param claims The payload: one JSON object, in bytes.
|
|
1741
|
+
* @returns The token, a new string, or `null`.
|
|
1620
1742
|
*/
|
|
1621
1743
|
export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): string | null => {
|
|
1622
1744
|
if (!key.canSign || !key.live || !jwtIsObject(claims)) {
|
|
@@ -1649,13 +1771,23 @@ export const jwtSignKid = (key: JwtKey, kid: string | null, claims: u8[]): strin
|
|
|
1649
1771
|
return input + "." + base64urlEncode(sig)
|
|
1650
1772
|
}
|
|
1651
1773
|
|
|
1652
|
-
/**
|
|
1774
|
+
/**
|
|
1775
|
+
* `jwtSignKid` with no `kid`.
|
|
1776
|
+
*
|
|
1777
|
+
* @param key The key to sign with.
|
|
1778
|
+
* @param claims The payload: one JSON object, in bytes.
|
|
1779
|
+
* @returns The token, or `null` as `jwtSignKid` answers it.
|
|
1780
|
+
*/
|
|
1653
1781
|
export const jwtSign = (key: JwtKey, claims: u8[]): string | null => jwtSignKid(key, null, claims)
|
|
1654
1782
|
|
|
1655
1783
|
/**
|
|
1656
1784
|
* `jwtSign` over the bytes of the string `claims`. Answers `null` for claims
|
|
1657
1785
|
* outside ASCII, as `verifyString` refuses them: escape such text (`\u00e9`)
|
|
1658
1786
|
* or pass its UTF-8 bytes to `jwtSign`.
|
|
1787
|
+
*
|
|
1788
|
+
* @param key The key to sign with.
|
|
1789
|
+
* @param claims The payload: one JSON object, ASCII.
|
|
1790
|
+
* @returns The token, or `null` as `jwtSignKid` answers it.
|
|
1659
1791
|
*/
|
|
1660
1792
|
export const jwtSignString = (key: JwtKey, claims: string): string | null =>
|
|
1661
1793
|
jwtIsAscii(claims) ? jwtSignKid(key, null, jwtBytesOf(claims)) : null
|