@meistrari/data-token 1.0.0 → 1.0.1

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,6 +1,8 @@
1
1
  import Elysia from 'elysia';
2
2
  import { getValidator } from '../index.mjs';
3
+ import 'node:buffer';
3
4
  import 'jose';
5
+ import 'jose/dist/types/util/errors';
4
6
  import 'zod';
5
7
 
6
8
  function createDataTokenPlugin(url, skipOnError = false, getToken = (c) => c.headers["x-data-token"] ?? "") {
@@ -1,6 +1,8 @@
1
1
  import { createMiddleware } from 'hono/factory';
2
2
  import { getValidator } from '../index.mjs';
3
+ import 'node:buffer';
3
4
  import 'jose';
5
+ import 'jose/dist/types/util/errors';
4
6
  import 'zod';
5
7
 
6
8
  function createDataTokenMiddleware(url, skipOnError = false, getToken = (c) => c.req.header("x-data-token") ?? "") {
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,4 +1,6 @@
1
+ import { Buffer } from 'node:buffer';
1
2
  import { createRemoteJWKSet, jwtVerify, errors, decodeJwt } from 'jose';
3
+ import { JWKSNoMatchingKey } from 'jose/dist/types/util/errors';
2
4
  import { z } from 'zod';
3
5
 
4
6
  const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -56,7 +58,11 @@ const retry = createExponentialBackoff({
56
58
  maxRetries: 3,
57
59
  jitter: true
58
60
  });
59
- function getValidator(jwksUrl, { algorithms = ["RS256"], remoteJwksOptions = {} } = {}) {
61
+ function getValidator(jwksUrl, {
62
+ algorithms = ["RS256"],
63
+ remoteJwksOptions = {},
64
+ logger = console
65
+ } = {}) {
60
66
  let JWKS;
61
67
  return async (token) => {
62
68
  JWKS || (JWKS = createRemoteJWKSet(
@@ -68,11 +74,29 @@ function getValidator(jwksUrl, { algorithms = ["RS256"], remoteJwksOptions = {}
68
74
  ...remoteJwksOptions
69
75
  }
70
76
  ));
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);
77
+ try {
78
+ const { payload } = await retry(
79
+ async () => jwtVerify(token, JWKS, { algorithms }),
80
+ (err) => err instanceof errors.JWKSTimeout || err instanceof Error && err.message.includes("ECONNRESET")
81
+ );
82
+ return DataToken.parse(payload);
83
+ } catch (error) {
84
+ if (error instanceof JWKSNoMatchingKey) {
85
+ const [header] = token.split(".");
86
+ const tokenHeader = JSON.parse(Buffer.from(header, "base64").toString("utf-8"));
87
+ const jwksResponse = await fetch(jwksUrl);
88
+ const jwksText = await jwksResponse.json();
89
+ logger.error({
90
+ level: "error",
91
+ message: "No matching key found in JWKS for token",
92
+ jwksUrl,
93
+ algorithms,
94
+ jwks: jwksText,
95
+ tokenHeader
96
+ });
97
+ }
98
+ throw error;
99
+ }
76
100
  };
77
101
  }
78
102
  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.1",
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",