@arcblock/did-connect-js 1.30.0 → 4.0.0-beta.10
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 +252 -0
- package/dist/authenticator/wallet.d.ts.map +1 -0
- package/dist/authenticator/wallet.js +590 -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 +805 -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 +11 -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 +11 -0
- package/dist/schema/claims.d.ts.map +1 -0
- package/dist/schema/claims.js +226 -0
- package/dist/schema/claims.js.map +1 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +89 -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/dist/utils/helpers.d.ts +19 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +77 -0
- package/dist/utils/helpers.js.map +1 -0
- package/package.json +51 -107
- 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/did/did-connect/package.mjs +0 -118
- 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/node_modules/valibot/dist/index.mjs +0 -736
- package/esm/protocol.d.mts +0 -15
- package/esm/protocol.mjs +0 -40
- package/esm/schema/claims.d.mts +0 -16
- package/esm/schema/claims.mjs +0 -216
- package/esm/schema/index.d.mts +0 -29
- package/esm/schema/index.mjs +0 -75
- 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 -565
- package/lib/authenticator/wallet.d.cts +0 -270
- package/lib/did/did-connect/package.cjs +0 -121
- 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/node_modules/valibot/dist/index.cjs +0 -756
- package/lib/protocol.cjs +0 -46
- package/lib/protocol.d.cts +0 -15
- package/lib/schema/claims.cjs +0 -218
- package/lib/schema/claims.d.cts +0 -16
- package/lib/schema/index.cjs +0 -79
- package/lib/schema/index.d.cts +0 -29
- 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,48 +0,0 @@
|
|
|
1
|
-
import { ConnectRequest, ConnectResponse, NextFunction } from "../types.cjs";
|
|
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 };
|
package/lib/adapters/hono.cjs
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/adapters/hono.ts
|
|
3
|
-
/**
|
|
4
|
-
* Parse a raw Cookie header string into a key-value map.
|
|
5
|
-
*/
|
|
6
|
-
function parseCookieHeader(header) {
|
|
7
|
-
const cookies = {};
|
|
8
|
-
if (!header) return cookies;
|
|
9
|
-
for (const pair of header.split(";")) {
|
|
10
|
-
const idx = pair.indexOf("=");
|
|
11
|
-
if (idx > 0) {
|
|
12
|
-
const key = pair.slice(0, idx).trim();
|
|
13
|
-
const val = pair.slice(idx + 1).trim();
|
|
14
|
-
cookies[key] = decodeURIComponent(val);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return cookies;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Simple Accept-Language negotiation matching Express's req.acceptsLanguages().
|
|
21
|
-
* Only needs to handle the two languages used by DID Connect: 'en-US' and 'zh-CN'.
|
|
22
|
-
*/
|
|
23
|
-
function negotiateLanguage(acceptHeader, ...langs) {
|
|
24
|
-
if (!acceptHeader || langs.length === 0) 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)) return lang;
|
|
29
|
-
}
|
|
30
|
-
return langs[0] || false;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Create a ConnectRequest from a Hono-like Context.
|
|
34
|
-
*
|
|
35
|
-
* @param c - Hono Context object
|
|
36
|
-
* @param bodyCache - Pre-parsed request body (since c.req.json() is async)
|
|
37
|
-
*/
|
|
38
|
-
function createHonoRequest(c, bodyCache = {}) {
|
|
39
|
-
const url = new URL(c.req.url);
|
|
40
|
-
const rawHeaders = {};
|
|
41
|
-
if (c.req.raw?.headers) c.req.raw.headers.forEach((value, key) => {
|
|
42
|
-
rawHeaders[key.toLowerCase()] = value;
|
|
43
|
-
});
|
|
44
|
-
return {
|
|
45
|
-
body: bodyCache,
|
|
46
|
-
query: Object.fromEntries(url.searchParams.entries()),
|
|
47
|
-
params: typeof c.req.param === "function" ? c.req.param() : {},
|
|
48
|
-
headers: rawHeaders,
|
|
49
|
-
cookies: parseCookieHeader(rawHeaders.cookie || ""),
|
|
50
|
-
protocol: url.protocol.replace(":", ""),
|
|
51
|
-
originalUrl: url.pathname + url.search,
|
|
52
|
-
get(name) {
|
|
53
|
-
return rawHeaders[name.toLowerCase()];
|
|
54
|
-
},
|
|
55
|
-
acceptsLanguages(...langs) {
|
|
56
|
-
return negotiateLanguage(rawHeaders["accept-language"] || "", ...langs);
|
|
57
|
-
},
|
|
58
|
-
raw: c
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Create a ConnectResponse that buffers the handler's response.
|
|
63
|
-
* The Hono route wrapper reads the buffer via _getResult() to return c.json().
|
|
64
|
-
*/
|
|
65
|
-
function createHonoResponse() {
|
|
66
|
-
let result = null;
|
|
67
|
-
return {
|
|
68
|
-
jsonp(data) {
|
|
69
|
-
result = {
|
|
70
|
-
statusCode: 200,
|
|
71
|
-
body: data
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
json(data) {
|
|
75
|
-
result = {
|
|
76
|
-
statusCode: 200,
|
|
77
|
-
body: data
|
|
78
|
-
};
|
|
79
|
-
},
|
|
80
|
-
status(code) {
|
|
81
|
-
return { json(data) {
|
|
82
|
-
result = {
|
|
83
|
-
statusCode: code,
|
|
84
|
-
body: data
|
|
85
|
-
};
|
|
86
|
-
} };
|
|
87
|
-
},
|
|
88
|
-
_getResult() {
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Wrap a middleware chain + handler into a single Hono route handler.
|
|
95
|
-
* Runs middlewares sequentially with manual next() control,
|
|
96
|
-
* then runs the handler, then returns the buffered response.
|
|
97
|
-
*/
|
|
98
|
-
function wrapHandler(middlewares, handler) {
|
|
99
|
-
return async (c) => {
|
|
100
|
-
let body = {};
|
|
101
|
-
try {
|
|
102
|
-
const text = await c.req.text();
|
|
103
|
-
if (text) {
|
|
104
|
-
const contentType = c.req.header("content-type") || "";
|
|
105
|
-
if (contentType.includes("json")) body = JSON.parse(text);
|
|
106
|
-
else if (contentType.includes("urlencoded")) body = Object.fromEntries(new URLSearchParams(text).entries());
|
|
107
|
-
}
|
|
108
|
-
} catch {}
|
|
109
|
-
const req = createHonoRequest(c, body);
|
|
110
|
-
const res = createHonoResponse();
|
|
111
|
-
for (const mw of middlewares) {
|
|
112
|
-
let nextCalled = false;
|
|
113
|
-
await mw(req, res, () => {
|
|
114
|
-
nextCalled = true;
|
|
115
|
-
});
|
|
116
|
-
if (!nextCalled) {
|
|
117
|
-
const r$1 = res._getResult();
|
|
118
|
-
if (r$1) return c.json(r$1.body, r$1.statusCode);
|
|
119
|
-
return c.json({ error: "Unknown error" }, 500);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
await handler(req, res);
|
|
123
|
-
const r = res._getResult();
|
|
124
|
-
return c.json(r?.body ?? {}, r?.statusCode ?? 200);
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Register DID Connect routes on a Hono-compatible app.
|
|
129
|
-
*
|
|
130
|
-
* Routes registered are the same as attachExpress:
|
|
131
|
-
* - GET/POST `{prefix}/{action}/token`
|
|
132
|
-
* - GET `{prefix}/{action}/status`
|
|
133
|
-
* - GET `{prefix}/{action}/timeout`
|
|
134
|
-
* - GET `{prefix}/{action}/auth`
|
|
135
|
-
* - POST `{prefix}/{action}/auth`
|
|
136
|
-
* - GET `{prefix}/{action}/auth/submit`
|
|
137
|
-
*/
|
|
138
|
-
function attachHono({ app, prefix, action, handlers }) {
|
|
139
|
-
const pathname = `${prefix}/${action}/auth`;
|
|
140
|
-
const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
|
|
141
|
-
app.use(`${prefix}/${action}/*`, async (c, next) => {
|
|
142
|
-
c.header("Access-Control-Allow-Origin", "*");
|
|
143
|
-
c.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
144
|
-
c.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
145
|
-
if (c.req.method === "OPTIONS") return c.text("", 204);
|
|
146
|
-
await next();
|
|
147
|
-
});
|
|
148
|
-
app.get(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
149
|
-
app.post(`${prefix}/${action}/token`, wrapHandler([], generateSession));
|
|
150
|
-
app.get(`${prefix}/${action}/status`, wrapHandler([ensureContext], checkSession));
|
|
151
|
-
app.get(`${prefix}/${action}/timeout`, wrapHandler([ensureContext], expireSession));
|
|
152
|
-
app.get(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthRequest));
|
|
153
|
-
app.post(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
154
|
-
app.get(`${pathname}/submit`, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
|
|
155
|
-
return {
|
|
156
|
-
generateSession,
|
|
157
|
-
expireSession,
|
|
158
|
-
checkSession,
|
|
159
|
-
onAuthRequest,
|
|
160
|
-
onAuthResponse
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
//#endregion
|
|
165
|
-
exports.attachHono = attachHono;
|
|
166
|
-
exports.createHonoRequest = createHonoRequest;
|
|
167
|
-
exports.createHonoResponse = createHonoResponse;
|
package/lib/adapters/hono.d.cts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { ConnectRequest, ConnectResponse } from "../types.cjs";
|
|
2
|
-
import { HandlerFunctions } from "./express.cjs";
|
|
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 };
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
-
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
-
const require_package$1 = require('../did/did-connect/package.cjs');
|
|
4
|
-
let _ocap_wallet = require("@ocap/wallet");
|
|
5
|
-
let _arcblock_jwt = require("@arcblock/jwt");
|
|
6
|
-
_arcblock_jwt = require_rolldown_runtime.__toESM(_arcblock_jwt);
|
|
7
|
-
|
|
8
|
-
//#region src/authenticator/base.ts
|
|
9
|
-
const debug = require("debug")(`${require_package$1.default.name}:authenticator:base`);
|
|
10
|
-
const DEFAULT_CHAIN_INFO = {
|
|
11
|
-
id: "none",
|
|
12
|
-
host: "none",
|
|
13
|
-
type: "arcblock"
|
|
14
|
-
};
|
|
15
|
-
var BaseAuthenticator = class {
|
|
16
|
-
_validateWallet(wallet, canSign = true) {
|
|
17
|
-
if (!wallet) throw new Error("WalletAuthenticator cannot work without wallet");
|
|
18
|
-
if (typeof wallet === "function") return wallet;
|
|
19
|
-
if ((0, _ocap_wallet.isValid)(wallet, canSign)) {
|
|
20
|
-
if (!wallet.pk && wallet.publicKey) wallet.pk = wallet.publicKey;
|
|
21
|
-
if (!wallet.sk && wallet.secretKey) wallet.sk = wallet.secretKey;
|
|
22
|
-
return wallet;
|
|
23
|
-
}
|
|
24
|
-
if (canSign && !wallet.sk) throw new Error("WalletAuthenticator cannot work without wallet.sk");
|
|
25
|
-
if (!wallet.pk) throw new Error("WalletAuthenticator cannot work without wallet.pk");
|
|
26
|
-
if (!wallet.address) throw new Error("WalletAuthenticator cannot work without wallet.address");
|
|
27
|
-
try {
|
|
28
|
-
const fullWallet = (0, _ocap_wallet.fromJSON)(wallet);
|
|
29
|
-
if (!fullWallet.pk && fullWallet.publicKey) fullWallet.pk = fullWallet.publicKey;
|
|
30
|
-
if (!fullWallet.sk && fullWallet.secretKey) fullWallet.sk = fullWallet.secretKey;
|
|
31
|
-
return fullWallet;
|
|
32
|
-
} catch (_err) {
|
|
33
|
-
return wallet;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Verify a DID auth response sent from DID Wallet
|
|
38
|
-
*
|
|
39
|
-
* @method
|
|
40
|
-
* @param {object} data
|
|
41
|
-
* @param {string} [locale=en]
|
|
42
|
-
* @param {boolean} [enforceTimestamp=true]
|
|
43
|
-
* @returns Promise<boolean>
|
|
44
|
-
*/
|
|
45
|
-
async _verify(data, fieldPk, fieldInfo, locale = "en", enforceTimestamp = true) {
|
|
46
|
-
debug("verify", data, locale);
|
|
47
|
-
const errors = {
|
|
48
|
-
pkMissing: {
|
|
49
|
-
en: `${fieldPk} is required to complete auth`,
|
|
50
|
-
zh: `${fieldPk} 参数缺失`
|
|
51
|
-
},
|
|
52
|
-
tokenMissing: {
|
|
53
|
-
en: `${fieldInfo} is required to complete auth`,
|
|
54
|
-
zh: "JWT Token 参数缺失"
|
|
55
|
-
},
|
|
56
|
-
pkFormat: {
|
|
57
|
-
en: `${fieldPk} should be either base58 or base16 format`,
|
|
58
|
-
zh: `${fieldPk} 无法解析`
|
|
59
|
-
},
|
|
60
|
-
tokenInvalid: {
|
|
61
|
-
en: "Invalid JWT token",
|
|
62
|
-
zh: "签名无效"
|
|
63
|
-
},
|
|
64
|
-
timeInvalid: {
|
|
65
|
-
en: "JWT token expired, make sure your device time in sync with network",
|
|
66
|
-
zh: "签名中的时间戳无效,请确保设备和网络时间同步"
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
const pk = data[fieldPk];
|
|
70
|
-
const info = data[fieldInfo];
|
|
71
|
-
if (!pk) throw new Error(errors.pkMissing[locale]);
|
|
72
|
-
if (!info) throw new Error(errors.tokenMissing[locale]);
|
|
73
|
-
if (!pk) throw new Error(errors.pkFormat[locale]);
|
|
74
|
-
if (!await _arcblock_jwt.verify(info, pk)) {
|
|
75
|
-
const isValidSig = await _arcblock_jwt.verify(info, pk, {
|
|
76
|
-
tolerance: 0,
|
|
77
|
-
enforceTimestamp: false
|
|
78
|
-
});
|
|
79
|
-
if (enforceTimestamp) {
|
|
80
|
-
const error = isValidSig ? errors.timeInvalid[locale] : errors.tokenInvalid[locale];
|
|
81
|
-
throw new Error(error);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return _arcblock_jwt.decode(info);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
var base_default = BaseAuthenticator;
|
|
88
|
-
|
|
89
|
-
//#endregion
|
|
90
|
-
exports.DEFAULT_CHAIN_INFO = DEFAULT_CHAIN_INFO;
|
|
91
|
-
exports.default = base_default;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as Jwt from "@arcblock/jwt";
|
|
2
|
-
|
|
3
|
-
//#region src/authenticator/base.d.ts
|
|
4
|
-
declare const DEFAULT_CHAIN_INFO: {
|
|
5
|
-
id: string;
|
|
6
|
-
host: string;
|
|
7
|
-
type: string;
|
|
8
|
-
};
|
|
9
|
-
declare class BaseAuthenticator {
|
|
10
|
-
_validateWallet(wallet: any, canSign?: boolean): any;
|
|
11
|
-
/**
|
|
12
|
-
* Verify a DID auth response sent from DID Wallet
|
|
13
|
-
*
|
|
14
|
-
* @method
|
|
15
|
-
* @param {object} data
|
|
16
|
-
* @param {string} [locale=en]
|
|
17
|
-
* @param {boolean} [enforceTimestamp=true]
|
|
18
|
-
* @returns Promise<boolean>
|
|
19
|
-
*/
|
|
20
|
-
_verify(data: any, fieldPk: string, fieldInfo: string, locale?: string, enforceTimestamp?: boolean): Promise<Jwt.JwtBody>;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
export { DEFAULT_CHAIN_INFO, BaseAuthenticator as default };
|