trustline 0.0.1 → 0.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.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +339 -0
  3. package/dist/adapters/mysql/index.cjs +199 -0
  4. package/dist/adapters/mysql/index.cjs.map +1 -0
  5. package/dist/adapters/mysql/index.d.cts +6 -0
  6. package/dist/adapters/mysql/index.d.ts +6 -0
  7. package/dist/adapters/mysql/index.js +21 -0
  8. package/dist/adapters/mysql/index.js.map +1 -0
  9. package/dist/adapters/postgres/index.cjs +199 -0
  10. package/dist/adapters/postgres/index.cjs.map +1 -0
  11. package/dist/adapters/postgres/index.d.cts +6 -0
  12. package/dist/adapters/postgres/index.d.ts +6 -0
  13. package/dist/adapters/postgres/index.js +21 -0
  14. package/dist/adapters/postgres/index.js.map +1 -0
  15. package/dist/adapters/sqlite/index.cjs +216 -0
  16. package/dist/adapters/sqlite/index.cjs.map +1 -0
  17. package/dist/adapters/sqlite/index.d.cts +6 -0
  18. package/dist/adapters/sqlite/index.d.ts +6 -0
  19. package/dist/adapters/sqlite/index.js +28 -0
  20. package/dist/adapters/sqlite/index.js.map +1 -0
  21. package/dist/chunk-CTPFKR4O.js +157 -0
  22. package/dist/chunk-CTPFKR4O.js.map +1 -0
  23. package/dist/chunk-GF3NKEEK.js +18 -0
  24. package/dist/chunk-GF3NKEEK.js.map +1 -0
  25. package/dist/client/index.cjs +141 -0
  26. package/dist/client/index.cjs.map +1 -0
  27. package/dist/client/index.d.cts +18 -0
  28. package/dist/client/index.d.ts +18 -0
  29. package/dist/client/index.js +104 -0
  30. package/dist/client/index.js.map +1 -0
  31. package/dist/frameworks/express/index.cjs +121 -0
  32. package/dist/frameworks/express/index.cjs.map +1 -0
  33. package/dist/frameworks/express/index.d.cts +18 -0
  34. package/dist/frameworks/express/index.d.ts +18 -0
  35. package/dist/frameworks/express/index.js +83 -0
  36. package/dist/frameworks/express/index.js.map +1 -0
  37. package/dist/frameworks/fastify/index.cjs +158 -0
  38. package/dist/frameworks/fastify/index.cjs.map +1 -0
  39. package/dist/frameworks/fastify/index.d.cts +25 -0
  40. package/dist/frameworks/fastify/index.d.ts +25 -0
  41. package/dist/frameworks/fastify/index.js +120 -0
  42. package/dist/frameworks/fastify/index.js.map +1 -0
  43. package/dist/frameworks/hono/index.cjs +117 -0
  44. package/dist/frameworks/hono/index.cjs.map +1 -0
  45. package/dist/frameworks/hono/index.d.cts +17 -0
  46. package/dist/frameworks/hono/index.d.ts +17 -0
  47. package/dist/frameworks/hono/index.js +79 -0
  48. package/dist/frameworks/hono/index.js.map +1 -0
  49. package/dist/index-Dc4GFume.d.ts +34 -0
  50. package/dist/index-DqkKZOlH.d.cts +34 -0
  51. package/dist/index.cjs +571 -0
  52. package/dist/index.cjs.map +1 -0
  53. package/dist/index.d.cts +14 -0
  54. package/dist/index.d.ts +14 -0
  55. package/dist/index.js +537 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/interface-BzT_DC3u.d.cts +38 -0
  58. package/dist/interface-BzT_DC3u.d.ts +38 -0
  59. package/dist/token-BtfYGd9K.d.cts +33 -0
  60. package/dist/token-BtfYGd9K.d.ts +33 -0
  61. package/package.json +125 -3
  62. package/index.js +0 -1
@@ -0,0 +1,18 @@
1
+ import { RequestHandler, Request as Request$1 } from 'express';
2
+ import { S as ServiceIdentity } from '../../token-BtfYGd9K.js';
3
+ import 'jose';
4
+
5
+ interface WebHandler {
6
+ handle(request: Request): Promise<Response>;
7
+ }
8
+ declare function createExpressProvider(provider: WebHandler): RequestHandler;
9
+
10
+ interface GuardVerifier {
11
+ verify(token: string): Promise<ServiceIdentity>;
12
+ }
13
+ interface TrustlineRequest extends Request$1 {
14
+ trustline?: ServiceIdentity;
15
+ }
16
+ declare function createExpressGuard(guard: GuardVerifier): RequestHandler;
17
+
18
+ export { type TrustlineRequest, type WebHandler, createExpressGuard, createExpressProvider };
@@ -0,0 +1,83 @@
1
+ import {
2
+ AuthError
3
+ } from "../../chunk-GF3NKEEK.js";
4
+
5
+ // src/provider/express.ts
6
+ function createExpressProvider(provider) {
7
+ return async function trustlineProvider(request, response) {
8
+ const origin = `${request.protocol}://${request.get("host") ?? "localhost"}`;
9
+ const url = new URL(request.originalUrl || request.url, origin);
10
+ const body = request.method === "GET" || request.method === "HEAD" ? void 0 : await readBody(request);
11
+ const providerResponse = await provider.handle(
12
+ new Request(url.toString(), {
13
+ method: request.method,
14
+ headers: request.headers,
15
+ body
16
+ })
17
+ );
18
+ response.status(providerResponse.status);
19
+ providerResponse.headers.forEach((value, key) => {
20
+ response.setHeader(key, value);
21
+ });
22
+ response.send(await providerResponse.text());
23
+ };
24
+ }
25
+ function readBody(request) {
26
+ return new Promise((resolve, reject) => {
27
+ const chunks = [];
28
+ request.on("data", (chunk) => {
29
+ chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
30
+ });
31
+ request.on("end", () => {
32
+ resolve(
33
+ chunks.length > 0 ? Buffer.concat(chunks).toString("utf8") : void 0
34
+ );
35
+ });
36
+ request.on("error", reject);
37
+ });
38
+ }
39
+
40
+ // src/middleware/express.ts
41
+ function createExpressGuard(guard) {
42
+ return async function trustlineGuard(request, response, next) {
43
+ const token = getBearerToken(request.headers.authorization);
44
+ if (!token) {
45
+ response.status(401).json({
46
+ error: "missing_token",
47
+ message: "Missing bearer token"
48
+ });
49
+ return;
50
+ }
51
+ try {
52
+ const identity = await guard.verify(token);
53
+ request.trustline = identity;
54
+ next();
55
+ } catch (error) {
56
+ const authError = error instanceof AuthError ? error : new AuthError(
57
+ "invalid_token",
58
+ "Token verification failed",
59
+ 401,
60
+ error
61
+ );
62
+ response.status(authError.status).json({
63
+ error: authError.code,
64
+ message: authError.message
65
+ });
66
+ }
67
+ };
68
+ }
69
+ function getBearerToken(header) {
70
+ if (!header) {
71
+ return null;
72
+ }
73
+ const [scheme, value] = header.split(/\s+/, 2);
74
+ if (scheme?.toLowerCase() !== "bearer" || !value) {
75
+ return null;
76
+ }
77
+ return value;
78
+ }
79
+ export {
80
+ createExpressGuard,
81
+ createExpressProvider
82
+ };
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/provider/express.ts","../../../src/middleware/express.ts"],"sourcesContent":["import type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n RequestHandler,\n} from \"express\";\n\nexport interface WebHandler {\n handle(request: Request): Promise<Response>;\n}\n\nexport function createExpressProvider(provider: WebHandler): RequestHandler {\n return async function trustlineProvider(\n request: ExpressRequest,\n response: ExpressResponse,\n ) {\n const origin = `${request.protocol}://${request.get(\"host\") ?? \"localhost\"}`;\n const url = new URL(request.originalUrl || request.url, origin);\n const body =\n request.method === \"GET\" || request.method === \"HEAD\"\n ? undefined\n : await readBody(request);\n\n const providerResponse = await provider.handle(\n new Request(url.toString(), {\n method: request.method,\n headers: request.headers as HeadersInit,\n body,\n }),\n );\n\n response.status(providerResponse.status);\n providerResponse.headers.forEach((value, key) => {\n response.setHeader(key, value);\n });\n response.send(await providerResponse.text());\n };\n}\n\nfunction readBody(request: ExpressRequest): Promise<string | undefined> {\n return new Promise((resolve, reject) => {\n const chunks: Uint8Array[] = [];\n request.on(\"data\", (chunk) => {\n chunks.push(typeof chunk === \"string\" ? Buffer.from(chunk) : chunk);\n });\n request.on(\"end\", () => {\n resolve(\n chunks.length > 0 ? Buffer.concat(chunks).toString(\"utf8\") : undefined,\n );\n });\n request.on(\"error\", reject);\n });\n}\n","import type { Request, RequestHandler, Response } from \"express\";\n\nimport { AuthError } from \"../core/errors\";\nimport type { ServiceIdentity } from \"../core/token\";\n\nexport interface GuardVerifier {\n verify(token: string): Promise<ServiceIdentity>;\n}\n\nexport interface TrustlineRequest extends Request {\n trustline?: ServiceIdentity;\n}\n\nexport function createExpressGuard(guard: GuardVerifier): RequestHandler {\n return async function trustlineGuard(\n request: Request,\n response: Response,\n next,\n ) {\n const token = getBearerToken(request.headers.authorization);\n\n if (!token) {\n response.status(401).json({\n error: \"missing_token\",\n message: \"Missing bearer token\",\n });\n return;\n }\n\n try {\n const identity = await guard.verify(token);\n (request as TrustlineRequest).trustline = identity;\n next();\n } catch (error) {\n const authError =\n error instanceof AuthError\n ? error\n : new AuthError(\n \"invalid_token\",\n \"Token verification failed\",\n 401,\n error,\n );\n response.status(authError.status).json({\n error: authError.code,\n message: authError.message,\n });\n }\n };\n}\n\nfunction getBearerToken(header: string | undefined): string | null {\n if (!header) {\n return null;\n }\n\n const [scheme, value] = header.split(/\\s+/, 2);\n if (scheme?.toLowerCase() !== \"bearer\" || !value) {\n return null;\n }\n\n return value;\n}\n"],"mappings":";;;;;AAUO,SAAS,sBAAsB,UAAsC;AAC1E,SAAO,eAAe,kBACpB,SACA,UACA;AACA,UAAM,SAAS,GAAG,QAAQ,QAAQ,MAAM,QAAQ,IAAI,MAAM,KAAK,WAAW;AAC1E,UAAM,MAAM,IAAI,IAAI,QAAQ,eAAe,QAAQ,KAAK,MAAM;AAC9D,UAAM,OACJ,QAAQ,WAAW,SAAS,QAAQ,WAAW,SAC3C,SACA,MAAM,SAAS,OAAO;AAE5B,UAAM,mBAAmB,MAAM,SAAS;AAAA,MACtC,IAAI,QAAQ,IAAI,SAAS,GAAG;AAAA,QAC1B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB;AAAA,MACF,CAAC;AAAA,IACH;AAEA,aAAS,OAAO,iBAAiB,MAAM;AACvC,qBAAiB,QAAQ,QAAQ,CAAC,OAAO,QAAQ;AAC/C,eAAS,UAAU,KAAK,KAAK;AAAA,IAC/B,CAAC;AACD,aAAS,KAAK,MAAM,iBAAiB,KAAK,CAAC;AAAA,EAC7C;AACF;AAEA,SAAS,SAAS,SAAsD;AACtE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAuB,CAAC;AAC9B,YAAQ,GAAG,QAAQ,CAAC,UAAU;AAC5B,aAAO,KAAK,OAAO,UAAU,WAAW,OAAO,KAAK,KAAK,IAAI,KAAK;AAAA,IACpE,CAAC;AACD,YAAQ,GAAG,OAAO,MAAM;AACtB;AAAA,QACE,OAAO,SAAS,IAAI,OAAO,OAAO,MAAM,EAAE,SAAS,MAAM,IAAI;AAAA,MAC/D;AAAA,IACF,CAAC;AACD,YAAQ,GAAG,SAAS,MAAM;AAAA,EAC5B,CAAC;AACH;;;ACtCO,SAAS,mBAAmB,OAAsC;AACvE,SAAO,eAAe,eACpB,SACA,UACA,MACA;AACA,UAAM,QAAQ,eAAe,QAAQ,QAAQ,aAAa;AAE1D,QAAI,CAAC,OAAO;AACV,eAAS,OAAO,GAAG,EAAE,KAAK;AAAA,QACxB,OAAO;AAAA,QACP,SAAS;AAAA,MACX,CAAC;AACD;AAAA,IACF;AAEA,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,OAAO,KAAK;AACzC,MAAC,QAA6B,YAAY;AAC1C,WAAK;AAAA,IACP,SAAS,OAAO;AACd,YAAM,YACJ,iBAAiB,YACb,QACA,IAAI;AAAA,QACF;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACN,eAAS,OAAO,UAAU,MAAM,EAAE,KAAK;AAAA,QACrC,OAAO,UAAU;AAAA,QACjB,SAAS,UAAU;AAAA,MACrB,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,SAAS,eAAe,QAA2C;AACjE,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,QAAQ,KAAK,IAAI,OAAO,MAAM,OAAO,CAAC;AAC7C,MAAI,QAAQ,YAAY,MAAM,YAAY,CAAC,OAAO;AAChD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/frameworks/fastify/index.ts
21
+ var fastify_exports = {};
22
+ __export(fastify_exports, {
23
+ createFastifyGuard: () => createFastifyGuard,
24
+ createFastifyProvider: () => createFastifyProvider
25
+ });
26
+ module.exports = __toCommonJS(fastify_exports);
27
+
28
+ // src/core/errors.ts
29
+ var AuthError = class extends Error {
30
+ code;
31
+ status;
32
+ cause;
33
+ constructor(code, message, status, cause) {
34
+ super(message);
35
+ this.name = "AuthError";
36
+ this.code = code;
37
+ this.status = status;
38
+ this.cause = cause;
39
+ }
40
+ };
41
+
42
+ // src/provider/http.ts
43
+ function createHeaders(headers) {
44
+ const normalized = new Headers();
45
+ for (const [key, value] of Object.entries(headers)) {
46
+ if (typeof value === "string") {
47
+ normalized.set(key, value);
48
+ }
49
+ if (Array.isArray(value)) {
50
+ normalized.set(key, value.join(", "));
51
+ }
52
+ }
53
+ return normalized;
54
+ }
55
+ function serializeRequestBody(body) {
56
+ if (typeof body === "string") {
57
+ return body;
58
+ }
59
+ if (!body || typeof body !== "object") {
60
+ return void 0;
61
+ }
62
+ const params = new URLSearchParams();
63
+ for (const [key, value] of Object.entries(body)) {
64
+ if (value === void 0 || value === null) {
65
+ continue;
66
+ }
67
+ if (Array.isArray(value)) {
68
+ for (const item of value) {
69
+ params.append(key, String(item));
70
+ }
71
+ continue;
72
+ }
73
+ params.set(key, String(value));
74
+ }
75
+ return params.toString();
76
+ }
77
+ async function writeFastifyResponse(reply, response) {
78
+ reply.code(response.status);
79
+ response.headers.forEach((value, key) => {
80
+ reply.header(key, value);
81
+ });
82
+ reply.send(await response.text());
83
+ }
84
+
85
+ // src/frameworks/fastify/index.ts
86
+ function createFastifyProvider(provider) {
87
+ return async (fastify) => {
88
+ fastify.get("/.well-known/jwks.json", async (request, reply) => {
89
+ const response = await provider.handle(
90
+ new Request(buildRequestUrl(request), {
91
+ method: "GET",
92
+ headers: createHeaders(request.headers)
93
+ })
94
+ );
95
+ await writeFastifyResponse(reply, response);
96
+ });
97
+ fastify.post("/token", async (request, reply) => {
98
+ const response = await provider.handle(
99
+ new Request(buildRequestUrl(request), {
100
+ method: "POST",
101
+ headers: createHeaders(request.headers),
102
+ body: serializeRequestBody(request.body)
103
+ })
104
+ );
105
+ await writeFastifyResponse(reply, response);
106
+ });
107
+ };
108
+ }
109
+ function createFastifyGuard(guard) {
110
+ return async function trustlineFastifyGuard(request, reply) {
111
+ const token = getBearerToken(request.headers.authorization);
112
+ if (!token) {
113
+ reply.code(401).send({
114
+ error: "missing_token",
115
+ message: "Missing bearer token"
116
+ });
117
+ return;
118
+ }
119
+ try {
120
+ const identity = await guard.verify(token);
121
+ request.trustline = identity;
122
+ } catch (error) {
123
+ const authError = error instanceof AuthError ? error : new AuthError(
124
+ "invalid_token",
125
+ "Token verification failed",
126
+ 401,
127
+ error
128
+ );
129
+ reply.code(authError.status).send({
130
+ error: authError.code,
131
+ message: authError.message
132
+ });
133
+ }
134
+ };
135
+ }
136
+ function buildRequestUrl(request) {
137
+ const protocol = request.protocol ?? "http";
138
+ const host = Array.isArray(request.headers.host) ? request.headers.host[0] : request.headers.host;
139
+ const path = request.raw?.url ?? request.url ?? "/";
140
+ return new URL(path, `${protocol}://${host ?? "localhost"}`).toString();
141
+ }
142
+ function getBearerToken(header) {
143
+ const value = Array.isArray(header) ? header[0] : header;
144
+ if (!value) {
145
+ return null;
146
+ }
147
+ const [scheme, token] = value.split(/\s+/, 2);
148
+ if (scheme?.toLowerCase() !== "bearer" || !token) {
149
+ return null;
150
+ }
151
+ return token;
152
+ }
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ createFastifyGuard,
156
+ createFastifyProvider
157
+ });
158
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/frameworks/fastify/index.ts","../../../src/core/errors.ts","../../../src/provider/http.ts"],"sourcesContent":["import type { FastifyPluginAsync } from \"fastify\";\n\nimport { AuthError } from \"../../core/errors\";\nimport type { ServiceIdentity } from \"../../core/token\";\nimport type { Provider } from \"../../provider\";\nimport {\n createHeaders,\n serializeRequestBody,\n writeFastifyResponse,\n} from \"../../provider/http\";\n\nexport interface GuardVerifier {\n verify(token: string): Promise<ServiceIdentity>;\n}\n\nexport interface TrustlineFastifyRequest {\n trustline?: ServiceIdentity;\n}\n\nexport function createFastifyProvider(provider: Provider): FastifyPluginAsync {\n return async (fastify) => {\n fastify.get(\"/.well-known/jwks.json\", async (request, reply) => {\n const response = await provider.handle(\n new Request(buildRequestUrl(request), {\n method: \"GET\",\n headers: createHeaders(request.headers),\n }),\n );\n await writeFastifyResponse(reply, response);\n });\n\n fastify.post(\"/token\", async (request, reply) => {\n const response = await provider.handle(\n new Request(buildRequestUrl(request), {\n method: \"POST\",\n headers: createHeaders(request.headers),\n body: serializeRequestBody(request.body),\n }),\n );\n await writeFastifyResponse(reply, response);\n });\n };\n}\n\nexport function createFastifyGuard(guard: GuardVerifier) {\n return async function trustlineFastifyGuard(\n request: { headers: { authorization?: string | string[] } },\n reply: {\n code(status: number): { send(payload: unknown): void };\n send?(payload: unknown): void;\n },\n ) {\n const token = getBearerToken(request.headers.authorization);\n\n if (!token) {\n reply.code(401).send({\n error: \"missing_token\",\n message: \"Missing bearer token\",\n });\n return;\n }\n\n try {\n const identity = await guard.verify(token);\n (request as TrustlineFastifyRequest).trustline = identity;\n } catch (error) {\n const authError =\n error instanceof AuthError\n ? error\n : new AuthError(\n \"invalid_token\",\n \"Token verification failed\",\n 401,\n error,\n );\n reply.code(authError.status).send({\n error: authError.code,\n message: authError.message,\n });\n }\n };\n}\n\nfunction buildRequestUrl(request: {\n protocol?: string;\n headers: { host?: string | string[] };\n raw?: { url?: string };\n url?: string;\n}): string {\n const protocol = request.protocol ?? \"http\";\n const host = Array.isArray(request.headers.host)\n ? request.headers.host[0]\n : request.headers.host;\n const path = request.raw?.url ?? request.url ?? \"/\";\n return new URL(path, `${protocol}://${host ?? \"localhost\"}`).toString();\n}\n\nfunction getBearerToken(header: string | string[] | undefined): string | null {\n const value = Array.isArray(header) ? header[0] : header;\n if (!value) {\n return null;\n }\n\n const [scheme, token] = value.split(/\\s+/, 2);\n if (scheme?.toLowerCase() !== \"bearer\" || !token) {\n return null;\n }\n\n return token;\n}\n","export type AuthErrorCode =\n | \"missing_token\"\n | \"invalid_token\"\n | \"invalid_issuer\"\n | \"invalid_audience\"\n | \"invalid_scope\"\n | \"invalid_env\"\n | \"jwks_fetch_failed\";\n\nexport class AuthError extends Error {\n public readonly code: AuthErrorCode;\n public readonly status: number;\n public readonly cause?: unknown;\n\n constructor(\n code: AuthErrorCode,\n message: string,\n status: number,\n cause?: unknown,\n ) {\n super(message);\n this.name = \"AuthError\";\n this.code = code;\n this.status = status;\n this.cause = cause;\n }\n}\n","export interface WebHandler {\n handle(request: Request): Promise<Response>;\n}\n\nexport function createHeaders(headers: Record<string, unknown>): Headers {\n const normalized = new Headers();\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === \"string\") {\n normalized.set(key, value);\n }\n\n if (Array.isArray(value)) {\n normalized.set(key, value.join(\", \"));\n }\n }\n return normalized;\n}\n\nexport function serializeRequestBody(body: unknown): string | undefined {\n if (typeof body === \"string\") {\n return body;\n }\n\n if (!body || typeof body !== \"object\") {\n return undefined;\n }\n\n const params = new URLSearchParams();\n for (const [key, value] of Object.entries(body)) {\n if (value === undefined || value === null) {\n continue;\n }\n\n if (Array.isArray(value)) {\n for (const item of value) {\n params.append(key, String(item));\n }\n continue;\n }\n\n params.set(key, String(value));\n }\n\n return params.toString();\n}\n\nexport async function writeFastifyResponse(\n reply: {\n code(status: number): void;\n header(name: string, value: string): void;\n send(payload: string): void;\n },\n response: Response,\n): Promise<void> {\n reply.code(response.status);\n response.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n reply.send(await response.text());\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSO,IAAM,YAAN,cAAwB,MAAM;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AAAA,EAEhB,YACE,MACA,SACA,QACA,OACA;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AACF;;;ACtBO,SAAS,cAAc,SAA2C;AACvE,QAAM,aAAa,IAAI,QAAQ;AAC/B,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,OAAO,GAAG;AAClD,QAAI,OAAO,UAAU,UAAU;AAC7B,iBAAW,IAAI,KAAK,KAAK;AAAA,IAC3B;AAEA,QAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,iBAAW,IAAI,KAAK,MAAM,KAAK,IAAI,CAAC;AAAA,IACtC;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,qBAAqB,MAAmC;AACtE,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI,gBAAgB;AACnC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,QAAI,UAAU,UAAa,UAAU,MAAM;AACzC;AAAA,IACF;AAEA,QAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,iBAAW,QAAQ,OAAO;AACxB,eAAO,OAAO,KAAK,OAAO,IAAI,CAAC;AAAA,MAC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AC,QAAoC,YAAY;AAAA,IACnD,SAAS,OAAO;AACd,YAAM,YACJ,iBAAiB,YACb,QACA,IAAI;AAAA,QACF;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACN,YAAM,KAAK,UAAU,MAAM,EAAE,KAAK;AAAA,QAChC,OAAO,UAAU;AAAA,QACjB,SAAS,UAAU;AAAA,MACrB,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,SAAS,gBAAgB,SAKd;AACT,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,OAAO,MAAM,QAAQ,QAAQ,QAAQ,IAAI,IAC3C,QAAQ,QAAQ,KAAK,CAAC,IACtB,QAAQ,QAAQ;AACpB,QAAM,OAAO,QAAQ,KAAK,OAAO,QAAQ,OAAO;AAChD,SAAO,IAAI,IAAI,MAAM,GAAG,QAAQ,MAAM,QAAQ,WAAW,EAAE,EAAE,SAAS;AACxE;AAEA,SAAS,eAAe,QAAsD;AAC5E,QAAM,QAAQ,MAAM,QAAQ,MAAM,IAAI,OAAO,CAAC,IAAI;AAClD,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,QAAQ,KAAK,IAAI,MAAM,MAAM,OAAO,CAAC;AAC5C,MAAI,QAAQ,YAAY,MAAM,YAAY,CAAC,OAAO;AAChD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,25 @@
1
+ import { FastifyPluginAsync } from 'fastify';
2
+ import { S as ServiceIdentity } from '../../token-BtfYGd9K.cjs';
3
+ import { P as Provider } from '../../index-DqkKZOlH.cjs';
4
+ import 'jose';
5
+ import '../../interface-BzT_DC3u.cjs';
6
+
7
+ interface GuardVerifier {
8
+ verify(token: string): Promise<ServiceIdentity>;
9
+ }
10
+ interface TrustlineFastifyRequest {
11
+ trustline?: ServiceIdentity;
12
+ }
13
+ declare function createFastifyProvider(provider: Provider): FastifyPluginAsync;
14
+ declare function createFastifyGuard(guard: GuardVerifier): (request: {
15
+ headers: {
16
+ authorization?: string | string[];
17
+ };
18
+ }, reply: {
19
+ code(status: number): {
20
+ send(payload: unknown): void;
21
+ };
22
+ send?(payload: unknown): void;
23
+ }) => Promise<void>;
24
+
25
+ export { type GuardVerifier, type TrustlineFastifyRequest, createFastifyGuard, createFastifyProvider };
@@ -0,0 +1,25 @@
1
+ import { FastifyPluginAsync } from 'fastify';
2
+ import { S as ServiceIdentity } from '../../token-BtfYGd9K.js';
3
+ import { P as Provider } from '../../index-Dc4GFume.js';
4
+ import 'jose';
5
+ import '../../interface-BzT_DC3u.js';
6
+
7
+ interface GuardVerifier {
8
+ verify(token: string): Promise<ServiceIdentity>;
9
+ }
10
+ interface TrustlineFastifyRequest {
11
+ trustline?: ServiceIdentity;
12
+ }
13
+ declare function createFastifyProvider(provider: Provider): FastifyPluginAsync;
14
+ declare function createFastifyGuard(guard: GuardVerifier): (request: {
15
+ headers: {
16
+ authorization?: string | string[];
17
+ };
18
+ }, reply: {
19
+ code(status: number): {
20
+ send(payload: unknown): void;
21
+ };
22
+ send?(payload: unknown): void;
23
+ }) => Promise<void>;
24
+
25
+ export { type GuardVerifier, type TrustlineFastifyRequest, createFastifyGuard, createFastifyProvider };
@@ -0,0 +1,120 @@
1
+ import {
2
+ AuthError
3
+ } from "../../chunk-GF3NKEEK.js";
4
+
5
+ // src/provider/http.ts
6
+ function createHeaders(headers) {
7
+ const normalized = new Headers();
8
+ for (const [key, value] of Object.entries(headers)) {
9
+ if (typeof value === "string") {
10
+ normalized.set(key, value);
11
+ }
12
+ if (Array.isArray(value)) {
13
+ normalized.set(key, value.join(", "));
14
+ }
15
+ }
16
+ return normalized;
17
+ }
18
+ function serializeRequestBody(body) {
19
+ if (typeof body === "string") {
20
+ return body;
21
+ }
22
+ if (!body || typeof body !== "object") {
23
+ return void 0;
24
+ }
25
+ const params = new URLSearchParams();
26
+ for (const [key, value] of Object.entries(body)) {
27
+ if (value === void 0 || value === null) {
28
+ continue;
29
+ }
30
+ if (Array.isArray(value)) {
31
+ for (const item of value) {
32
+ params.append(key, String(item));
33
+ }
34
+ continue;
35
+ }
36
+ params.set(key, String(value));
37
+ }
38
+ return params.toString();
39
+ }
40
+ async function writeFastifyResponse(reply, response) {
41
+ reply.code(response.status);
42
+ response.headers.forEach((value, key) => {
43
+ reply.header(key, value);
44
+ });
45
+ reply.send(await response.text());
46
+ }
47
+
48
+ // src/frameworks/fastify/index.ts
49
+ function createFastifyProvider(provider) {
50
+ return async (fastify) => {
51
+ fastify.get("/.well-known/jwks.json", async (request, reply) => {
52
+ const response = await provider.handle(
53
+ new Request(buildRequestUrl(request), {
54
+ method: "GET",
55
+ headers: createHeaders(request.headers)
56
+ })
57
+ );
58
+ await writeFastifyResponse(reply, response);
59
+ });
60
+ fastify.post("/token", async (request, reply) => {
61
+ const response = await provider.handle(
62
+ new Request(buildRequestUrl(request), {
63
+ method: "POST",
64
+ headers: createHeaders(request.headers),
65
+ body: serializeRequestBody(request.body)
66
+ })
67
+ );
68
+ await writeFastifyResponse(reply, response);
69
+ });
70
+ };
71
+ }
72
+ function createFastifyGuard(guard) {
73
+ return async function trustlineFastifyGuard(request, reply) {
74
+ const token = getBearerToken(request.headers.authorization);
75
+ if (!token) {
76
+ reply.code(401).send({
77
+ error: "missing_token",
78
+ message: "Missing bearer token"
79
+ });
80
+ return;
81
+ }
82
+ try {
83
+ const identity = await guard.verify(token);
84
+ request.trustline = identity;
85
+ } catch (error) {
86
+ const authError = error instanceof AuthError ? error : new AuthError(
87
+ "invalid_token",
88
+ "Token verification failed",
89
+ 401,
90
+ error
91
+ );
92
+ reply.code(authError.status).send({
93
+ error: authError.code,
94
+ message: authError.message
95
+ });
96
+ }
97
+ };
98
+ }
99
+ function buildRequestUrl(request) {
100
+ const protocol = request.protocol ?? "http";
101
+ const host = Array.isArray(request.headers.host) ? request.headers.host[0] : request.headers.host;
102
+ const path = request.raw?.url ?? request.url ?? "/";
103
+ return new URL(path, `${protocol}://${host ?? "localhost"}`).toString();
104
+ }
105
+ function getBearerToken(header) {
106
+ const value = Array.isArray(header) ? header[0] : header;
107
+ if (!value) {
108
+ return null;
109
+ }
110
+ const [scheme, token] = value.split(/\s+/, 2);
111
+ if (scheme?.toLowerCase() !== "bearer" || !token) {
112
+ return null;
113
+ }
114
+ return token;
115
+ }
116
+ export {
117
+ createFastifyGuard,
118
+ createFastifyProvider
119
+ };
120
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/provider/http.ts","../../../src/frameworks/fastify/index.ts"],"sourcesContent":["export interface WebHandler {\n handle(request: Request): Promise<Response>;\n}\n\nexport function createHeaders(headers: Record<string, unknown>): Headers {\n const normalized = new Headers();\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === \"string\") {\n normalized.set(key, value);\n }\n\n if (Array.isArray(value)) {\n normalized.set(key, value.join(\", \"));\n }\n }\n return normalized;\n}\n\nexport function serializeRequestBody(body: unknown): string | undefined {\n if (typeof body === \"string\") {\n return body;\n }\n\n if (!body || typeof body !== \"object\") {\n return undefined;\n }\n\n const params = new URLSearchParams();\n for (const [key, value] of Object.entries(body)) {\n if (value === undefined || value === null) {\n continue;\n }\n\n if (Array.isArray(value)) {\n for (const item of value) {\n params.append(key, String(item));\n }\n continue;\n }\n\n params.set(key, String(value));\n }\n\n return params.toString();\n}\n\nexport async function writeFastifyResponse(\n reply: {\n code(status: number): void;\n header(name: string, value: string): void;\n send(payload: string): void;\n },\n response: Response,\n): Promise<void> {\n reply.code(response.status);\n response.headers.forEach((value, key) => {\n reply.header(key, value);\n });\n reply.send(await response.text());\n}\n","import type { FastifyPluginAsync } from \"fastify\";\n\nimport { AuthError } from \"../../core/errors\";\nimport type { ServiceIdentity } from \"../../core/token\";\nimport type { Provider } from \"../../provider\";\nimport {\n createHeaders,\n serializeRequestBody,\n writeFastifyResponse,\n} from \"../../provider/http\";\n\nexport interface GuardVerifier {\n verify(token: string): Promise<ServiceIdentity>;\n}\n\nexport interface TrustlineFastifyRequest {\n trustline?: ServiceIdentity;\n}\n\nexport function createFastifyProvider(provider: Provider): FastifyPluginAsync {\n return async (fastify) => {\n fastify.get(\"/.well-known/jwks.json\", async (request, reply) => {\n const response = await provider.handle(\n new Request(buildRequestUrl(request), {\n method: \"GET\",\n headers: createHeaders(request.headers),\n }),\n );\n await writeFastifyResponse(reply, response);\n });\n\n fastify.post(\"/token\", async (request, reply) => {\n const response = await provider.handle(\n new Request(buildRequestUrl(request), {\n method: \"POST\",\n headers: createHeaders(request.headers),\n body: serializeRequestBody(request.body),\n }),\n );\n await writeFastifyResponse(reply, response);\n });\n };\n}\n\nexport function createFastifyGuard(guard: GuardVerifier) {\n return async function trustlineFastifyGuard(\n request: { headers: { authorization?: string | string[] } },\n reply: {\n code(status: number): { send(payload: unknown): void };\n send?(payload: unknown): void;\n },\n ) {\n const token = getBearerToken(request.headers.authorization);\n\n if (!token) {\n reply.code(401).send({\n error: \"missing_token\",\n message: \"Missing bearer token\",\n });\n return;\n }\n\n try {\n const identity = await guard.verify(token);\n (request as TrustlineFastifyRequest).trustline = identity;\n } catch (error) {\n const authError =\n error instanceof AuthError\n ? error\n : new AuthError(\n \"invalid_token\",\n \"Token verification failed\",\n 401,\n error,\n );\n reply.code(authError.status).send({\n error: authError.code,\n message: authError.message,\n });\n }\n };\n}\n\nfunction buildRequestUrl(request: {\n protocol?: string;\n headers: { host?: string | string[] };\n raw?: { url?: string };\n url?: string;\n}): string {\n const protocol = request.protocol ?? \"http\";\n const host = Array.isArray(request.headers.host)\n ? request.headers.host[0]\n : request.headers.host;\n const path = request.raw?.url ?? request.url ?? \"/\";\n return new URL(path, `${protocol}://${host ?? \"localhost\"}`).toString();\n}\n\nfunction getBearerToken(header: string | string[] | undefined): string | null {\n const value = Array.isArray(header) ? header[0] : header;\n if (!value) {\n return null;\n }\n\n const [scheme, token] = value.split(/\\s+/, 2);\n if (scheme?.toLowerCase() !== \"bearer\" || !token) {\n return null;\n }\n\n return token;\n}\n"],"mappings":";;;;;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AAI,MAAM,GAAG,QAAQ,MAAM,QAAQ,WAAW,EAAE,EAAE,SAAS;AACxE;AAEA,SAAS,eAAe,QAAsD;AAC5E,QAAM,QAAQ,MAAM,QAAQ,MAAM,IAAI,OAAO,CAAC,IAAI;AAClD,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,QAAQ,KAAK,IAAI,MAAM,MAAM,OAAO,CAAC;AAC5C,MAAI,QAAQ,YAAY,MAAM,YAAY,CAAC,OAAO;AAChD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/frameworks/hono/index.ts
21
+ var hono_exports = {};
22
+ __export(hono_exports, {
23
+ createHonoGuard: () => createHonoGuard,
24
+ createHonoProvider: () => createHonoProvider
25
+ });
26
+ module.exports = __toCommonJS(hono_exports);
27
+ var import_hono = require("hono");
28
+ var import_factory = require("hono/factory");
29
+
30
+ // src/core/errors.ts
31
+ var AuthError = class extends Error {
32
+ code;
33
+ status;
34
+ cause;
35
+ constructor(code, message, status, cause) {
36
+ super(message);
37
+ this.name = "AuthError";
38
+ this.code = code;
39
+ this.status = status;
40
+ this.cause = cause;
41
+ }
42
+ };
43
+
44
+ // src/frameworks/hono/index.ts
45
+ function createHonoProvider(provider) {
46
+ const app = new import_hono.Hono();
47
+ app.get(
48
+ "/.well-known/jwks.json",
49
+ async (context) => provider.handle(
50
+ new Request(context.req.raw.url, {
51
+ method: "GET",
52
+ headers: context.req.raw.headers
53
+ })
54
+ )
55
+ );
56
+ app.post(
57
+ "/token",
58
+ async (context) => provider.handle(
59
+ new Request(context.req.raw.url, {
60
+ method: "POST",
61
+ headers: context.req.raw.headers,
62
+ body: context.req.raw.body
63
+ })
64
+ )
65
+ );
66
+ return app;
67
+ }
68
+ function createHonoGuard(guard) {
69
+ return (0, import_factory.createMiddleware)(async (context, next) => {
70
+ const header = context.req.header("authorization");
71
+ const token = getBearerToken(header);
72
+ if (!token) {
73
+ return context.json(
74
+ {
75
+ error: "missing_token",
76
+ message: "Missing bearer token"
77
+ },
78
+ 401
79
+ );
80
+ }
81
+ try {
82
+ const identity = await guard.verify(token);
83
+ context.set("trustline", identity);
84
+ await next();
85
+ } catch (error) {
86
+ const authError = error instanceof AuthError ? error : new AuthError(
87
+ "invalid_token",
88
+ "Token verification failed",
89
+ 401,
90
+ error
91
+ );
92
+ return context.json(
93
+ {
94
+ error: authError.code,
95
+ message: authError.message
96
+ },
97
+ authError.status
98
+ );
99
+ }
100
+ });
101
+ }
102
+ function getBearerToken(header) {
103
+ if (!header) {
104
+ return null;
105
+ }
106
+ const [scheme, token] = header.split(/\s+/, 2);
107
+ if (scheme?.toLowerCase() !== "bearer" || !token) {
108
+ return null;
109
+ }
110
+ return token;
111
+ }
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ createHonoGuard,
115
+ createHonoProvider
116
+ });
117
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/frameworks/hono/index.ts","../../../src/core/errors.ts"],"sourcesContent":["import { Hono } from \"hono\";\nimport { createMiddleware } from \"hono/factory\";\n\nimport { AuthError } from \"../../core/errors\";\nimport type { ServiceIdentity } from \"../../core/token\";\nimport type { Provider } from \"../../provider\";\n\nexport interface GuardVerifier {\n verify(token: string): Promise<ServiceIdentity>;\n}\n\nexport function createHonoProvider(provider: Provider): Hono {\n const app = new Hono();\n\n app.get(\"/.well-known/jwks.json\", async (context) =>\n provider.handle(\n new Request(context.req.raw.url, {\n method: \"GET\",\n headers: context.req.raw.headers,\n }),\n ),\n );\n\n app.post(\"/token\", async (context) =>\n provider.handle(\n new Request(context.req.raw.url, {\n method: \"POST\",\n headers: context.req.raw.headers,\n body: context.req.raw.body,\n }),\n ),\n );\n\n return app;\n}\n\nexport function createHonoGuard(guard: GuardVerifier) {\n return createMiddleware(async (context, next) => {\n const header = context.req.header(\"authorization\");\n const token = getBearerToken(header);\n\n if (!token) {\n return context.json(\n {\n error: \"missing_token\",\n message: \"Missing bearer token\",\n },\n 401,\n );\n }\n\n try {\n const identity = await guard.verify(token);\n context.set(\"trustline\", identity);\n await next();\n } catch (error) {\n const authError =\n error instanceof AuthError\n ? error\n : new AuthError(\n \"invalid_token\",\n \"Token verification failed\",\n 401,\n error,\n );\n return context.json(\n {\n error: authError.code,\n message: authError.message,\n },\n authError.status as 401 | 403,\n );\n }\n });\n}\n\nfunction getBearerToken(header: string | undefined): string | null {\n if (!header) {\n return null;\n }\n\n const [scheme, token] = header.split(/\\s+/, 2);\n if (scheme?.toLowerCase() !== \"bearer\" || !token) {\n return null;\n }\n\n return token;\n}\n","export type AuthErrorCode =\n | \"missing_token\"\n | \"invalid_token\"\n | \"invalid_issuer\"\n | \"invalid_audience\"\n | \"invalid_scope\"\n | \"invalid_env\"\n | \"jwks_fetch_failed\";\n\nexport class AuthError extends Error {\n public readonly code: AuthErrorCode;\n public readonly status: number;\n public readonly cause?: unknown;\n\n constructor(\n code: AuthErrorCode,\n message: string,\n status: number,\n cause?: unknown,\n ) {\n super(message);\n this.name = \"AuthError\";\n this.code = code;\n this.status = status;\n this.cause = cause;\n }\n}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,YAAM,YACJ,iBAAiB,YACb,QACA,IAAI;AAAA,QACF;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACN,aAAO,QAAQ;AAAA,QACb;AAAA,UACE,OAAO,UAAU;AAAA,UACjB,SAAS,UAAU;AAAA,QACrB;AAAA,QACA,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAEA,SAAS,eAAe,QAA2C;AACjE,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,QAAQ,KAAK,IAAI,OAAO,MAAM,OAAO,CAAC;AAC7C,MAAI,QAAQ,YAAY,MAAM,YAAY,CAAC,OAAO;AAChD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,17 @@
1
+ import * as hono from 'hono';
2
+ import { Hono } from 'hono';
3
+ import { S as ServiceIdentity } from '../../token-BtfYGd9K.cjs';
4
+ import { P as Provider } from '../../index-DqkKZOlH.cjs';
5
+ import 'jose';
6
+ import '../../interface-BzT_DC3u.cjs';
7
+
8
+ interface GuardVerifier {
9
+ verify(token: string): Promise<ServiceIdentity>;
10
+ }
11
+ declare function createHonoProvider(provider: Provider): Hono;
12
+ declare function createHonoGuard(guard: GuardVerifier): hono.MiddlewareHandler<any, string, {}, Response | (Response & hono.TypedResponse<{
13
+ error: string;
14
+ message: string;
15
+ }, 401, "json">)>;
16
+
17
+ export { type GuardVerifier, createHonoGuard, createHonoProvider };
@@ -0,0 +1,17 @@
1
+ import * as hono from 'hono';
2
+ import { Hono } from 'hono';
3
+ import { S as ServiceIdentity } from '../../token-BtfYGd9K.js';
4
+ import { P as Provider } from '../../index-Dc4GFume.js';
5
+ import 'jose';
6
+ import '../../interface-BzT_DC3u.js';
7
+
8
+ interface GuardVerifier {
9
+ verify(token: string): Promise<ServiceIdentity>;
10
+ }
11
+ declare function createHonoProvider(provider: Provider): Hono;
12
+ declare function createHonoGuard(guard: GuardVerifier): hono.MiddlewareHandler<any, string, {}, Response | (Response & hono.TypedResponse<{
13
+ error: string;
14
+ message: string;
15
+ }, 401, "json">)>;
16
+
17
+ export { type GuardVerifier, createHonoGuard, createHonoProvider };