@meistrari/data-token 0.0.16 → 0.0.19

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,3 +1,4 @@
1
+ import * as _sinclair_typebox from '@sinclair/typebox';
1
2
  import Elysia, { Context } from 'elysia';
2
3
 
3
4
  /**
@@ -18,7 +19,7 @@ import Elysia, { Context } from 'elysia';
18
19
  * .get('/', ({ authData }) => ({ authData }))
19
20
  * ```
20
21
  */
21
- declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getToken?: (c: Context) => string): Elysia<"", false, {
22
+ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getToken?: (c: Context) => string): Elysia<"", {
22
23
  decorator: {};
23
24
  store: {};
24
25
  derive: {
@@ -37,12 +38,13 @@ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getTo
37
38
  };
38
39
  resolve: {};
39
40
  }, {
40
- type: {};
41
+ typebox: _sinclair_typebox.TModule<{}>;
41
42
  error: {};
42
43
  }, {
43
44
  schema: {};
44
45
  macro: {};
45
46
  macroFn: {};
47
+ parser: {};
46
48
  }, {}, {
47
49
  derive: {};
48
50
  resolve: {};
@@ -1,3 +1,4 @@
1
+ import * as _sinclair_typebox from '@sinclair/typebox';
1
2
  import Elysia, { Context } from 'elysia';
2
3
 
3
4
  /**
@@ -18,7 +19,7 @@ import Elysia, { Context } from 'elysia';
18
19
  * .get('/', ({ authData }) => ({ authData }))
19
20
  * ```
20
21
  */
21
- declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getToken?: (c: Context) => string): Elysia<"", false, {
22
+ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getToken?: (c: Context) => string): Elysia<"", {
22
23
  decorator: {};
23
24
  store: {};
24
25
  derive: {
@@ -37,12 +38,13 @@ declare function createDataTokenPlugin(url: string, skipOnError?: boolean, getTo
37
38
  };
38
39
  resolve: {};
39
40
  }, {
40
- type: {};
41
+ typebox: _sinclair_typebox.TModule<{}>;
41
42
  error: {};
42
43
  }, {
43
44
  schema: {};
44
45
  macro: {};
45
46
  macroFn: {};
47
+ parser: {};
46
48
  }, {}, {
47
49
  derive: {};
48
50
  resolve: {};
@@ -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.815fae84.mjs';
3
+ import { D as DataToken } from '../shared/data-token.1d70cb5e.mjs';
4
4
  import 'zod';
5
5
 
6
6
  type DataTokenContext = {
@@ -28,4 +28,5 @@ type DataTokenContext = {
28
28
  */
29
29
  declare function createDataTokenMiddleware<Env extends DataTokenContext>(url: string | ((c: Context<Env>) => string), skipOnError?: boolean, getToken?: (c: Context<Env>) => string): hono.MiddlewareHandler<Env, string, {}>;
30
30
 
31
- export { type DataTokenContext, createDataTokenMiddleware };
31
+ export { createDataTokenMiddleware };
32
+ export 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.815fae84.js';
3
+ import { D as DataToken } from '../shared/data-token.1d70cb5e.js';
4
4
  import 'zod';
5
5
 
6
6
  type DataTokenContext = {
@@ -28,4 +28,5 @@ type DataTokenContext = {
28
28
  */
29
29
  declare function createDataTokenMiddleware<Env extends DataTokenContext>(url: string | ((c: Context<Env>) => string), skipOnError?: boolean, getToken?: (c: Context<Env>) => string): hono.MiddlewareHandler<Env, string, {}>;
30
30
 
31
- export { type DataTokenContext, createDataTokenMiddleware };
31
+ export { createDataTokenMiddleware };
32
+ export type { DataTokenContext };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,7 @@
1
- export { D as DataToken, V as ValidatorConfig } from './shared/data-token.815fae84.mjs';
2
- import 'zod';
1
+ import * as jose from 'jose';
2
+ import { errors } from 'jose';
3
+ import * as zod from 'zod';
4
+ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb5e.mjs';
3
5
 
4
6
  /**
5
7
  * Get a validator function that can validate a data token.
@@ -23,5 +25,63 @@ declare function getValidator(jwksUrl: string, { algorithms }?: {
23
25
  uuid: string | null;
24
26
  } | undefined;
25
27
  }>;
28
+ /**
29
+ * Check if an error is an authentication error.
30
+ *
31
+ * @param err - The error to check.
32
+ * @returns True if the error is an authentication error, false otherwise.
33
+ */
34
+ declare function isAuthenticationError(err: unknown): err is errors.JOSEError;
35
+ /**
36
+ * **THIS DOES NOT VALIDATE THE TOKEN. DO NOT USE IN AUTHENTICATION-SENSITIVE CONTEXTS**
37
+ *
38
+ * Decodes a JWT and tries to parse its payload according to the data token schema.
39
+ *
40
+ * @param jwt JWT string to be parsed
41
+ * @returns Result of trying to porse the payload
42
+ */
43
+ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
44
+ requestId: string;
45
+ workspace: {
46
+ id: string;
47
+ title: string;
48
+ };
49
+ user?: {
50
+ id: string;
51
+ email: string;
52
+ uuid: string | null;
53
+ } | undefined;
54
+ }, {
55
+ requestId: string;
56
+ workspace: {
57
+ id: string;
58
+ title: string;
59
+ };
60
+ user?: {
61
+ id: string;
62
+ email: string;
63
+ uuid: string | null;
64
+ } | undefined;
65
+ }>;
66
+ /**
67
+ * **THIS DOES NOT VALIDATE THE TOKEN. DO NOT USE IN AUTHENTICATION-SENSITIVE CONTEXTS**
68
+ *
69
+ * Type-unsafe version of {@link safeParse}. Decodes a JWT and casts its type to {@link DataToken} but **does not perform any payload validation**.
70
+ *
71
+ * @param jwt JWT string to be decoded
72
+ * @returns The JWT payload typed as a DataToken
73
+ */
74
+ declare function unsafeDecode(jwt: string): {
75
+ requestId: string;
76
+ workspace: {
77
+ id: string;
78
+ title: string;
79
+ };
80
+ user?: {
81
+ id: string;
82
+ email: string;
83
+ uuid: string | null;
84
+ } | undefined;
85
+ } & jose.JWTPayload;
26
86
 
27
- export { getValidator };
87
+ export { getValidator, isAuthenticationError, safeParse, unsafeDecode };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- export { D as DataToken, V as ValidatorConfig } from './shared/data-token.815fae84.js';
2
- import 'zod';
1
+ import * as jose from 'jose';
2
+ import { errors } from 'jose';
3
+ import * as zod from 'zod';
4
+ export { D as DataToken, V as ValidatorConfig } from './shared/data-token.1d70cb5e.js';
3
5
 
4
6
  /**
5
7
  * Get a validator function that can validate a data token.
@@ -23,5 +25,63 @@ declare function getValidator(jwksUrl: string, { algorithms }?: {
23
25
  uuid: string | null;
24
26
  } | undefined;
25
27
  }>;
28
+ /**
29
+ * Check if an error is an authentication error.
30
+ *
31
+ * @param err - The error to check.
32
+ * @returns True if the error is an authentication error, false otherwise.
33
+ */
34
+ declare function isAuthenticationError(err: unknown): err is errors.JOSEError;
35
+ /**
36
+ * **THIS DOES NOT VALIDATE THE TOKEN. DO NOT USE IN AUTHENTICATION-SENSITIVE CONTEXTS**
37
+ *
38
+ * Decodes a JWT and tries to parse its payload according to the data token schema.
39
+ *
40
+ * @param jwt JWT string to be parsed
41
+ * @returns Result of trying to porse the payload
42
+ */
43
+ declare function safeParse(jwt: string): zod.SafeParseReturnType<{
44
+ requestId: string;
45
+ workspace: {
46
+ id: string;
47
+ title: string;
48
+ };
49
+ user?: {
50
+ id: string;
51
+ email: string;
52
+ uuid: string | null;
53
+ } | undefined;
54
+ }, {
55
+ requestId: string;
56
+ workspace: {
57
+ id: string;
58
+ title: string;
59
+ };
60
+ user?: {
61
+ id: string;
62
+ email: string;
63
+ uuid: string | null;
64
+ } | undefined;
65
+ }>;
66
+ /**
67
+ * **THIS DOES NOT VALIDATE THE TOKEN. DO NOT USE IN AUTHENTICATION-SENSITIVE CONTEXTS**
68
+ *
69
+ * Type-unsafe version of {@link safeParse}. Decodes a JWT and casts its type to {@link DataToken} but **does not perform any payload validation**.
70
+ *
71
+ * @param jwt JWT string to be decoded
72
+ * @returns The JWT payload typed as a DataToken
73
+ */
74
+ declare function unsafeDecode(jwt: string): {
75
+ requestId: string;
76
+ workspace: {
77
+ id: string;
78
+ title: string;
79
+ };
80
+ user?: {
81
+ id: string;
82
+ email: string;
83
+ uuid: string | null;
84
+ } | undefined;
85
+ } & jose.JWTPayload;
26
86
 
27
- export { getValidator };
87
+ export { getValidator, isAuthenticationError, safeParse, unsafeDecode };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createRemoteJWKSet, jwtVerify, errors } from 'jose';
1
+ import { createRemoteJWKSet, jwtVerify, errors, decodeJwt } from 'jose';
2
2
  import { z } from 'zod';
3
3
 
4
4
  const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -69,5 +69,15 @@ function getValidator(jwksUrl, { algorithms } = { algorithms: ["RS256"] }) {
69
69
  return DataToken.parse(payload);
70
70
  };
71
71
  }
72
+ function isAuthenticationError(err) {
73
+ return err instanceof errors.JOSEError;
74
+ }
75
+ function safeParse(jwt) {
76
+ const payload = decodeJwt(jwt);
77
+ return DataToken.safeParse(payload);
78
+ }
79
+ function unsafeDecode(jwt) {
80
+ return decodeJwt(jwt);
81
+ }
72
82
 
73
- export { DataToken, getValidator };
83
+ export { DataToken, getValidator, isAuthenticationError, safeParse, unsafeDecode };
@@ -54,4 +54,5 @@ type ValidatorConfig = {
54
54
  algorithms?: string[];
55
55
  };
56
56
 
57
- export { DataToken as D, type ValidatorConfig as V };
57
+ export { DataToken as D };
58
+ export type { ValidatorConfig as V };
@@ -54,4 +54,5 @@ type ValidatorConfig = {
54
54
  algorithms?: string[];
55
55
  };
56
56
 
57
- export { DataToken as D, type ValidatorConfig as V };
57
+ export { DataToken as D };
58
+ export type { ValidatorConfig as V };
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@meistrari/data-token",
3
3
  "type": "module",
4
- "version": "0.0.16",
4
+ "version": "0.0.19",
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",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "github:meistrari/data-token"
11
+ },
8
12
  "exports": {
9
13
  ".": {
10
14
  "types": "./dist/index.d.ts",