@meistrari/data-token 1.0.0 → 1.0.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.
@@ -1,5 +1,6 @@
1
1
  import Elysia from 'elysia';
2
2
  import { getValidator } from '../index.mjs';
3
+ import 'node:buffer';
3
4
  import 'jose';
4
5
  import 'zod';
5
6
 
@@ -1,5 +1,6 @@
1
1
  import { createMiddleware } from 'hono/factory';
2
2
  import { getValidator } from '../index.mjs';
3
+ import 'node:buffer';
3
4
  import 'jose';
4
5
  import 'zod';
5
6
 
package/dist/index.d.mts CHANGED
@@ -10,11 +10,13 @@ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb
10
10
  * @param options - The options for the validator.
11
11
  * @param options.algorithms - The algorithms to use for validation.
12
12
  * @param options.remoteJwksOptions - The options to pass to `createRemoteJWKSet`.
13
+ * @param options.logger - The logger to use for logging errors.
13
14
  * @returns A function that can validate a data token.
14
15
  */
15
- declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions }?: {
16
+ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, logger, }?: {
16
17
  algorithms?: string[] | undefined;
17
18
  remoteJwksOptions?: RemoteJWKSetOptions | undefined;
19
+ logger?: any;
18
20
  }): (token: string) => Promise<{
19
21
  requestId: string;
20
22
  workspace: {
package/dist/index.d.ts CHANGED
@@ -10,11 +10,13 @@ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb
10
10
  * @param options - The options for the validator.
11
11
  * @param options.algorithms - The algorithms to use for validation.
12
12
  * @param options.remoteJwksOptions - The options to pass to `createRemoteJWKSet`.
13
+ * @param options.logger - The logger to use for logging errors.
13
14
  * @returns A function that can validate a data token.
14
15
  */
15
- declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions }?: {
16
+ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, logger, }?: {
16
17
  algorithms?: string[] | undefined;
17
18
  remoteJwksOptions?: RemoteJWKSetOptions | undefined;
19
+ logger?: any;
18
20
  }): (token: string) => Promise<{
19
21
  requestId: string;
20
22
  workspace: {
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { Buffer } from 'node:buffer';
1
2
  import { createRemoteJWKSet, jwtVerify, errors, decodeJwt } from 'jose';
2
3
  import { z } from 'zod';
3
4
 
@@ -56,7 +57,11 @@ const retry = createExponentialBackoff({
56
57
  maxRetries: 3,
57
58
  jitter: true
58
59
  });
59
- function getValidator(jwksUrl, { algorithms = ["RS256"], remoteJwksOptions = {} } = {}) {
60
+ function getValidator(jwksUrl, {
61
+ algorithms = ["RS256"],
62
+ remoteJwksOptions = {},
63
+ logger = console
64
+ } = {}) {
60
65
  let JWKS;
61
66
  return async (token) => {
62
67
  JWKS || (JWKS = createRemoteJWKSet(
@@ -68,11 +73,29 @@ function getValidator(jwksUrl, { algorithms = ["RS256"], remoteJwksOptions = {}
68
73
  ...remoteJwksOptions
69
74
  }
70
75
  ));
71
- const { payload } = await retry(
72
- async () => jwtVerify(token, JWKS, { algorithms }),
73
- (err) => err instanceof errors.JWKSTimeout || err instanceof Error && err.message.includes("ECONNRESET")
74
- );
75
- return DataToken.parse(payload);
76
+ try {
77
+ const { payload } = await retry(
78
+ async () => jwtVerify(token, JWKS, { algorithms }),
79
+ (err) => err instanceof errors.JWKSTimeout || err instanceof Error && err.message.includes("ECONNRESET")
80
+ );
81
+ return DataToken.parse(payload);
82
+ } catch (error) {
83
+ if (error instanceof errors.JWKSNoMatchingKey) {
84
+ const [header] = token.split(".");
85
+ const tokenHeader = JSON.parse(Buffer.from(header, "base64").toString("utf-8"));
86
+ const jwksResponse = await fetch(jwksUrl);
87
+ const jwksText = await jwksResponse.json();
88
+ logger.error({
89
+ level: "error",
90
+ message: "No matching key found in JWKS for token",
91
+ jwksUrl,
92
+ algorithms,
93
+ jwks: jwksText,
94
+ tokenHeader
95
+ });
96
+ }
97
+ throw error;
98
+ }
76
99
  };
77
100
  }
78
101
  function isAuthenticationError(err) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meistrari/data-token",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "description": "Internal library to verify and decode the API Gateway data token",
6
6
  "author": "Roz <roz@meistrari.com>",
7
7
  "license": "UNLICENSED",