@arcblock/did-connect-js 1.29.23 → 4.0.0-beta.1
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/{lib/adapters/detect.d.cts → dist/adapters/detect.d.ts} +3 -5
- package/dist/adapters/detect.d.ts.map +1 -0
- package/dist/adapters/detect.js +13 -0
- package/dist/adapters/detect.js.map +1 -0
- package/dist/adapters/express.d.ts +40 -0
- package/dist/adapters/express.d.ts.map +1 -0
- package/dist/adapters/express.js +30 -0
- package/dist/adapters/express.js.map +1 -0
- package/dist/adapters/hono.d.ts +49 -0
- package/dist/adapters/hono.d.ts.map +1 -0
- package/dist/adapters/hono.js +176 -0
- package/dist/adapters/hono.js.map +1 -0
- package/dist/authenticator/base.d.ts +21 -0
- package/dist/authenticator/base.d.ts.map +1 -0
- package/dist/authenticator/base.js +109 -0
- package/dist/authenticator/base.js.map +1 -0
- package/dist/authenticator/wallet.d.ts +221 -0
- package/dist/authenticator/wallet.d.ts.map +1 -0
- package/dist/authenticator/wallet.js +588 -0
- package/dist/authenticator/wallet.js.map +1 -0
- package/dist/handlers/base.d.ts +25 -0
- package/dist/handlers/base.d.ts.map +1 -0
- package/dist/handlers/base.js +48 -0
- package/dist/handlers/base.js.map +1 -0
- package/dist/handlers/util.d.ts +48 -0
- package/dist/handlers/util.d.ts.map +1 -0
- package/dist/handlers/util.js +812 -0
- package/dist/handlers/util.js.map +1 -0
- package/dist/handlers/wallet.d.ts +96 -0
- package/dist/handlers/wallet.d.ts.map +1 -0
- package/dist/handlers/wallet.js +129 -0
- package/dist/handlers/wallet.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.d.ts +14 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +37 -0
- package/dist/protocol.js.map +1 -0
- package/dist/schema/claims.d.ts +17 -0
- package/dist/schema/claims.d.ts.map +1 -0
- package/dist/schema/claims.js +205 -0
- package/dist/schema/claims.js.map +1 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +49 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/storage/kv.d.ts +31 -0
- package/dist/storage/kv.d.ts.map +1 -0
- package/dist/storage/kv.js +54 -0
- package/dist/storage/kv.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +48 -104
- package/LICENSE +0 -13
- package/README.md +0 -210
- package/esm/_virtual/rolldown_runtime.mjs +0 -8
- package/esm/adapters/detect.d.mts +0 -11
- package/esm/adapters/detect.mjs +0 -16
- package/esm/adapters/express.d.mts +0 -48
- package/esm/adapters/express.mjs +0 -39
- package/esm/adapters/hono.d.mts +0 -57
- package/esm/adapters/hono.mjs +0 -164
- package/esm/authenticator/base.d.mts +0 -23
- package/esm/authenticator/base.mjs +0 -88
- package/esm/authenticator/wallet.d.mts +0 -270
- package/esm/authenticator/wallet.mjs +0 -556
- package/esm/handlers/base.d.mts +0 -32
- package/esm/handlers/base.mjs +0 -37
- package/esm/handlers/util.d.mts +0 -70
- package/esm/handlers/util.mjs +0 -739
- package/esm/handlers/wallet.d.mts +0 -120
- package/esm/handlers/wallet.mjs +0 -139
- package/esm/index.d.mts +0 -8
- package/esm/index.mjs +0 -8
- package/esm/package.mjs +0 -118
- package/esm/protocol.d.mts +0 -15
- package/esm/protocol.mjs +0 -40
- package/esm/schema/claims.d.mts +0 -18
- package/esm/schema/claims.mjs +0 -154
- package/esm/schema/index.d.mts +0 -9
- package/esm/schema/index.mjs +0 -49
- package/esm/storage/kv.d.mts +0 -33
- package/esm/storage/kv.mjs +0 -55
- package/esm/types.d.mts +0 -55
- package/esm/types.mjs +0 -1
- package/lib/_virtual/rolldown_runtime.cjs +0 -31
- package/lib/adapters/detect.cjs +0 -18
- package/lib/adapters/express.cjs +0 -41
- package/lib/adapters/express.d.cts +0 -48
- package/lib/adapters/hono.cjs +0 -167
- package/lib/adapters/hono.d.cts +0 -57
- package/lib/authenticator/base.cjs +0 -91
- package/lib/authenticator/base.d.cts +0 -23
- package/lib/authenticator/wallet.cjs +0 -564
- package/lib/authenticator/wallet.d.cts +0 -270
- package/lib/handlers/base.cjs +0 -40
- package/lib/handlers/base.d.cts +0 -32
- package/lib/handlers/util.cjs +0 -759
- package/lib/handlers/util.d.cts +0 -70
- package/lib/handlers/wallet.cjs +0 -139
- package/lib/handlers/wallet.d.cts +0 -119
- package/lib/index.cjs +0 -16
- package/lib/index.d.cts +0 -8
- package/lib/package.cjs +0 -121
- package/lib/protocol.cjs +0 -46
- package/lib/protocol.d.cts +0 -15
- package/lib/schema/claims.cjs +0 -156
- package/lib/schema/claims.d.cts +0 -18
- package/lib/schema/index.cjs +0 -52
- package/lib/schema/index.d.cts +0 -9
- package/lib/storage/kv.cjs +0 -57
- package/lib/storage/kv.d.cts +0 -33
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +0 -55
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
//#region src/adapters/detect.d.ts
|
|
2
1
|
/**
|
|
3
2
|
* Detect whether an app object is Express or Hono.
|
|
4
3
|
*
|
|
5
4
|
* Express: has set(), engine(), and lazyrouter() or _router
|
|
6
5
|
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
7
6
|
*/
|
|
8
|
-
declare function isExpressApp(app: any): boolean;
|
|
9
|
-
declare function isHonoApp(app: any): boolean;
|
|
10
|
-
//#
|
|
11
|
-
export { isExpressApp, isHonoApp };
|
|
7
|
+
export declare function isExpressApp(app: any): boolean;
|
|
8
|
+
export declare function isHonoApp(app: any): boolean;
|
|
9
|
+
//# sourceMappingURL=detect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../../src/adapters/detect.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAE9C;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAE3C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect whether an app object is Express or Hono.
|
|
3
|
+
*
|
|
4
|
+
* Express: has set(), engine(), and lazyrouter() or _router
|
|
5
|
+
* Hono: has fetch() method + basePath property, no Express set/engine
|
|
6
|
+
*/
|
|
7
|
+
export function isExpressApp(app) {
|
|
8
|
+
return typeof app?.set === 'function' && typeof app?.engine === 'function';
|
|
9
|
+
}
|
|
10
|
+
export function isHonoApp(app) {
|
|
11
|
+
return typeof app?.fetch === 'function' && !isExpressApp(app);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=detect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect.js","sourceRoot":"","sources":["../../src/adapters/detect.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,UAAU,YAAY,CAAC,GAAQ;IACnC,OAAO,OAAO,GAAG,EAAE,GAAG,KAAK,UAAU,IAAI,OAAO,GAAG,EAAE,MAAM,KAAK,UAAU,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,GAAQ;IAChC,OAAO,OAAO,GAAG,EAAE,KAAK,KAAK,UAAU,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ConnectRequest, ConnectResponse, NextFunction } from '../types.js';
|
|
2
|
+
export interface ExpressLikeApp {
|
|
3
|
+
use(path: string, ...handlers: any[]): void;
|
|
4
|
+
get(path: string, ...handlers: any[]): void;
|
|
5
|
+
post(path: string, ...handlers: any[]): void;
|
|
6
|
+
}
|
|
7
|
+
export interface HandlerFunctions {
|
|
8
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
9
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
10
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
11
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
12
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
13
|
+
ensureContext: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => Promise<void>;
|
|
14
|
+
ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface AttachExpressOptions {
|
|
17
|
+
app: ExpressLikeApp;
|
|
18
|
+
prefix: string;
|
|
19
|
+
action: string;
|
|
20
|
+
handlers: HandlerFunctions;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Register DID Connect routes on an Express-compatible app.
|
|
24
|
+
*
|
|
25
|
+
* Routes registered:
|
|
26
|
+
* - GET/POST `{prefix}/{action}/token` — generate session
|
|
27
|
+
* - GET `{prefix}/{action}/status` — check session status
|
|
28
|
+
* - GET `{prefix}/{action}/timeout` — expire session
|
|
29
|
+
* - GET `{prefix}/{action}/auth` — wallet fetches auth request
|
|
30
|
+
* - POST `{prefix}/{action}/auth` — wallet submits auth response
|
|
31
|
+
* - GET `{prefix}/{action}/auth/submit` — web wallet submit
|
|
32
|
+
*/
|
|
33
|
+
export declare function attachExpress({ app, prefix, action, handlers }: AttachExpressOptions): {
|
|
34
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
35
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
36
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
37
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
38
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
39
|
+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,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;CAC3F;AAED,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,cAAc,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,gBAAgB,CAAC;CAC5B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,oBAAoB;2BA3B5D,cAAc,OAAO,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC;yBAExD,cAAc,OAAO,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC;wBADvD,cAAc,OAAO,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC;yBAErD,cAAc,OAAO,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC;0BACrD,cAAc,OAAO,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC;EAuD7E"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register DID Connect routes on an Express-compatible app.
|
|
3
|
+
*
|
|
4
|
+
* Routes registered:
|
|
5
|
+
* - GET/POST `{prefix}/{action}/token` — generate session
|
|
6
|
+
* - GET `{prefix}/{action}/status` — check session status
|
|
7
|
+
* - GET `{prefix}/{action}/timeout` — expire session
|
|
8
|
+
* - GET `{prefix}/{action}/auth` — wallet fetches auth request
|
|
9
|
+
* - POST `{prefix}/{action}/auth` — wallet submits auth response
|
|
10
|
+
* - GET `{prefix}/{action}/auth/submit` — web wallet submit
|
|
11
|
+
*/
|
|
12
|
+
export function attachExpress({ app, prefix, action, handlers }) {
|
|
13
|
+
const pathname = `${prefix}/${action}/auth`;
|
|
14
|
+
const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson, } = handlers;
|
|
15
|
+
// 1. WEB|Wallet: generate new token
|
|
16
|
+
app.get(`${prefix}/${action}/token`, generateSession);
|
|
17
|
+
app.post(`${prefix}/${action}/token`, generateSession);
|
|
18
|
+
// 2. WEB: check for token status
|
|
19
|
+
app.get(`${prefix}/${action}/status`, ensureContext, checkSession);
|
|
20
|
+
// 3. WEB: expire old token
|
|
21
|
+
app.get(`${prefix}/${action}/timeout`, ensureContext, expireSession);
|
|
22
|
+
// 4. Wallet: fetch auth request
|
|
23
|
+
app.get(pathname, ensureContext, ensureSignedJson, onAuthRequest);
|
|
24
|
+
// 5. Wallet: submit auth response
|
|
25
|
+
app.post(pathname, ensureContext, ensureSignedJson, onAuthResponse);
|
|
26
|
+
// 6. Web wallet: submit auth response via GET
|
|
27
|
+
app.get(`${pathname}/submit`, ensureContext, ensureSignedJson, onAuthResponse);
|
|
28
|
+
return { generateSession, expireSession, checkSession, onAuthRequest, onAuthResponse };
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=express.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.js","sourceRoot":"","sources":["../../src/adapters/express.ts"],"names":[],"mappings":"AAyBA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAwB;IACnF,MAAM,QAAQ,GAAG,GAAG,MAAM,IAAI,MAAM,OAAO,CAAC;IAC5C,MAAM,EACJ,eAAe,EACf,YAAY,EACZ,aAAa,EACb,aAAa,EACb,cAAc,EACd,aAAa,EACb,gBAAgB,GACjB,GAAG,QAAQ,CAAC;IAEb,oCAAoC;IACpC,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,MAAM,QAAQ,EAAE,eAAe,CAAC,CAAC;IACtD,GAAG,CAAC,IAAI,CAAC,GAAG,MAAM,IAAI,MAAM,QAAQ,EAAE,eAAe,CAAC,CAAC;IAEvD,iCAAiC;IACjC,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,MAAM,SAAS,EAAE,aAAa,EAAE,YAAY,CAAC,CAAC;IAEnE,2BAA2B;IAC3B,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,MAAM,UAAU,EAAE,aAAa,EAAE,aAAa,CAAC,CAAC;IAErE,gCAAgC;IAChC,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;IAElE,kCAAkC;IAClC,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,aAAa,EAAE,gBAAgB,EAAE,cAAc,CAAC,CAAC;IAEpE,8CAA8C;IAC9C,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,SAAS,EAAE,aAAa,EAAE,gBAAgB,EAAE,cAAc,CAAC,CAAC;IAE/E,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,CAAC;AACzF,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ConnectRequest, ConnectResponse } from '../types.js';
|
|
2
|
+
import type { HandlerFunctions } from './express.js';
|
|
3
|
+
export interface HonoLikeApp {
|
|
4
|
+
get(path: string, handler: (c: any) => any): void;
|
|
5
|
+
post(path: string, handler: (c: any) => any): void;
|
|
6
|
+
use(path: string, handler: any): void;
|
|
7
|
+
}
|
|
8
|
+
export interface AttachHonoOptions {
|
|
9
|
+
app: HonoLikeApp;
|
|
10
|
+
prefix: string;
|
|
11
|
+
action: string;
|
|
12
|
+
handlers: HandlerFunctions;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a ConnectRequest from a Hono-like Context.
|
|
16
|
+
*
|
|
17
|
+
* @param c - Hono Context object
|
|
18
|
+
* @param bodyCache - Pre-parsed request body (since c.req.json() is async)
|
|
19
|
+
*/
|
|
20
|
+
export declare function createHonoRequest(c: any, bodyCache?: any): ConnectRequest;
|
|
21
|
+
/**
|
|
22
|
+
* Create a ConnectResponse that buffers the handler's response.
|
|
23
|
+
* The Hono route wrapper reads the buffer via _getResult() to return c.json().
|
|
24
|
+
*/
|
|
25
|
+
export declare function createHonoResponse(): ConnectResponse & {
|
|
26
|
+
_getResult(): {
|
|
27
|
+
statusCode: number;
|
|
28
|
+
body: any;
|
|
29
|
+
} | null;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Register DID Connect routes on a Hono-compatible app.
|
|
33
|
+
*
|
|
34
|
+
* Routes registered are the same as attachExpress:
|
|
35
|
+
* - GET/POST `{prefix}/{action}/token`
|
|
36
|
+
* - GET `{prefix}/{action}/status`
|
|
37
|
+
* - GET `{prefix}/{action}/timeout`
|
|
38
|
+
* - GET `{prefix}/{action}/auth`
|
|
39
|
+
* - POST `{prefix}/{action}/auth`
|
|
40
|
+
* - GET `{prefix}/{action}/auth/submit`
|
|
41
|
+
*/
|
|
42
|
+
export declare function attachHono({ app, prefix, action, handlers }: AttachHonoOptions): {
|
|
43
|
+
generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
44
|
+
expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
45
|
+
checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
46
|
+
onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
47
|
+
onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=hono.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hono.d.ts","sourceRoot":"","sources":["../../src/adapters/hono.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAgB,MAAM,aAAa,CAAC;AACjF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,GAAG,GAAG,IAAI,CAAC;IAClD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,GAAG,GAAG,IAAI,CAAC;IACnD,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI,CAAC;CACvC;AAED,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,WAAW,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,gBAAgB,CAAC;CAC5B;AAmCD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,SAAS,GAAE,GAAQ,GAAG,cAAc,CAyB7E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,GAAG;IAAE,UAAU,IAAI;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,GAAG,IAAI,CAAA;CAAE,CAuBjH;AAmDD;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,iBAAiB;;;;;;EA4C9E"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a raw Cookie header string into a key-value map.
|
|
3
|
+
*/
|
|
4
|
+
function parseCookieHeader(header) {
|
|
5
|
+
const cookies = {};
|
|
6
|
+
if (!header)
|
|
7
|
+
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)
|
|
24
|
+
return langs[0] || false;
|
|
25
|
+
const lower = acceptHeader.toLowerCase();
|
|
26
|
+
for (const lang of langs) {
|
|
27
|
+
const prefix = lang.toLowerCase().split('-')[0];
|
|
28
|
+
if (lower.includes(lang.toLowerCase()) || lower.includes(prefix)) {
|
|
29
|
+
return lang;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return langs[0] || false;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a ConnectRequest from a Hono-like Context.
|
|
36
|
+
*
|
|
37
|
+
* @param c - Hono Context object
|
|
38
|
+
* @param bodyCache - Pre-parsed request body (since c.req.json() is async)
|
|
39
|
+
*/
|
|
40
|
+
export function createHonoRequest(c, bodyCache = {}) {
|
|
41
|
+
const url = new URL(c.req.url);
|
|
42
|
+
const rawHeaders = {};
|
|
43
|
+
if (c.req.raw?.headers) {
|
|
44
|
+
c.req.raw.headers.forEach((value, key) => {
|
|
45
|
+
rawHeaders[key.toLowerCase()] = value;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
body: bodyCache,
|
|
50
|
+
query: Object.fromEntries(url.searchParams.entries()),
|
|
51
|
+
params: typeof c.req.param === 'function' ? c.req.param() : {},
|
|
52
|
+
headers: rawHeaders,
|
|
53
|
+
cookies: parseCookieHeader(rawHeaders.cookie || ''),
|
|
54
|
+
protocol: url.protocol.replace(':', ''),
|
|
55
|
+
originalUrl: url.pathname + url.search,
|
|
56
|
+
get(name) {
|
|
57
|
+
return rawHeaders[name.toLowerCase()];
|
|
58
|
+
},
|
|
59
|
+
acceptsLanguages(...langs) {
|
|
60
|
+
return negotiateLanguage(rawHeaders['accept-language'] || '', ...langs);
|
|
61
|
+
},
|
|
62
|
+
raw: c,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a ConnectResponse that buffers the handler's response.
|
|
67
|
+
* The Hono route wrapper reads the buffer via _getResult() to return c.json().
|
|
68
|
+
*/
|
|
69
|
+
export function createHonoResponse() {
|
|
70
|
+
let result = null;
|
|
71
|
+
const response = {
|
|
72
|
+
jsonp(data) {
|
|
73
|
+
result = { statusCode: 200, body: data };
|
|
74
|
+
},
|
|
75
|
+
json(data) {
|
|
76
|
+
result = { statusCode: 200, body: data };
|
|
77
|
+
},
|
|
78
|
+
status(code) {
|
|
79
|
+
return {
|
|
80
|
+
json(data) {
|
|
81
|
+
result = { statusCode: code, body: data };
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
_getResult() {
|
|
86
|
+
return result;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
return response;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Wrap a middleware chain + handler into a single Hono route handler.
|
|
93
|
+
* Runs middlewares sequentially with manual next() control,
|
|
94
|
+
* then runs the handler, then returns the buffered response.
|
|
95
|
+
*/
|
|
96
|
+
function wrapHandler(middlewares, handler) {
|
|
97
|
+
return async (c) => {
|
|
98
|
+
let body = {};
|
|
99
|
+
try {
|
|
100
|
+
const text = await c.req.text();
|
|
101
|
+
if (text) {
|
|
102
|
+
const contentType = c.req.header('content-type') || '';
|
|
103
|
+
if (contentType.includes('json')) {
|
|
104
|
+
body = JSON.parse(text);
|
|
105
|
+
}
|
|
106
|
+
else if (contentType.includes('urlencoded')) {
|
|
107
|
+
body = Object.fromEntries(new URLSearchParams(text).entries());
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
// Body parsing failed, use empty object
|
|
113
|
+
}
|
|
114
|
+
const req = createHonoRequest(c, body);
|
|
115
|
+
const res = createHonoResponse();
|
|
116
|
+
// Run middlewares
|
|
117
|
+
for (const mw of middlewares) {
|
|
118
|
+
let nextCalled = false;
|
|
119
|
+
await mw(req, res, () => {
|
|
120
|
+
nextCalled = true;
|
|
121
|
+
});
|
|
122
|
+
if (!nextCalled) {
|
|
123
|
+
// Middleware short-circuited (e.g., error response)
|
|
124
|
+
const r = res._getResult();
|
|
125
|
+
if (r)
|
|
126
|
+
return c.json(r.body, r.statusCode);
|
|
127
|
+
return c.json({ error: 'Unknown error' }, 500);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Run handler
|
|
131
|
+
await handler(req, res);
|
|
132
|
+
const r = res._getResult();
|
|
133
|
+
return c.json(r?.body ?? {}, r?.statusCode ?? 200);
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Register DID Connect routes on a Hono-compatible app.
|
|
138
|
+
*
|
|
139
|
+
* Routes registered are the same as attachExpress:
|
|
140
|
+
* - GET/POST `{prefix}/{action}/token`
|
|
141
|
+
* - GET `{prefix}/{action}/status`
|
|
142
|
+
* - GET `{prefix}/{action}/timeout`
|
|
143
|
+
* - GET `{prefix}/{action}/auth`
|
|
144
|
+
* - POST `{prefix}/{action}/auth`
|
|
145
|
+
* - GET `{prefix}/{action}/auth/submit`
|
|
146
|
+
*/
|
|
147
|
+
export function attachHono({ app, prefix, action, handlers }) {
|
|
148
|
+
const pathname = `${prefix}/${action}/auth`;
|
|
149
|
+
const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson, } = handlers;
|
|
150
|
+
// CORS — use Hono's built-in cors or manual headers
|
|
151
|
+
// For simplicity, add CORS headers via a middleware
|
|
152
|
+
app.use(`${prefix}/${action}/*`, async (c, next) => {
|
|
153
|
+
c.header('Access-Control-Allow-Origin', '*');
|
|
154
|
+
c.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
155
|
+
c.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
156
|
+
if (c.req.method === 'OPTIONS') {
|
|
157
|
+
return c.text('', 204);
|
|
158
|
+
}
|
|
159
|
+
await next();
|
|
160
|
+
});
|
|
161
|
+
// 1. WEB|Wallet: generate new token
|
|
162
|
+
app.get(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
163
|
+
app.post(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
164
|
+
// 2. WEB: check for token status
|
|
165
|
+
app.get(`${prefix}/${action}/status`, wrapHandler([ensureContext], checkSession));
|
|
166
|
+
// 3. WEB: expire old token
|
|
167
|
+
app.get(`${prefix}/${action}/timeout`, wrapHandler([ensureContext], expireSession));
|
|
168
|
+
// 4. Wallet: fetch auth request
|
|
169
|
+
app.get(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthRequest));
|
|
170
|
+
// 5. Wallet: submit auth response
|
|
171
|
+
app.post(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
172
|
+
// 6. Web wallet: submit via GET
|
|
173
|
+
app.get(`${pathname}/submit`, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
174
|
+
return { generateSession, expireSession, checkSession, onAuthRequest, onAuthResponse };
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=hono.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hono.js","sourceRoot":"","sources":["../../src/adapters/hono.ts"],"names":[],"mappings":"AAgBA;;GAEG;AACH,SAAS,iBAAiB,CAAC,MAAc;IACvC,MAAM,OAAO,GAA2B,EAAE,CAAC;IAC3C,IAAI,CAAC,MAAM;QAAE,OAAO,OAAO,CAAC;IAC5B,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,GAAG,GAAG,CAAC,EAA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E,gBAAgB,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC;IAE9F,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,CAAC;AACzF,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as Jwt from '@arcblock/jwt';
|
|
2
|
+
export declare const DEFAULT_CHAIN_INFO: {
|
|
3
|
+
id: string;
|
|
4
|
+
host: string;
|
|
5
|
+
type: string;
|
|
6
|
+
};
|
|
7
|
+
declare class BaseAuthenticator {
|
|
8
|
+
_validateWallet(wallet: any, canSign?: boolean): any;
|
|
9
|
+
/**
|
|
10
|
+
* Verify a DID auth response sent from DID Wallet
|
|
11
|
+
*
|
|
12
|
+
* @method
|
|
13
|
+
* @param {object} data
|
|
14
|
+
* @param {string} [locale=en]
|
|
15
|
+
* @param {boolean} [enforceTimestamp=true]
|
|
16
|
+
* @returns Promise<boolean>
|
|
17
|
+
*/
|
|
18
|
+
_verify(data: any, fieldPk: string, fieldInfo: string, locale?: string, enforceTimestamp?: boolean): Promise<Jwt.JwtBody>;
|
|
19
|
+
}
|
|
20
|
+
export default BaseAuthenticator;
|
|
21
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/authenticator/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,eAAe,CAAC;AAMrC,eAAO,MAAM,kBAAkB;;;;CAAiD,CAAC;AAEjF,cAAM,iBAAiB;IACrB,eAAe,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,UAAO;IAiD3C;;;;;;;;OAQG;IACG,OAAO,CACX,IAAI,EAAE,GAAG,EACT,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,MAAM,SAAO,EACb,gBAAgB,UAAO,GACtB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;CAmDxB;AAED,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as Jwt from '@arcblock/jwt';
|
|
2
|
+
import { fromJSON, isValid } from '@ocap/wallet';
|
|
3
|
+
import Debug from 'debug';
|
|
4
|
+
const debug = Debug('@arcblock/did-connect-js:authenticator:base');
|
|
5
|
+
export const DEFAULT_CHAIN_INFO = { id: 'none', host: 'none', type: 'arcblock' };
|
|
6
|
+
class BaseAuthenticator {
|
|
7
|
+
_validateWallet(wallet, canSign = true) {
|
|
8
|
+
if (!wallet) {
|
|
9
|
+
throw new Error('WalletAuthenticator cannot work without wallet');
|
|
10
|
+
}
|
|
11
|
+
if (typeof wallet === 'function') {
|
|
12
|
+
return wallet;
|
|
13
|
+
}
|
|
14
|
+
if (isValid(wallet, canSign)) {
|
|
15
|
+
// Full wallet object with methods - add pk/sk aliases for backward compatibility
|
|
16
|
+
if (!wallet.pk && wallet.publicKey) {
|
|
17
|
+
wallet.pk = wallet.publicKey;
|
|
18
|
+
}
|
|
19
|
+
if (!wallet.sk && wallet.secretKey) {
|
|
20
|
+
wallet.sk = wallet.secretKey;
|
|
21
|
+
}
|
|
22
|
+
return wallet;
|
|
23
|
+
}
|
|
24
|
+
// Validate required fields for JSON wallet object
|
|
25
|
+
if (canSign && !wallet.sk) {
|
|
26
|
+
throw new Error('WalletAuthenticator cannot work without wallet.sk');
|
|
27
|
+
}
|
|
28
|
+
if (!wallet.pk) {
|
|
29
|
+
throw new Error('WalletAuthenticator cannot work without wallet.pk');
|
|
30
|
+
}
|
|
31
|
+
if (!wallet.address) {
|
|
32
|
+
throw new Error('WalletAuthenticator cannot work without wallet.address');
|
|
33
|
+
}
|
|
34
|
+
// If it's a plain JSON object with required fields, convert it to a full wallet object
|
|
35
|
+
// This ensures backward compatibility with code that uses toJSON()
|
|
36
|
+
try {
|
|
37
|
+
const fullWallet = fromJSON(wallet);
|
|
38
|
+
// Add pk/sk aliases for backward compatibility
|
|
39
|
+
if (!fullWallet.pk && fullWallet.publicKey) {
|
|
40
|
+
fullWallet.pk = fullWallet.publicKey;
|
|
41
|
+
}
|
|
42
|
+
if (!fullWallet.sk && fullWallet.secretKey) {
|
|
43
|
+
fullWallet.sk = fullWallet.secretKey;
|
|
44
|
+
}
|
|
45
|
+
return fullWallet;
|
|
46
|
+
}
|
|
47
|
+
catch (_err) {
|
|
48
|
+
// If fromJSON fails, return the original object for backward compatibility
|
|
49
|
+
return wallet;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Verify a DID auth response sent from DID Wallet
|
|
54
|
+
*
|
|
55
|
+
* @method
|
|
56
|
+
* @param {object} data
|
|
57
|
+
* @param {string} [locale=en]
|
|
58
|
+
* @param {boolean} [enforceTimestamp=true]
|
|
59
|
+
* @returns Promise<boolean>
|
|
60
|
+
*/
|
|
61
|
+
async _verify(data, fieldPk, fieldInfo, locale = 'en', enforceTimestamp = true) {
|
|
62
|
+
debug('verify', data, locale);
|
|
63
|
+
const errors = {
|
|
64
|
+
pkMissing: {
|
|
65
|
+
en: `${fieldPk} is required to complete auth`,
|
|
66
|
+
zh: `${fieldPk} 参数缺失`,
|
|
67
|
+
},
|
|
68
|
+
tokenMissing: {
|
|
69
|
+
en: `${fieldInfo} is required to complete auth`,
|
|
70
|
+
zh: 'JWT Token 参数缺失',
|
|
71
|
+
},
|
|
72
|
+
pkFormat: {
|
|
73
|
+
en: `${fieldPk} should be either base58 or base16 format`,
|
|
74
|
+
zh: `${fieldPk} 无法解析`,
|
|
75
|
+
},
|
|
76
|
+
tokenInvalid: {
|
|
77
|
+
en: 'Invalid JWT token',
|
|
78
|
+
zh: '签名无效',
|
|
79
|
+
},
|
|
80
|
+
timeInvalid: {
|
|
81
|
+
en: 'JWT token expired, make sure your device time in sync with network',
|
|
82
|
+
zh: '签名中的时间戳无效,请确保设备和网络时间同步',
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
const pk = data[fieldPk];
|
|
86
|
+
const info = data[fieldInfo];
|
|
87
|
+
if (!pk) {
|
|
88
|
+
throw new Error(errors.pkMissing[locale]);
|
|
89
|
+
}
|
|
90
|
+
if (!info) {
|
|
91
|
+
throw new Error(errors.tokenMissing[locale]);
|
|
92
|
+
}
|
|
93
|
+
if (!pk) {
|
|
94
|
+
throw new Error(errors.pkFormat[locale]);
|
|
95
|
+
}
|
|
96
|
+
// NOTE: since the token can be invalid because of wallet-app clock not in sync
|
|
97
|
+
// We should tell the user that if it's caused by clock
|
|
98
|
+
if (!(await Jwt.verify(info, pk))) {
|
|
99
|
+
const isValidSig = await Jwt.verify(info, pk, { tolerance: 0, enforceTimestamp: false });
|
|
100
|
+
if (enforceTimestamp) {
|
|
101
|
+
const error = isValidSig ? errors.timeInvalid[locale] : errors.tokenInvalid[locale];
|
|
102
|
+
throw new Error(error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return Jwt.decode(info);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export default BaseAuthenticator;
|
|
109
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/authenticator/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,KAAK,GAAG,KAAK,CAAC,6CAA6C,CAAC,CAAC;AAEnE,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;AAEjF,MAAM,iBAAiB;IACrB,eAAe,CAAC,MAAW,EAAE,OAAO,GAAG,IAAI;QACzC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;Q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