@exortek/apikey 1.1.1 → 1.1.2
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/CHANGELOG.md +18 -0
- package/README.md +3 -3
- package/dist/index.cjs +37 -291
- package/dist/index.mjs +37 -291
- package/dist/internal/guards.d.ts +2 -2
- package/dist/middleware/express.cjs +38 -292
- package/dist/middleware/express.mjs +38 -292
- package/dist/middleware/fastify.cjs +38 -292
- package/dist/middleware/fastify.mjs +38 -292
- package/dist/stores.cjs +81 -296
- package/dist/stores.mjs +81 -296
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @exortek/apikey
|
|
2
2
|
|
|
3
|
+
## 1.1.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b9e0647: Publish only the package-root README, CHANGELOG and LICENSE.
|
|
8
|
+
|
|
9
|
+
The `files` list matched those names at any depth rather than just the root, so a nested document was published
|
|
10
|
+
alongside them — `@exortek/oauth2` shipped its `examples/README.md`. The entries are now anchored to the package root.
|
|
11
|
+
|
|
12
|
+
- 0a94f13: Smaller bundles — the internal argument-guard helpers are now tree-shakeable.
|
|
13
|
+
|
|
14
|
+
Each package bundles the guard helpers it uses. They were previously built as one object holding all fourteen, which a
|
|
15
|
+
bundler cannot take apart, so every package shipped all of them regardless of how many it called. They are now
|
|
16
|
+
individually importable, and each package pulls in only what it uses.
|
|
17
|
+
|
|
18
|
+
No API change: the errors, codes and messages raised by argument validation are identical. Published bundles shrink by
|
|
19
|
+
roughly 7-18% depending on the package.
|
|
20
|
+
|
|
3
21
|
## 1.1.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
> Stripe-style prefixed API keys for Node.js 22+ — 3-segment tokens (`sk_live_<id>_<secret>`), HMAC-SHA256 storage hash with optional pepper rotation, scope allowlists, memory + Redis stores, Express + Fastify middleware. Zero non-`@exortek/*` runtime dependencies.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@exortek/apikey)
|
|
6
|
-
[](https://github.com/ExorTek/auth/actions/workflows/ci.yml)
|
|
7
7
|
[](https://nodejs.org)
|
|
8
8
|
[](https://packagephobia.com/result?p=@exortek/apikey)
|
|
9
9
|
[](./dist/index.d.ts)
|
|
10
|
-
[](
|
|
10
|
+
[](https://github.com/ExorTek/auth/blob/master/LICENSE)
|
|
11
11
|
|
|
12
12
|
📖 **Docs:** [**auth.memet.dev/apikey**](https://auth.memet.dev/apikey)
|
|
13
13
|
|
|
@@ -325,4 +325,4 @@ Expected verify failures do NOT throw — they surface as `{ valid: false, reaso
|
|
|
325
325
|
|
|
326
326
|
## License
|
|
327
327
|
|
|
328
|
-
MIT © ExorTek — see [LICENSE](
|
|
328
|
+
MIT © ExorTek — see [LICENSE](https://github.com/ExorTek/auth/blob/master/LICENSE).
|
package/dist/index.cjs
CHANGED
|
@@ -351,20 +351,33 @@ class ApiKeyError extends BaseError {
|
|
|
351
351
|
* `@exortek/shared/validate`: **schema** for whole options objects,
|
|
352
352
|
* **asserts** for one-liner argument guards at the call site.
|
|
353
353
|
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
354
|
+
* Every guard is exported as a `make…` **factory** taking the binding
|
|
355
|
+
* package's `wrap` and returning the guard itself. A package binds the
|
|
356
|
+
* guards it calls once, in its `internal/guards.js`, so every argument
|
|
357
|
+
* failure throws that package's error class — `err instanceof
|
|
358
|
+
* CryptoError` holds, and users see at a glance which package raised it.
|
|
359
|
+
*
|
|
360
|
+
* // packages/<pkg>/src/internal/guards.js
|
|
361
|
+
* import { makeAssertString, makeInvalidArgument } from '@exortek/shared/asserts';
|
|
362
|
+
* import { CryptoError, ErrorCode } from '../errors.js';
|
|
363
|
+
*
|
|
364
|
+
* const wrap = (message, extra) => new CryptoError(ErrorCode.INVALID_ARGUMENT, message, extra);
|
|
365
|
+
*
|
|
366
|
+
* export const assertString = makeAssertString(wrap);
|
|
367
|
+
* export const invalidArgument = makeInvalidArgument(wrap);
|
|
368
|
+
*
|
|
369
|
+
* Importing each factory by name is what makes the module
|
|
370
|
+
* tree-shakeable. An earlier version handed back one object carrying
|
|
371
|
+
* every guard, so all of them shipped in every package's bundle no
|
|
372
|
+
* matter how few were used — `assertBoolean` reached fifteen published
|
|
373
|
+
* packages without a single call site. Import what you call.
|
|
361
374
|
*
|
|
362
375
|
* **Path naming convention** (used as the `name` argument):
|
|
363
376
|
*
|
|
364
377
|
* `<publicFunction>[.options|.config][.<field>]`
|
|
365
378
|
*
|
|
366
|
-
* - `assertString(name, 'createUser.name')`
|
|
367
|
-
* - `assertPositiveInt(n, 'scrypt.options.r')`
|
|
379
|
+
* - `assertString(name, 'createUser.name')` — top-level arg
|
|
380
|
+
* - `assertPositiveInt(n, 'scrypt.options.r')` — nested option
|
|
368
381
|
* - `assertBytesOrString(pwd, 'pepper.wrap.password')` — method arg
|
|
369
382
|
*
|
|
370
383
|
* Keep the path short: the emitted error is `"<name> must be <desc>"`,
|
|
@@ -373,301 +386,34 @@ class ApiKeyError extends BaseError {
|
|
|
373
386
|
* encryptSymmetric()"), not for restating the field.
|
|
374
387
|
*/
|
|
375
388
|
|
|
376
|
-
/**
|
|
377
|
-
* @typedef {{ cause?: unknown }} WrapExtra
|
|
378
|
-
* Extra options forwarded to the wrap function, so `invalidArgument`
|
|
379
|
-
* sites that catch an underlying error can preserve the `cause` chain
|
|
380
|
-
* without falling back to a raw `new PackageError(...)`.
|
|
381
|
-
*
|
|
382
|
-
* @typedef {(message: string, extra?: WrapExtra) => Error} WrapFn
|
|
383
|
-
* Constructs (never throws) the binding package's error, e.g.
|
|
384
|
-
* `(m, { cause } = {}) => new CryptoError(ErrorCode.INVALID_ARGUMENT, m, { cause })`.
|
|
385
|
-
*
|
|
386
|
-
* @typedef {{ hint?: string }} AssertOptions
|
|
387
|
-
* `hint` is appended to the message after an em-dash — use it for
|
|
388
|
-
* actionable guidance ("pass the exact bytes returned by …").
|
|
389
|
-
*
|
|
390
|
-
* @typedef {{ safeParse: (input: unknown, path?: string) => { ok: true, value: unknown } | { ok: false, errors: string[] } }} ParseableSchema
|
|
391
|
-
*/
|
|
392
389
|
|
|
393
390
|
/**
|
|
394
|
-
*
|
|
395
|
-
* `
|
|
396
|
-
*
|
|
391
|
+
* Construct (not throw) the bound error with a free-form message — for
|
|
392
|
+
* `throw invalidArgument('…')` sites that don't fit the `X must be Y`
|
|
393
|
+
* shape (canonicalisation errors, cross-field constraint failures). Pass
|
|
394
|
+
* `{ cause }` when the failure was triggered by an underlying throw whose
|
|
395
|
+
* chain matters (e.g. `JSON.stringify` on a bigint).
|
|
397
396
|
*
|
|
398
|
-
* @
|
|
399
|
-
* @param {string} description
|
|
400
|
-
* @param {string} [hint]
|
|
401
|
-
* @returns {string}
|
|
397
|
+
* @type {(wrap: WrapFn) => (msg: string, extra?: WrapExtra) => Error}
|
|
402
398
|
*/
|
|
403
|
-
|
|
404
|
-
return `${name} must be ${description}${hint ? ` — ${hint}` : ''}`;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const ENCODINGS = new Set(['hex', 'base64', 'base64url']);
|
|
399
|
+
const makeInvalidArgument = wrap => (msg, extra) => wrap(msg, extra);
|
|
408
400
|
|
|
409
401
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
* import { bindAsserts } from '@exortek/shared/asserts';
|
|
415
|
-
* import { CryptoError, ErrorCode } from '../errors.js';
|
|
416
|
-
* export const g = bindAsserts((m) => new CryptoError(ErrorCode.INVALID_ARGUMENT, m));
|
|
417
|
-
*
|
|
418
|
-
* // call sites:
|
|
419
|
-
* g.assertPositiveInt(options.iterations, 'options.iterations');
|
|
420
|
-
* const cfg = g.parse(OptionsSchema, options, 'options');
|
|
421
|
-
*
|
|
422
|
-
* @param {WrapFn} wrap
|
|
402
|
+
* Argument guards bound to `ApiKeyError` — the package-wide binding of
|
|
403
|
+
* `@exortek/shared/asserts`. Import guards from here, never from the
|
|
404
|
+
* shared module directly.
|
|
423
405
|
*/
|
|
424
|
-
function bindAsserts(wrap) {
|
|
425
|
-
/**
|
|
426
|
-
* @param {string} name
|
|
427
|
-
* @param {string} description
|
|
428
|
-
* @param {AssertOptions} [opts]
|
|
429
|
-
* @returns {never}
|
|
430
|
-
*/
|
|
431
|
-
function fail(name, description, opts) {
|
|
432
|
-
throw wrap(message(name, description, opts?.hint));
|
|
433
|
-
}
|
|
434
406
|
|
|
435
|
-
/**
|
|
436
|
-
* Assert that `value` is a plain object (not `null`, not an array,
|
|
437
|
-
* not a primitive).
|
|
438
|
-
* @param {unknown} value
|
|
439
|
-
* @param {string} name
|
|
440
|
-
* @param {AssertOptions} [opts]
|
|
441
|
-
*/
|
|
442
|
-
function assertObject(value, name, opts) {
|
|
443
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
444
|
-
fail(name, 'an object', opts);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return {
|
|
449
|
-
/**
|
|
450
|
-
* Construct (not throw) the bound error with a free-form message —
|
|
451
|
-
* for `throw g.invalidArgument('…')` sites that don't fit the
|
|
452
|
-
* `X must be Y` shape (canonicalisation errors, cross-field
|
|
453
|
-
* constraint failures). Pass `{ cause }` when the argument-shape
|
|
454
|
-
* failure was triggered by an underlying throw whose chain matters
|
|
455
|
-
* (e.g. `JSON.stringify` on a bigint).
|
|
456
|
-
*
|
|
457
|
-
* @param {string} msg
|
|
458
|
-
* @param {WrapExtra} [extra]
|
|
459
|
-
* @returns {Error}
|
|
460
|
-
*/
|
|
461
|
-
invalidArgument(msg, extra) {
|
|
462
|
-
return wrap(msg, extra);
|
|
463
|
-
},
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Validate `input` against a `@exortek/shared/validate` schema;
|
|
467
|
-
* failures throw the bound error carrying every collected message.
|
|
468
|
-
* The bridge that keeps schema validation on the package's own
|
|
469
|
-
* error surface.
|
|
470
|
-
*
|
|
471
|
-
* @param {ParseableSchema} schema
|
|
472
|
-
* @param {unknown} input
|
|
473
|
-
* @param {string} [path='options']
|
|
474
|
-
* @returns {unknown} the parsed (possibly defaulted) value
|
|
475
|
-
*/
|
|
476
|
-
parse(schema, input, path = 'options') {
|
|
477
|
-
const r = schema.safeParse(input, path);
|
|
478
|
-
if (!r.ok) {
|
|
479
|
-
throw wrap(r.errors.join('; '));
|
|
480
|
-
}
|
|
481
|
-
return r.value;
|
|
482
|
-
},
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Assert that `value` is a non-negative safe integer (`0, 1, 2, …`).
|
|
486
|
-
* @param {unknown} value
|
|
487
|
-
* @param {string} name Argument name to include in the error message.
|
|
488
|
-
* @param {AssertOptions} [opts]
|
|
489
|
-
*/
|
|
490
|
-
assertNonNegativeInt(value, name, opts) {
|
|
491
|
-
if (!Number.isSafeInteger(value) || /** @type {number} */ (value) < 0) {
|
|
492
|
-
fail(name, 'a non-negative safe integer', opts);
|
|
493
|
-
}
|
|
494
|
-
},
|
|
495
|
-
|
|
496
|
-
/**
|
|
497
|
-
* Assert that `value` is a strictly positive safe integer (`1, 2, 3, …`).
|
|
498
|
-
* @param {unknown} value
|
|
499
|
-
* @param {string} name
|
|
500
|
-
* @param {AssertOptions} [opts]
|
|
501
|
-
*/
|
|
502
|
-
assertPositiveInt(value, name, opts) {
|
|
503
|
-
if (!Number.isSafeInteger(value) || /** @type {number} */ (value) <= 0) {
|
|
504
|
-
fail(name, 'a positive integer', opts);
|
|
505
|
-
}
|
|
506
|
-
},
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* Assert that `value` fits in a 48-bit unsigned integer (`0 … 2^48 − 1`).
|
|
510
|
-
* Used for Unix millisecond timestamps in UUID v7 / ULID.
|
|
511
|
-
* @param {unknown} value
|
|
512
|
-
* @param {string} name
|
|
513
|
-
* @param {AssertOptions} [opts]
|
|
514
|
-
*/
|
|
515
|
-
assertUint48(value, name, opts) {
|
|
516
|
-
if (
|
|
517
|
-
!Number.isSafeInteger(value) ||
|
|
518
|
-
/** @type {number} */ (value) < 0 ||
|
|
519
|
-
/** @type {number} */ (value) > 0xffffffffffff
|
|
520
|
-
) {
|
|
521
|
-
fail(name, 'a non-negative safe integer ≤ 2^48 − 1 (Unix ms since epoch)', opts);
|
|
522
|
-
}
|
|
523
|
-
},
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* Assert that `value` is a string (may be empty).
|
|
527
|
-
* @param {unknown} value
|
|
528
|
-
* @param {string} name
|
|
529
|
-
* @param {AssertOptions} [opts]
|
|
530
|
-
*/
|
|
531
|
-
assertString(value, name, opts) {
|
|
532
|
-
if (typeof value !== 'string') {
|
|
533
|
-
fail(name, 'a string', opts);
|
|
534
|
-
}
|
|
535
|
-
},
|
|
536
|
-
|
|
537
|
-
/**
|
|
538
|
-
* Assert that `value` is a non-empty string.
|
|
539
|
-
* @param {unknown} value
|
|
540
|
-
* @param {string} name
|
|
541
|
-
* @param {AssertOptions} [opts]
|
|
542
|
-
*/
|
|
543
|
-
assertNonEmptyString(value, name, opts) {
|
|
544
|
-
if (typeof value !== 'string' || value.length === 0) {
|
|
545
|
-
fail(name, 'a non-empty string', opts);
|
|
546
|
-
}
|
|
547
|
-
},
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
* Assert that `value` is a boolean.
|
|
551
|
-
* @param {unknown} value
|
|
552
|
-
* @param {string} name
|
|
553
|
-
* @param {AssertOptions} [opts]
|
|
554
|
-
*/
|
|
555
|
-
assertBoolean(value, name, opts) {
|
|
556
|
-
if (typeof value !== 'boolean') {
|
|
557
|
-
fail(name, 'a boolean', opts);
|
|
558
|
-
}
|
|
559
|
-
},
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* Assert that `value` is a function.
|
|
563
|
-
* @param {unknown} value
|
|
564
|
-
* @param {string} name
|
|
565
|
-
* @param {AssertOptions} [opts]
|
|
566
|
-
*/
|
|
567
|
-
assertFunction(value, name, opts) {
|
|
568
|
-
if (typeof value !== 'function') {
|
|
569
|
-
fail(name, 'a function', opts);
|
|
570
|
-
}
|
|
571
|
-
},
|
|
572
|
-
|
|
573
|
-
assertObject,
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
* Assert that `value` is either `undefined` or a plain object —
|
|
577
|
-
* the "optional options object" pattern.
|
|
578
|
-
* @param {unknown} value
|
|
579
|
-
* @param {string} name
|
|
580
|
-
* @param {AssertOptions} [opts]
|
|
581
|
-
*/
|
|
582
|
-
assertOptionalObject(value, name, opts) {
|
|
583
|
-
if (value === undefined) {
|
|
584
|
-
return;
|
|
585
|
-
}
|
|
586
|
-
assertObject(value, name, opts);
|
|
587
|
-
},
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Assert that `value` is a byte buffer (`Buffer` or `Uint8Array`) —
|
|
591
|
-
* strings are NOT accepted. For already-encoded material where a
|
|
592
|
-
* string would be ambiguous (ciphertext, signatures, raw key bytes).
|
|
593
|
-
* @param {unknown} value
|
|
594
|
-
* @param {string} name
|
|
595
|
-
* @param {AssertOptions} [opts]
|
|
596
|
-
*/
|
|
597
|
-
assertBytes(value, name, opts) {
|
|
598
|
-
if (!Buffer.isBuffer(value) && !(value instanceof Uint8Array)) {
|
|
599
|
-
fail(name, 'a Buffer or Uint8Array', opts);
|
|
600
|
-
}
|
|
601
|
-
},
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* Assert that `value` is either a string or a byte buffer (`Buffer`
|
|
605
|
-
* or `Uint8Array`).
|
|
606
|
-
* @param {unknown} value
|
|
607
|
-
* @param {string} name
|
|
608
|
-
* @param {AssertOptions} [opts]
|
|
609
|
-
*/
|
|
610
|
-
assertBytesOrString(value, name, opts) {
|
|
611
|
-
if (typeof value !== 'string' && !Buffer.isBuffer(value) && !(value instanceof Uint8Array)) {
|
|
612
|
-
fail(name, 'a string or Buffer', opts);
|
|
613
|
-
}
|
|
614
|
-
},
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
* Assert that `encoding` is one of the accepted output/input
|
|
618
|
-
* encodings. Pass `allowBuffer: false` where a `Buffer` output makes
|
|
619
|
-
* no sense (verifying a string signature, decoding a token payload).
|
|
620
|
-
*
|
|
621
|
-
* @param {unknown} encoding
|
|
622
|
-
* @param {string} name
|
|
623
|
-
* @param {AssertOptions & { allowBuffer?: boolean }} [opts]
|
|
624
|
-
*/
|
|
625
|
-
assertEncoding(encoding, name, opts) {
|
|
626
|
-
const allowBuffer = opts?.allowBuffer !== false;
|
|
627
|
-
const valid = (typeof encoding === 'string' && ENCODINGS.has(encoding)) || (allowBuffer && encoding === 'buffer');
|
|
628
|
-
if (!valid) {
|
|
629
|
-
const list = allowBuffer ? "'hex', 'base64', 'base64url', or 'buffer'" : "'hex', 'base64', or 'base64url'";
|
|
630
|
-
fail(name, list, opts);
|
|
631
|
-
}
|
|
632
|
-
},
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
407
|
|
|
636
408
|
/**
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
* own error class + `INVALID_ARGUMENT` code. Emits the standard
|
|
640
|
-
* `new ErrorClass(code, message, { cause })` shape, so the underlying
|
|
641
|
-
* `BaseError` `{ cause, status, details }` options object is respected.
|
|
642
|
-
*
|
|
643
|
-
* // packages/<pkg>/src/internal/guards.js
|
|
644
|
-
* import { defineGuards } from '@exortek/shared/asserts';
|
|
645
|
-
* import { PasswordError, ErrorCode } from '../errors.js';
|
|
409
|
+
* Bind every guard this package calls to ApiKeyError, so an argument-shape
|
|
410
|
+
* failure anywhere carries one class and one code to branch on.
|
|
646
411
|
*
|
|
647
|
-
*
|
|
648
|
-
* defineGuards(PasswordError, ErrorCode.INVALID_ARGUMENT);
|
|
649
|
-
*
|
|
650
|
-
* Reach for the raw {@link bindAsserts} when you need a different
|
|
651
|
-
* mapping (an alt code, a decorated message) — it stays available.
|
|
652
|
-
*
|
|
653
|
-
* @param {new (code: string, message: string, options?: { cause?: unknown }) => Error} ErrorClass
|
|
654
|
-
* @param {string} code
|
|
655
|
-
*/
|
|
656
|
-
function defineGuards(ErrorClass, code) {
|
|
657
|
-
return bindAsserts((message, extra) => new ErrorClass(code, message, extra));
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Argument guards bound to `ApiKeyError` — the package-wide binding of
|
|
662
|
-
* `@exortek/shared/asserts`. Import guards from here, never from the
|
|
663
|
-
* shared module directly.
|
|
412
|
+
* @type {import('@exortek/shared/asserts').WrapFn}
|
|
664
413
|
*/
|
|
414
|
+
const wrap = (message, extra) => new ApiKeyError(ErrorCode.INVALID_ARGUMENT, message, extra);
|
|
665
415
|
|
|
666
|
-
|
|
667
|
-
const { invalidArgument} = defineGuards(
|
|
668
|
-
ApiKeyError,
|
|
669
|
-
ErrorCode.INVALID_ARGUMENT,
|
|
670
|
-
);
|
|
416
|
+
const invalidArgument = makeInvalidArgument(wrap);
|
|
671
417
|
|
|
672
418
|
/**
|
|
673
419
|
* Scope match helpers.
|