@meistrari/data-token 1.0.2 → 1.1.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.
@@ -2,6 +2,7 @@ import Elysia from 'elysia';
2
2
  import { getValidator } from '../index.mjs';
3
3
  import 'node:buffer';
4
4
  import 'jose';
5
+ import '@meistrari/logger';
5
6
  import 'zod';
6
7
 
7
8
  function createDataTokenPlugin(url, skipOnError = false, getToken = (c) => c.headers["x-data-token"] ?? "") {
@@ -2,6 +2,7 @@ import { createMiddleware } from 'hono/factory';
2
2
  import { getValidator } from '../index.mjs';
3
3
  import 'node:buffer';
4
4
  import 'jose';
5
+ import '@meistrari/logger';
5
6
  import 'zod';
6
7
 
7
8
  function createDataTokenMiddleware(url, skipOnError = false, getToken = (c) => c.req.header("x-data-token") ?? "") {
package/dist/index.d.mts CHANGED
@@ -13,10 +13,9 @@ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb
13
13
  * @param options.logger - The logger to use for logging errors.
14
14
  * @returns A function that can validate a data token.
15
15
  */
16
- declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, logger, }?: {
16
+ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, }?: {
17
17
  algorithms?: string[] | undefined;
18
18
  remoteJwksOptions?: RemoteJWKSetOptions | undefined;
19
- logger?: any;
20
19
  }): (token: string) => Promise<{
21
20
  requestId: string;
22
21
  workspace: {
package/dist/index.d.ts CHANGED
@@ -13,10 +13,9 @@ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb
13
13
  * @param options.logger - The logger to use for logging errors.
14
14
  * @returns A function that can validate a data token.
15
15
  */
16
- declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, logger, }?: {
16
+ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions, }?: {
17
17
  algorithms?: string[] | undefined;
18
18
  remoteJwksOptions?: RemoteJWKSetOptions | undefined;
19
- logger?: any;
20
19
  }): (token: string) => Promise<{
21
20
  requestId: string;
22
21
  workspace: {
package/dist/index.mjs CHANGED
@@ -1,7 +1,14 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import { createRemoteJWKSet, jwtVerify, errors, decodeJwt } from 'jose';
3
+ import pino from '@meistrari/logger';
3
4
  import { z } from 'zod';
4
5
 
6
+ const logger = pino({
7
+ base: {
8
+ module: "@meistrari/data-token"
9
+ }
10
+ });
11
+
5
12
  const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
6
13
  function calculateDelay(options, attempt) {
7
14
  let delay = options.baseDelay * options.factor ** attempt;
@@ -59,8 +66,7 @@ const retry = createExponentialBackoff({
59
66
  });
60
67
  function getValidator(jwksUrl, {
61
68
  algorithms = ["RS256"],
62
- remoteJwksOptions = {},
63
- logger = console
69
+ remoteJwksOptions = {}
64
70
  } = {}) {
65
71
  let JWKS;
66
72
  return async (token) => {
@@ -80,20 +86,20 @@ function getValidator(jwksUrl, {
80
86
  );
81
87
  return DataToken.parse(payload);
82
88
  } 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
- });
89
+ if (error instanceof errors.JWTExpired) {
90
+ throw error;
96
91
  }
92
+ const [header] = token.split(".");
93
+ const tokenHeader = JSON.parse(Buffer.from(header, "base64").toString("utf-8"));
94
+ const jwksResponse = await fetch(jwksUrl);
95
+ const jwksText = await jwksResponse.json();
96
+ logger.error({
97
+ error,
98
+ jwksUrl,
99
+ algorithms,
100
+ jwks: jwksText,
101
+ tokenHeader
102
+ }, "Failed to validate data token");
97
103
  throw error;
98
104
  }
99
105
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meistrari/data-token",
3
3
  "type": "module",
4
- "version": "1.0.2",
4
+ "version": "1.1.0",
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",
@@ -41,6 +41,7 @@
41
41
  "prepack": "bun run build"
42
42
  },
43
43
  "dependencies": {
44
+ "@meistrari/logger": "2.0.1",
44
45
  "cockatiel": "^3.2.1",
45
46
  "jose": "^5.8.0",
46
47
  "zod": "^3.23.8"