@meistrari/data-token 1.0.3 → 1.2.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.
@@ -33,6 +33,7 @@ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getTo
33
33
  id: string;
34
34
  email: string;
35
35
  uuid: string | null;
36
+ roles?: string[] | null | undefined;
36
37
  } | undefined;
37
38
  } | null;
38
39
  };
@@ -33,6 +33,7 @@ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getTo
33
33
  id: string;
34
34
  email: string;
35
35
  uuid: string | null;
36
+ roles?: string[] | null | undefined;
36
37
  } | undefined;
37
38
  } | null;
38
39
  };
@@ -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"] ?? "") {
@@ -1,6 +1,6 @@
1
1
  import * as hono from 'hono';
2
2
  import { Context } from 'hono';
3
- import { D as DataToken } from '../shared/data-token.1d70cb5e.mjs';
3
+ import { D as DataToken } from '../shared/data-token.f2fe9a18.mjs';
4
4
  import 'zod';
5
5
 
6
6
  type DataTokenContext = {
@@ -1,6 +1,6 @@
1
1
  import * as hono from 'hono';
2
2
  import { Context } from 'hono';
3
- import { D as DataToken } from '../shared/data-token.1d70cb5e.js';
3
+ import { D as DataToken } from '../shared/data-token.f2fe9a18.js';
4
4
  import 'zod';
5
5
 
6
6
  type DataTokenContext = {
@@ -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
@@ -1,7 +1,7 @@
1
1
  import * as jose from 'jose';
2
2
  import { RemoteJWKSetOptions, errors } from 'jose';
3
3
  import * as zod from 'zod';
4
- export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb5e.mjs';
4
+ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.f2fe9a18.mjs';
5
5
 
6
6
  /**
7
7
  * Get a validator function that can validate a data token.
@@ -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: {
@@ -27,6 +26,7 @@ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions,
27
26
  id: string;
28
27
  email: string;
29
28
  uuid: string | null;
29
+ roles?: string[] | null | undefined;
30
30
  } | undefined;
31
31
  }>;
32
32
  /**
@@ -54,6 +54,7 @@ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
54
54
  id: string;
55
55
  email: string;
56
56
  uuid: string | null;
57
+ roles?: string[] | null | undefined;
57
58
  } | undefined;
58
59
  }, {
59
60
  requestId: string;
@@ -65,6 +66,7 @@ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
65
66
  id: string;
66
67
  email: string;
67
68
  uuid: string | null;
69
+ roles?: string[] | null | undefined;
68
70
  } | undefined;
69
71
  }>;
70
72
  /**
@@ -85,6 +87,7 @@ declare function unsafeDecode(jwt: string): {
85
87
  id: string;
86
88
  email: string;
87
89
  uuid: string | null;
90
+ roles?: string[] | null | undefined;
88
91
  } | undefined;
89
92
  } & jose.JWTPayload;
90
93
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as jose from 'jose';
2
2
  import { RemoteJWKSetOptions, errors } from 'jose';
3
3
  import * as zod from 'zod';
4
- export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb5e.js';
4
+ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.f2fe9a18.js';
5
5
 
6
6
  /**
7
7
  * Get a validator function that can validate a data token.
@@ -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: {
@@ -27,6 +26,7 @@ declare function getValidator(jwksUrl: string, { algorithms, remoteJwksOptions,
27
26
  id: string;
28
27
  email: string;
29
28
  uuid: string | null;
29
+ roles?: string[] | null | undefined;
30
30
  } | undefined;
31
31
  }>;
32
32
  /**
@@ -54,6 +54,7 @@ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
54
54
  id: string;
55
55
  email: string;
56
56
  uuid: string | null;
57
+ roles?: string[] | null | undefined;
57
58
  } | undefined;
58
59
  }, {
59
60
  requestId: string;
@@ -65,6 +66,7 @@ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
65
66
  id: string;
66
67
  email: string;
67
68
  uuid: string | null;
69
+ roles?: string[] | null | undefined;
68
70
  } | undefined;
69
71
  }>;
70
72
  /**
@@ -85,6 +87,7 @@ declare function unsafeDecode(jwt: string): {
85
87
  id: string;
86
88
  email: string;
87
89
  uuid: string | null;
90
+ roles?: string[] | null | undefined;
88
91
  } | undefined;
89
92
  } & jose.JWTPayload;
90
93
 
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;
@@ -49,7 +56,8 @@ const DataToken = z.object({
49
56
  user: z.object({
50
57
  id: z.string(),
51
58
  email: z.string(),
52
- uuid: z.string().nullable()
59
+ uuid: z.string().nullable(),
60
+ roles: z.array(z.string()).optional().nullable()
53
61
  }).optional()
54
62
  });
55
63
 
@@ -59,8 +67,7 @@ const retry = createExponentialBackoff({
59
67
  });
60
68
  function getValidator(jwksUrl, {
61
69
  algorithms = ["RS256"],
62
- remoteJwksOptions = {},
63
- logger = console
70
+ remoteJwksOptions = {}
64
71
  } = {}) {
65
72
  let JWKS;
66
73
  return async (token) => {
@@ -88,7 +95,6 @@ function getValidator(jwksUrl, {
88
95
  const jwksResponse = await fetch(jwksUrl);
89
96
  const jwksText = await jwksResponse.json();
90
97
  logger.error({
91
- level: "error",
92
98
  error,
93
99
  jwksUrl,
94
100
  algorithms,
@@ -16,14 +16,17 @@ declare const DataToken: z.ZodObject<{
16
16
  id: z.ZodString;
17
17
  email: z.ZodString;
18
18
  uuid: z.ZodNullable<z.ZodString>;
19
+ roles: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
19
20
  }, "strip", z.ZodTypeAny, {
20
21
  id: string;
21
22
  email: string;
22
23
  uuid: string | null;
24
+ roles?: string[] | null | undefined;
23
25
  }, {
24
26
  id: string;
25
27
  email: string;
26
28
  uuid: string | null;
29
+ roles?: string[] | null | undefined;
27
30
  }>>;
28
31
  }, "strip", z.ZodTypeAny, {
29
32
  requestId: string;
@@ -35,6 +38,7 @@ declare const DataToken: z.ZodObject<{
35
38
  id: string;
36
39
  email: string;
37
40
  uuid: string | null;
41
+ roles?: string[] | null | undefined;
38
42
  } | undefined;
39
43
  }, {
40
44
  requestId: string;
@@ -46,6 +50,7 @@ declare const DataToken: z.ZodObject<{
46
50
  id: string;
47
51
  email: string;
48
52
  uuid: string | null;
53
+ roles?: string[] | null | undefined;
49
54
  } | undefined;
50
55
  }>;
51
56
  type DataToken = z.infer<typeof DataToken>;
@@ -16,14 +16,17 @@ declare const DataToken: z.ZodObject<{
16
16
  id: z.ZodString;
17
17
  email: z.ZodString;
18
18
  uuid: z.ZodNullable<z.ZodString>;
19
+ roles: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
19
20
  }, "strip", z.ZodTypeAny, {
20
21
  id: string;
21
22
  email: string;
22
23
  uuid: string | null;
24
+ roles?: string[] | null | undefined;
23
25
  }, {
24
26
  id: string;
25
27
  email: string;
26
28
  uuid: string | null;
29
+ roles?: string[] | null | undefined;
27
30
  }>>;
28
31
  }, "strip", z.ZodTypeAny, {
29
32
  requestId: string;
@@ -35,6 +38,7 @@ declare const DataToken: z.ZodObject<{
35
38
  id: string;
36
39
  email: string;
37
40
  uuid: string | null;
41
+ roles?: string[] | null | undefined;
38
42
  } | undefined;
39
43
  }, {
40
44
  requestId: string;
@@ -46,6 +50,7 @@ declare const DataToken: z.ZodObject<{
46
50
  id: string;
47
51
  email: string;
48
52
  uuid: string | null;
53
+ roles?: string[] | null | undefined;
49
54
  } | undefined;
50
55
  }>;
51
56
  type DataToken = z.infer<typeof DataToken>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meistrari/data-token",
3
3
  "type": "module",
4
- "version": "1.0.3",
4
+ "version": "1.2.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"