@arcblock/did-connect-js 1.29.13 → 1.29.14
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.
- package/esm/adapters/detect.d.mts +11 -0
- package/esm/adapters/detect.mjs +16 -0
- package/esm/adapters/express.d.mts +48 -0
- package/esm/adapters/express.mjs +39 -0
- package/esm/adapters/hono.d.mts +57 -0
- package/esm/adapters/hono.mjs +164 -0
- package/esm/handlers/util.d.mts +13 -11
- package/esm/handlers/wallet.d.mts +7 -5
- package/esm/handlers/wallet.mjs +20 -15
- package/esm/index.d.mts +6 -1
- package/esm/index.mjs +5 -1
- package/esm/package.mjs +1 -0
- package/esm/storage/kv.d.mts +33 -0
- package/esm/storage/kv.mjs +55 -0
- package/esm/types.d.mts +55 -0
- package/esm/types.mjs +1 -0
- package/lib/adapters/detect.cjs +18 -0
- package/lib/adapters/detect.d.cts +11 -0
- package/lib/adapters/express.cjs +41 -0
- package/lib/adapters/express.d.cts +48 -0
- package/lib/adapters/hono.cjs +167 -0
- package/lib/adapters/hono.d.cts +57 -0
- package/lib/handlers/util.d.cts +13 -11
- package/lib/handlers/wallet.cjs +20 -17
- package/lib/handlers/wallet.d.cts +6 -5
- package/lib/index.cjs +12 -1
- package/lib/index.d.cts +6 -1
- package/lib/package.cjs +1 -0
- package/lib/storage/kv.cjs +57 -0
- package/lib/storage/kv.d.cts +33 -0
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +55 -0
- package/package.json +10 -9
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/adapters/detect.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Detect whether an app object is Express or Hono.
|
|
4
|
+
*
|
|
5
|
+
* Express: has set(), engine(), and lazyrouter() or _router
|
|
6
|
+
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
7
|
+
*/
|
|
8
|
+
declare function isExpressApp(app: any): boolean;
|
|
9
|
+
declare function isHonoApp(app: any): boolean;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { isExpressApp, isHonoApp };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/adapters/detect.ts
|
|
2
|
+
/**
|
|
3
|
+
* Detect whether an app object is Express or Hono.
|
|
4
|
+
*
|
|
5
|
+
* Express: has set(), engine(), and lazyrouter() or _router
|
|
6
|
+
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
7
|
+
*/
|
|
8
|
+
function isExpressApp(app) {
|
|
9
|
+
return typeof app?.set === "function" && typeof app?.engine === "function";
|
|
10
|
+
}
|
|
11
|
+
function isHonoApp(app) {
|
|
12
|
+
return typeof app?.fetch === "function" && !isExpressApp(app);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { isExpressApp, isHonoApp };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse, NextFunction } from "../types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/adapters/express.d.ts
|
|
4
|
+
interface ExpressLikeApp {
|
|
5
|
+
use(path: string, ...handlers: any[]): void;
|
|
6
|
+
get(path: string, ...handlers: any[]): void;
|
|
7
|
+
post(path: string, ...handlers: any[]): void;
|
|
8
|
+
}
|
|
9
|
+
interface HandlerFunctions {
|
|
10
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
11
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
12
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
13
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
14
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
15
|
+
ensureContext: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => Promise<void>;
|
|
16
|
+
ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
|
|
17
|
+
}
|
|
18
|
+
interface AttachExpressOptions {
|
|
19
|
+
app: ExpressLikeApp;
|
|
20
|
+
prefix: string;
|
|
21
|
+
action: string;
|
|
22
|
+
handlers: HandlerFunctions;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Register DID Connect routes on an Express-compatible app.
|
|
26
|
+
*
|
|
27
|
+
* Routes registered:
|
|
28
|
+
* - GET/POST `{prefix}/{action}/token` — generate session
|
|
29
|
+
* - GET `{prefix}/{action}/status` — check session status
|
|
30
|
+
* - GET `{prefix}/{action}/timeout` — expire session
|
|
31
|
+
* - GET `{prefix}/{action}/auth` — wallet fetches auth request
|
|
32
|
+
* - POST `{prefix}/{action}/auth` — wallet submits auth response
|
|
33
|
+
* - GET `{prefix}/{action}/auth/submit` — web wallet submit
|
|
34
|
+
*/
|
|
35
|
+
declare function attachExpress({
|
|
36
|
+
app,
|
|
37
|
+
prefix,
|
|
38
|
+
action,
|
|
39
|
+
handlers
|
|
40
|
+
}: AttachExpressOptions): {
|
|
41
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
42
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
43
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
44
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
45
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
export { AttachExpressOptions, ExpressLikeApp, HandlerFunctions, attachExpress };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import cors from "cors";
|
|
2
|
+
|
|
3
|
+
//#region src/adapters/express.ts
|
|
4
|
+
/**
|
|
5
|
+
* Register DID Connect routes on an Express-compatible app.
|
|
6
|
+
*
|
|
7
|
+
* Routes registered:
|
|
8
|
+
* - GET/POST `{prefix}/{action}/token` — generate session
|
|
9
|
+
* - GET `{prefix}/{action}/status` — check session status
|
|
10
|
+
* - GET `{prefix}/{action}/timeout` — expire session
|
|
11
|
+
* - GET `{prefix}/{action}/auth` — wallet fetches auth request
|
|
12
|
+
* - POST `{prefix}/{action}/auth` — wallet submits auth response
|
|
13
|
+
* - GET `{prefix}/{action}/auth/submit` — web wallet submit
|
|
14
|
+
*/
|
|
15
|
+
function attachExpress({ app, prefix, action, handlers }) {
|
|
16
|
+
const pathname = `${prefix}/${action}/auth`;
|
|
17
|
+
const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
|
|
18
|
+
app.use(`${prefix}/${action}`, cors({
|
|
19
|
+
origin: "*",
|
|
20
|
+
optionsSuccessStatus: 204
|
|
21
|
+
}));
|
|
22
|
+
app.get(`${prefix}/${action}/token`, generateSession);
|
|
23
|
+
app.post(`${prefix}/${action}/token`, generateSession);
|
|
24
|
+
app.get(`${prefix}/${action}/status`, ensureContext, checkSession);
|
|
25
|
+
app.get(`${prefix}/${action}/timeout`, ensureContext, expireSession);
|
|
26
|
+
app.get(pathname, ensureContext, ensureSignedJson, onAuthRequest);
|
|
27
|
+
app.post(pathname, ensureContext, ensureSignedJson, onAuthResponse);
|
|
28
|
+
app.get(`${pathname}/submit`, ensureContext, ensureSignedJson, onAuthResponse);
|
|
29
|
+
return {
|
|
30
|
+
generateSession,
|
|
31
|
+
expireSession,
|
|
32
|
+
checkSession,
|
|
33
|
+
onAuthRequest,
|
|
34
|
+
onAuthResponse
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { attachExpress };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse } from "../types.mjs";
|
|
2
|
+
import { HandlerFunctions } from "./express.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/adapters/hono.d.ts
|
|
5
|
+
interface HonoLikeApp {
|
|
6
|
+
get(path: string, handler: (c: any) => any): void;
|
|
7
|
+
post(path: string, handler: (c: any) => any): void;
|
|
8
|
+
use(path: string, handler: any): void;
|
|
9
|
+
}
|
|
10
|
+
interface AttachHonoOptions {
|
|
11
|
+
app: HonoLikeApp;
|
|
12
|
+
prefix: string;
|
|
13
|
+
action: string;
|
|
14
|
+
handlers: HandlerFunctions;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a ConnectRequest from a Hono-like Context.
|
|
18
|
+
*
|
|
19
|
+
* @param c - Hono Context object
|
|
20
|
+
* @param bodyCache - Pre-parsed request body (since c.req.json() is async)
|
|
21
|
+
*/
|
|
22
|
+
declare function createHonoRequest(c: any, bodyCache?: any): ConnectRequest;
|
|
23
|
+
/**
|
|
24
|
+
* Create a ConnectResponse that buffers the handler's response.
|
|
25
|
+
* The Hono route wrapper reads the buffer via _getResult() to return c.json().
|
|
26
|
+
*/
|
|
27
|
+
declare function createHonoResponse(): ConnectResponse & {
|
|
28
|
+
_getResult(): {
|
|
29
|
+
statusCode: number;
|
|
30
|
+
body: any;
|
|
31
|
+
} | null;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Register DID Connect routes on a Hono-compatible app.
|
|
35
|
+
*
|
|
36
|
+
* Routes registered are the same as attachExpress:
|
|
37
|
+
* - GET/POST `{prefix}/{action}/token`
|
|
38
|
+
* - GET `{prefix}/{action}/status`
|
|
39
|
+
* - GET `{prefix}/{action}/timeout`
|
|
40
|
+
* - GET `{prefix}/{action}/auth`
|
|
41
|
+
* - POST `{prefix}/{action}/auth`
|
|
42
|
+
* - GET `{prefix}/{action}/auth/submit`
|
|
43
|
+
*/
|
|
44
|
+
declare function attachHono({
|
|
45
|
+
app,
|
|
46
|
+
prefix,
|
|
47
|
+
action,
|
|
48
|
+
handlers
|
|
49
|
+
}: AttachHonoOptions): {
|
|
50
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
51
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
52
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
53
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
54
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
55
|
+
};
|
|
56
|
+
//#endregion
|
|
57
|
+
export { AttachHonoOptions, HonoLikeApp, attachHono, createHonoRequest, createHonoResponse };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
//#region src/adapters/hono.ts
|
|
2
|
+
/**
|
|
3
|
+
* Parse a raw Cookie header string into a key-value map.
|
|
4
|
+
*/
|
|
5
|
+
function parseCookieHeader(header) {
|
|
6
|
+
const cookies = {};
|
|
7
|
+
if (!header) return cookies;
|
|
8
|
+
for (const pair of header.split(";")) {
|
|
9
|
+
const idx = pair.indexOf("=");
|
|
10
|
+
if (idx > 0) {
|
|
11
|
+
const key = pair.slice(0, idx).trim();
|
|
12
|
+
const val = pair.slice(idx + 1).trim();
|
|
13
|
+
cookies[key] = decodeURIComponent(val);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return cookies;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Simple Accept-Language negotiation matching Express's req.acceptsLanguages().
|
|
20
|
+
* Only needs to handle the two languages used by DID Connect: 'en-US' and 'zh-CN'.
|
|
21
|
+
*/
|
|
22
|
+
function negotiateLanguage(acceptHeader, ...langs) {
|
|
23
|
+
if (!acceptHeader || langs.length === 0) return langs[0] || false;
|
|
24
|
+
const lower = acceptHeader.toLowerCase();
|
|
25
|
+
for (const lang of langs) {
|
|
26
|
+
const prefix = lang.toLowerCase().split("-")[0];
|
|
27
|
+
if (lower.includes(lang.toLowerCase()) || lower.includes(prefix)) return lang;
|
|
28
|
+
}
|
|
29
|
+
return langs[0] || false;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a ConnectRequest from a Hono-like Context.
|
|
33
|
+
*
|
|
34
|
+
* @param c - Hono Context object
|
|
35
|
+
* @param bodyCache - Pre-parsed request body (since c.req.json() is async)
|
|
36
|
+
*/
|
|
37
|
+
function createHonoRequest(c, bodyCache = {}) {
|
|
38
|
+
const url = new URL(c.req.url);
|
|
39
|
+
const rawHeaders = {};
|
|
40
|
+
if (c.req.raw?.headers) c.req.raw.headers.forEach((value, key) => {
|
|
41
|
+
rawHeaders[key.toLowerCase()] = value;
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
body: bodyCache,
|
|
45
|
+
query: Object.fromEntries(url.searchParams.entries()),
|
|
46
|
+
params: typeof c.req.param === "function" ? c.req.param() : {},
|
|
47
|
+
headers: rawHeaders,
|
|
48
|
+
cookies: parseCookieHeader(rawHeaders.cookie || ""),
|
|
49
|
+
protocol: url.protocol.replace(":", ""),
|
|
50
|
+
originalUrl: url.pathname + url.search,
|
|
51
|
+
get(name) {
|
|
52
|
+
return rawHeaders[name.toLowerCase()];
|
|
53
|
+
},
|
|
54
|
+
acceptsLanguages(...langs) {
|
|
55
|
+
return negotiateLanguage(rawHeaders["accept-language"] || "", ...langs);
|
|
56
|
+
},
|
|
57
|
+
raw: c
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create a ConnectResponse that buffers the handler's response.
|
|
62
|
+
* The Hono route wrapper reads the buffer via _getResult() to return c.json().
|
|
63
|
+
*/
|
|
64
|
+
function createHonoResponse() {
|
|
65
|
+
let result = null;
|
|
66
|
+
return {
|
|
67
|
+
jsonp(data) {
|
|
68
|
+
result = {
|
|
69
|
+
statusCode: 200,
|
|
70
|
+
body: data
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
json(data) {
|
|
74
|
+
result = {
|
|
75
|
+
statusCode: 200,
|
|
76
|
+
body: data
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
status(code) {
|
|
80
|
+
return { json(data) {
|
|
81
|
+
result = {
|
|
82
|
+
statusCode: code,
|
|
83
|
+
body: data
|
|
84
|
+
};
|
|
85
|
+
} };
|
|
86
|
+
},
|
|
87
|
+
_getResult() {
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Wrap a middleware chain + handler into a single Hono route handler.
|
|
94
|
+
* Runs middlewares sequentially with manual next() control,
|
|
95
|
+
* then runs the handler, then returns the buffered response.
|
|
96
|
+
*/
|
|
97
|
+
function wrapHandler(middlewares, handler) {
|
|
98
|
+
return async (c) => {
|
|
99
|
+
let body = {};
|
|
100
|
+
try {
|
|
101
|
+
const text = await c.req.text();
|
|
102
|
+
if (text) {
|
|
103
|
+
const contentType = c.req.header("content-type") || "";
|
|
104
|
+
if (contentType.includes("json")) body = JSON.parse(text);
|
|
105
|
+
else if (contentType.includes("urlencoded")) body = Object.fromEntries(new URLSearchParams(text).entries());
|
|
106
|
+
}
|
|
107
|
+
} catch {}
|
|
108
|
+
const req = createHonoRequest(c, body);
|
|
109
|
+
const res = createHonoResponse();
|
|
110
|
+
for (const mw of middlewares) {
|
|
111
|
+
let nextCalled = false;
|
|
112
|
+
await mw(req, res, () => {
|
|
113
|
+
nextCalled = true;
|
|
114
|
+
});
|
|
115
|
+
if (!nextCalled) {
|
|
116
|
+
const r$1 = res._getResult();
|
|
117
|
+
if (r$1) return c.json(r$1.body, r$1.statusCode);
|
|
118
|
+
return c.json({ error: "Unknown error" }, 500);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
await handler(req, res);
|
|
122
|
+
const r = res._getResult();
|
|
123
|
+
return c.json(r?.body ?? {}, r?.statusCode ?? 200);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Register DID Connect routes on a Hono-compatible app.
|
|
128
|
+
*
|
|
129
|
+
* Routes registered are the same as attachExpress:
|
|
130
|
+
* - GET/POST `{prefix}/{action}/token`
|
|
131
|
+
* - GET `{prefix}/{action}/status`
|
|
132
|
+
* - GET `{prefix}/{action}/timeout`
|
|
133
|
+
* - GET `{prefix}/{action}/auth`
|
|
134
|
+
* - POST `{prefix}/{action}/auth`
|
|
135
|
+
* - GET `{prefix}/{action}/auth/submit`
|
|
136
|
+
*/
|
|
137
|
+
function attachHono({ app, prefix, action, handlers }) {
|
|
138
|
+
const pathname = `${prefix}/${action}/auth`;
|
|
139
|
+
const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
|
|
140
|
+
app.use(`${prefix}/${action}/*`, async (c, next) => {
|
|
141
|
+
c.header("Access-Control-Allow-Origin", "*");
|
|
142
|
+
c.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
143
|
+
c.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
144
|
+
if (c.req.method === "OPTIONS") return c.text("", 204);
|
|
145
|
+
await next();
|
|
146
|
+
});
|
|
147
|
+
app.get(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
148
|
+
app.post(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
149
|
+
app.get(`${prefix}/${action}/status`, wrapHandler([ensureContext], checkSession));
|
|
150
|
+
app.get(`${prefix}/${action}/timeout`, wrapHandler([ensureContext], expireSession));
|
|
151
|
+
app.get(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthRequest));
|
|
152
|
+
app.post(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
153
|
+
app.get(`${pathname}/submit`, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
154
|
+
return {
|
|
155
|
+
generateSession,
|
|
156
|
+
expireSession,
|
|
157
|
+
checkSession,
|
|
158
|
+
onAuthRequest,
|
|
159
|
+
onAuthResponse
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
export { attachHono, createHonoRequest, createHonoResponse };
|
package/esm/handlers/util.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse, NextFunction } from "../types.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/handlers/util.d.ts
|
|
2
4
|
declare const errors: Record<string, Record<string, string>>;
|
|
3
|
-
declare const preparePathname: (path: string, req:
|
|
4
|
-
declare const prepareBaseUrl: (req:
|
|
5
|
+
declare const preparePathname: (path: string, req: ConnectRequest) => string;
|
|
6
|
+
declare const prepareBaseUrl: (req: ConnectRequest, params?: Record<string, any>) => string;
|
|
5
7
|
declare const getStepChallenge: () => any;
|
|
6
8
|
declare const parseWalletUA: (userAgent: string) => {
|
|
7
9
|
os: string;
|
|
@@ -26,8 +28,8 @@ interface CreateHandlersOptions {
|
|
|
26
28
|
authenticator: any;
|
|
27
29
|
authPrincipal: any;
|
|
28
30
|
persistentDynamicClaims?: boolean;
|
|
29
|
-
getSignParams?: (req:
|
|
30
|
-
getPathName?: (pathname: string, req:
|
|
31
|
+
getSignParams?: (req: ConnectRequest) => any;
|
|
32
|
+
getPathName?: (pathname: string, req: ConnectRequest) => string;
|
|
31
33
|
options: {
|
|
32
34
|
tokenKey: string;
|
|
33
35
|
encKey: string;
|
|
@@ -55,13 +57,13 @@ declare function createHandlers({
|
|
|
55
57
|
getPathName,
|
|
56
58
|
options
|
|
57
59
|
}: CreateHandlersOptions): {
|
|
58
|
-
generateSession: (req:
|
|
59
|
-
expireSession: (req:
|
|
60
|
-
checkSession: (req:
|
|
61
|
-
onAuthRequest: (req:
|
|
62
|
-
onAuthResponse: (req:
|
|
63
|
-
ensureContext: (req:
|
|
64
|
-
ensureSignedJson: (req:
|
|
60
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
61
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
62
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
63
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
64
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
65
|
+
ensureContext: (req: ConnectRequest, _res: ConnectResponse, next: NextFunction) => Promise<void>;
|
|
66
|
+
ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
|
|
65
67
|
createExtraParams: (locale: string, params: any, extra?: any) => any;
|
|
66
68
|
};
|
|
67
69
|
//#endregion
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse } from "../types.mjs";
|
|
1
2
|
import BaseHandler from "./base.mjs";
|
|
3
|
+
import "../index.mjs";
|
|
2
4
|
|
|
3
5
|
//#region src/handlers/wallet.d.ts
|
|
4
6
|
|
|
@@ -107,11 +109,11 @@ declare class WalletHandlers extends BaseHandler {
|
|
|
107
109
|
authPrincipal?: boolean | string | any;
|
|
108
110
|
persistentDynamicClaims?: boolean;
|
|
109
111
|
}): {
|
|
110
|
-
generateSession: (req:
|
|
111
|
-
expireSession: (req:
|
|
112
|
-
checkSession: (req:
|
|
113
|
-
onAuthRequest: (req:
|
|
114
|
-
onAuthResponse: (req:
|
|
112
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
113
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
114
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
115
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
116
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
115
117
|
};
|
|
116
118
|
}
|
|
117
119
|
//#endregion
|
package/esm/handlers/wallet.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { __require } from "../_virtual/rolldown_runtime.mjs";
|
|
2
2
|
import { require_package } from "../package.mjs";
|
|
3
|
+
import { isHonoApp } from "../adapters/detect.mjs";
|
|
4
|
+
import { attachExpress } from "../adapters/express.mjs";
|
|
5
|
+
import { attachHono } from "../adapters/hono.mjs";
|
|
3
6
|
import base_default from "./base.mjs";
|
|
4
7
|
import createHandlers from "./util.mjs";
|
|
5
|
-
import cors from "cors";
|
|
6
8
|
|
|
7
9
|
//#region src/handlers/wallet.ts
|
|
8
10
|
const debug = __require("debug")(`${require_package().name}:handlers:wallet`);
|
|
@@ -108,24 +110,27 @@ var WalletHandlers = class extends base_default {
|
|
|
108
110
|
tokenStorage: this.tokenStorage,
|
|
109
111
|
authenticator: this.authenticator
|
|
110
112
|
});
|
|
111
|
-
|
|
112
|
-
origin: "*",
|
|
113
|
-
optionsSuccessStatus: 204
|
|
114
|
-
}));
|
|
115
|
-
app.get(`${prefix}/${action}/token`, generateSession);
|
|
116
|
-
app.post(`${prefix}/${action}/token`, generateSession);
|
|
117
|
-
app.get(`${prefix}/${action}/status`, ensureContext, checkSession);
|
|
118
|
-
app.get(`${prefix}/${action}/timeout`, ensureContext, expireSession);
|
|
119
|
-
app.get(pathname, ensureContext, ensureSignedJson, onAuthRequest);
|
|
120
|
-
app.post(pathname, ensureContext, ensureSignedJson, onAuthResponse);
|
|
121
|
-
app.get(`${pathname}/submit`, ensureContext, ensureSignedJson, onAuthResponse);
|
|
122
|
-
return {
|
|
113
|
+
const handlerFns = {
|
|
123
114
|
generateSession,
|
|
124
|
-
expireSession,
|
|
125
115
|
checkSession,
|
|
116
|
+
expireSession,
|
|
126
117
|
onAuthRequest,
|
|
127
|
-
onAuthResponse
|
|
118
|
+
onAuthResponse,
|
|
119
|
+
ensureContext,
|
|
120
|
+
ensureSignedJson
|
|
128
121
|
};
|
|
122
|
+
if (isHonoApp(app)) return attachHono({
|
|
123
|
+
app,
|
|
124
|
+
prefix,
|
|
125
|
+
action,
|
|
126
|
+
handlers: handlerFns
|
|
127
|
+
});
|
|
128
|
+
return attachExpress({
|
|
129
|
+
app,
|
|
130
|
+
prefix,
|
|
131
|
+
action,
|
|
132
|
+
handlers: handlerFns
|
|
133
|
+
});
|
|
129
134
|
}
|
|
130
135
|
};
|
|
131
136
|
var wallet_default = WalletHandlers;
|
package/esm/index.d.mts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { isExpressApp, isHonoApp } from "./adapters/detect.mjs";
|
|
2
|
+
import { ConnectRequest, ConnectResponse, NextFunction } from "./types.mjs";
|
|
3
|
+
import { attachExpress } from "./adapters/express.mjs";
|
|
4
|
+
import { attachHono, createHonoRequest, createHonoResponse } from "./adapters/hono.mjs";
|
|
1
5
|
import WalletAuthenticator from "./authenticator/wallet.mjs";
|
|
6
|
+
import CloudflareKVStorage from "./storage/kv.mjs";
|
|
2
7
|
import WalletHandlers from "./handlers/wallet.mjs";
|
|
3
|
-
export { WalletAuthenticator, WalletHandlers };
|
|
8
|
+
export { CloudflareKVStorage, type ConnectRequest, type ConnectResponse, type NextFunction, WalletAuthenticator, WalletHandlers, attachExpress, attachHono, createHonoRequest, createHonoResponse, isExpressApp, isHonoApp };
|
package/esm/index.mjs
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import wallet_default from "./authenticator/wallet.mjs";
|
|
2
|
+
import { isExpressApp, isHonoApp } from "./adapters/detect.mjs";
|
|
3
|
+
import { attachExpress } from "./adapters/express.mjs";
|
|
4
|
+
import { attachHono, createHonoRequest, createHonoResponse } from "./adapters/hono.mjs";
|
|
2
5
|
import wallet_default$1 from "./handlers/wallet.mjs";
|
|
6
|
+
import kv_default from "./storage/kv.mjs";
|
|
3
7
|
|
|
4
|
-
export { wallet_default as WalletAuthenticator, wallet_default$1 as WalletHandlers };
|
|
8
|
+
export { kv_default as CloudflareKVStorage, wallet_default as WalletAuthenticator, wallet_default$1 as WalletHandlers, attachExpress, attachHono, createHonoRequest, createHonoResponse, isExpressApp, isHonoApp };
|
package/esm/package.mjs
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
|
|
3
|
+
//#region src/storage/kv.d.ts
|
|
4
|
+
interface KVNamespace {
|
|
5
|
+
get(key: string): Promise<string | null>;
|
|
6
|
+
put(key: string, value: string, options?: {
|
|
7
|
+
expirationTtl?: number;
|
|
8
|
+
}): Promise<void>;
|
|
9
|
+
delete(key: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
interface CloudflareKVStorageOptions {
|
|
12
|
+
/** TTL in seconds for KV entries. Default: 300 (5 minutes) */
|
|
13
|
+
ttl?: number;
|
|
14
|
+
/** Key prefix for KV entries. Default: '' */
|
|
15
|
+
prefix?: string;
|
|
16
|
+
}
|
|
17
|
+
declare class CloudflareKVStorage extends EventEmitter {
|
|
18
|
+
private kv;
|
|
19
|
+
private ttl;
|
|
20
|
+
private prefix;
|
|
21
|
+
constructor(kv: KVNamespace, options?: CloudflareKVStorageOptions);
|
|
22
|
+
private key;
|
|
23
|
+
create(token: string, status?: string): Promise<{
|
|
24
|
+
token: string;
|
|
25
|
+
status: string;
|
|
26
|
+
}>;
|
|
27
|
+
read(token: string): Promise<any>;
|
|
28
|
+
update(token: string, updates: Record<string, any>): Promise<any>;
|
|
29
|
+
delete(token: string): Promise<void>;
|
|
30
|
+
exist(token: string, did?: string): Promise<boolean>;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { CloudflareKVStorage as default };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
|
|
3
|
+
//#region src/storage/kv.ts
|
|
4
|
+
var CloudflareKVStorage = class extends EventEmitter {
|
|
5
|
+
constructor(kv, options = {}) {
|
|
6
|
+
super();
|
|
7
|
+
this.kv = kv;
|
|
8
|
+
this.ttl = options.ttl ?? 300;
|
|
9
|
+
this.prefix = options.prefix ?? "";
|
|
10
|
+
}
|
|
11
|
+
key(token) {
|
|
12
|
+
return `${this.prefix}${token}`;
|
|
13
|
+
}
|
|
14
|
+
async create(token, status = "created") {
|
|
15
|
+
const record = {
|
|
16
|
+
token,
|
|
17
|
+
status
|
|
18
|
+
};
|
|
19
|
+
await this.kv.put(this.key(token), JSON.stringify(record), { expirationTtl: this.ttl });
|
|
20
|
+
this.emit("create", record);
|
|
21
|
+
return record;
|
|
22
|
+
}
|
|
23
|
+
async read(token) {
|
|
24
|
+
const raw = await this.kv.get(this.key(token));
|
|
25
|
+
if (!raw) return null;
|
|
26
|
+
return JSON.parse(raw);
|
|
27
|
+
}
|
|
28
|
+
async update(token, updates) {
|
|
29
|
+
const existing = await this.read(token);
|
|
30
|
+
if (!existing) return null;
|
|
31
|
+
delete updates.token;
|
|
32
|
+
const merged = {
|
|
33
|
+
...existing,
|
|
34
|
+
...updates
|
|
35
|
+
};
|
|
36
|
+
await this.kv.put(this.key(token), JSON.stringify(merged), { expirationTtl: this.ttl });
|
|
37
|
+
this.emit("update", merged);
|
|
38
|
+
return merged;
|
|
39
|
+
}
|
|
40
|
+
async delete(token) {
|
|
41
|
+
const existing = await this.read(token);
|
|
42
|
+
if (existing) this.emit("destroy", existing);
|
|
43
|
+
await this.kv.delete(this.key(token));
|
|
44
|
+
}
|
|
45
|
+
async exist(token, did) {
|
|
46
|
+
const record = await this.read(token);
|
|
47
|
+
if (!record) return false;
|
|
48
|
+
if (did) return record.did === did;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var kv_default = CloudflareKVStorage;
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { kv_default as default };
|
package/esm/types.d.mts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Framework-agnostic request interface for DID Connect handlers.
|
|
4
|
+
*
|
|
5
|
+
* Designed to match Express's `req` shape so that Express requests
|
|
6
|
+
* satisfy it natively (zero-wrapping). Hono adapter creates a wrapper
|
|
7
|
+
* that maps Hono Context to this interface.
|
|
8
|
+
*/
|
|
9
|
+
interface ConnectRequest {
|
|
10
|
+
/** Parsed request body (from body-parser or Hono) */
|
|
11
|
+
body: Record<string, any>;
|
|
12
|
+
/** Parsed query string parameters */
|
|
13
|
+
query: Record<string, any>;
|
|
14
|
+
/** URL path parameters (e.g. :action) */
|
|
15
|
+
params: Record<string, any>;
|
|
16
|
+
/** Raw request headers */
|
|
17
|
+
headers: Record<string, string | string[] | undefined>;
|
|
18
|
+
/** Parsed cookies (from cookie-parser or manual parsing) */
|
|
19
|
+
cookies: Record<string, string>;
|
|
20
|
+
/** Request protocol ('http' or 'https') */
|
|
21
|
+
protocol: string;
|
|
22
|
+
/** Full original URL including query string */
|
|
23
|
+
originalUrl: string;
|
|
24
|
+
/** Session context, populated by ensureContext middleware */
|
|
25
|
+
context?: any;
|
|
26
|
+
/** Flag to prevent double monkey-patching in ensureSignedJson */
|
|
27
|
+
ensureSignedJson?: boolean;
|
|
28
|
+
/** Case-insensitive header getter (matches Express req.get) */
|
|
29
|
+
get(name: string): string | undefined;
|
|
30
|
+
/** Content negotiation for locale (matches Express req.acceptsLanguages) */
|
|
31
|
+
acceptsLanguages(...languages: string[]): string | false;
|
|
32
|
+
/** Original framework-specific request object (Express req or Hono Context) */
|
|
33
|
+
raw?: any;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Framework-agnostic response interface for DID Connect handlers.
|
|
37
|
+
*
|
|
38
|
+
* Express: maps directly to res.jsonp/res.json/res.status().json().
|
|
39
|
+
* Hono: uses a buffer pattern — calls are captured, then the outer
|
|
40
|
+
* Hono handler reads the buffer to return c.json().
|
|
41
|
+
*/
|
|
42
|
+
interface ConnectResponse {
|
|
43
|
+
/** Send JSON response (Express JSONP, Hono json) */
|
|
44
|
+
jsonp(data: any): void;
|
|
45
|
+
/** Send JSON response */
|
|
46
|
+
json(data: any): void;
|
|
47
|
+
/** Set status code and return object with json() method */
|
|
48
|
+
status(code: number): {
|
|
49
|
+
json(data: any): void;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Middleware next() callback */
|
|
53
|
+
type NextFunction = () => void;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { ConnectRequest, ConnectResponse, NextFunction };
|
package/esm/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/adapters/detect.ts
|
|
3
|
+
/**
|
|
4
|
+
* Detect whether an app object is Express or Hono.
|
|
5
|
+
*
|
|
6
|
+
* Express: has set(), engine(), and lazyrouter() or _router
|
|
7
|
+
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
8
|
+
*/
|
|
9
|
+
function isExpressApp(app) {
|
|
10
|
+
return typeof app?.set === "function" && typeof app?.engine === "function";
|
|
11
|
+
}
|
|
12
|
+
function isHonoApp(app) {
|
|
13
|
+
return typeof app?.fetch === "function" && !isExpressApp(app);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.isExpressApp = isExpressApp;
|
|
18
|
+
exports.isHonoApp = isHonoApp;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/adapters/detect.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Detect whether an app object is Express or Hono.
|
|
4
|
+
*
|
|
5
|
+
* Express: has set(), engine(), and lazyrouter() or _router
|
|
6
|
+
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
7
|
+
*/
|
|
8
|
+
declare function isExpressApp(app: any): boolean;
|
|
9
|
+
declare function isHonoApp(app: any): boolean;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { isExpressApp, isHonoApp };
|