@blocklet/aigne-hub 0.2.10 → 0.2.12
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/cjs/api/error.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CreditError = exports.SubscriptionError = exports.CreditErrorType = exports.SubscriptionErrorType = void 0;
|
|
3
|
+
exports.StatusCodeError = exports.CreditError = exports.SubscriptionError = exports.CreditErrorType = exports.SubscriptionErrorType = void 0;
|
|
4
4
|
var SubscriptionErrorType;
|
|
5
5
|
(function (SubscriptionErrorType) {
|
|
6
6
|
SubscriptionErrorType["UNSUBSCRIBED"] = "UNSUBSCRIBED";
|
|
@@ -40,3 +40,10 @@ class CreditError extends Error {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.CreditError = CreditError;
|
|
43
|
+
class StatusCodeError extends Error {
|
|
44
|
+
constructor(statusCode, message) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.statusCode = statusCode;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.StatusCodeError = StatusCodeError;
|
|
@@ -15,6 +15,7 @@ const middlewares_1 = require("@blocklet/sdk/lib/middlewares");
|
|
|
15
15
|
const wallet_1 = __importDefault(require("@blocklet/sdk/lib/wallet"));
|
|
16
16
|
const mcrypto_1 = require("@ocap/mcrypto");
|
|
17
17
|
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
|
|
18
|
+
const error_1 = require("../error");
|
|
18
19
|
const TOKEN_EXPIRES_IN_SECONDS = 60 * 10;
|
|
19
20
|
exports.wallet = (0, wallet_1.default)();
|
|
20
21
|
const ADMIN_ROLES = ['owner', 'admin'];
|
|
@@ -28,8 +29,9 @@ function appIdFromPublicKey(publicKey) {
|
|
|
28
29
|
return (0, did_1.fromPublicKey)(publicKey, (0, did_1.DidType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 }));
|
|
29
30
|
}
|
|
30
31
|
function verifyRemoteComponentCall({ appId, timestamp, data, sig, pk, userDid, expiresIn = TOKEN_EXPIRES_IN_SECONDS, }) {
|
|
31
|
-
if (Math.abs(Date.now() / 1000 - timestamp) > expiresIn)
|
|
32
|
-
throw new
|
|
32
|
+
if (Math.abs(Date.now() / 1000 - timestamp) > expiresIn) {
|
|
33
|
+
throw new error_1.StatusCodeError(401, 'signature expired');
|
|
34
|
+
}
|
|
33
35
|
return signer.verify(hashData({ appId, timestamp, data, userDid }), sig, pk);
|
|
34
36
|
}
|
|
35
37
|
function signRemoteComponentCall({ data, userDid }) {
|
|
@@ -57,13 +59,14 @@ function ensureRemoteComponentCall(getPublicKey, fallback) {
|
|
|
57
59
|
const sig = req.get('x-component-sig');
|
|
58
60
|
const appId = req.get('x-app-id');
|
|
59
61
|
const timestamp = req.get('x-timestamp');
|
|
60
|
-
const userDid = req.get('x-app-user-did');
|
|
62
|
+
const userDid = req.get('x-app-user-did');
|
|
61
63
|
if (!sig || !appId || !timestamp) {
|
|
62
|
-
throw new
|
|
64
|
+
throw new error_1.StatusCodeError(400, 'Missing required headers x-component-sig/x-app-id/x-timestamp');
|
|
63
65
|
}
|
|
64
66
|
const pk = await getPublicKey(appId);
|
|
65
|
-
if (appIdFromPublicKey(pk) !== appId)
|
|
66
|
-
throw new
|
|
67
|
+
if (appIdFromPublicKey(pk) !== appId) {
|
|
68
|
+
throw new error_1.StatusCodeError(401, 'appId and public key not match');
|
|
69
|
+
}
|
|
67
70
|
if (!verifyRemoteComponentCall({
|
|
68
71
|
appId,
|
|
69
72
|
sig,
|
|
@@ -72,7 +75,7 @@ function ensureRemoteComponentCall(getPublicKey, fallback) {
|
|
|
72
75
|
pk,
|
|
73
76
|
userDid,
|
|
74
77
|
})) {
|
|
75
|
-
throw new
|
|
78
|
+
throw new error_1.StatusCodeError(401, 'Validate signature error');
|
|
76
79
|
}
|
|
77
80
|
req.appClient = {
|
|
78
81
|
appId,
|
package/lib/esm/api/error.js
CHANGED
|
@@ -4,6 +4,7 @@ import { auth } from '@blocklet/sdk/lib/middlewares';
|
|
|
4
4
|
import getWallet from '@blocklet/sdk/lib/wallet';
|
|
5
5
|
import { getHasher, getSigner, types } from '@ocap/mcrypto';
|
|
6
6
|
import stringify from 'json-stable-stringify';
|
|
7
|
+
import { StatusCodeError } from '../error';
|
|
7
8
|
const TOKEN_EXPIRES_IN_SECONDS = 60 * 10;
|
|
8
9
|
export const wallet = getWallet();
|
|
9
10
|
const ADMIN_ROLES = ['owner', 'admin'];
|
|
@@ -17,8 +18,9 @@ export function appIdFromPublicKey(publicKey) {
|
|
|
17
18
|
return fromPublicKey(publicKey, DidType({ role: types.RoleType.ROLE_APPLICATION, pk: types.KeyType.ED25519, hash: types.HashType.SHA3 }));
|
|
18
19
|
}
|
|
19
20
|
export function verifyRemoteComponentCall({ appId, timestamp, data, sig, pk, userDid, expiresIn = TOKEN_EXPIRES_IN_SECONDS, }) {
|
|
20
|
-
if (Math.abs(Date.now() / 1000 - timestamp) > expiresIn)
|
|
21
|
-
throw new
|
|
21
|
+
if (Math.abs(Date.now() / 1000 - timestamp) > expiresIn) {
|
|
22
|
+
throw new StatusCodeError(401, 'signature expired');
|
|
23
|
+
}
|
|
22
24
|
return signer.verify(hashData({ appId, timestamp, data, userDid }), sig, pk);
|
|
23
25
|
}
|
|
24
26
|
export function signRemoteComponentCall({ data, userDid }) {
|
|
@@ -46,13 +48,14 @@ export function ensureRemoteComponentCall(getPublicKey, fallback) {
|
|
|
46
48
|
const sig = req.get('x-component-sig');
|
|
47
49
|
const appId = req.get('x-app-id');
|
|
48
50
|
const timestamp = req.get('x-timestamp');
|
|
49
|
-
const userDid = req.get('x-app-user-did');
|
|
51
|
+
const userDid = req.get('x-app-user-did');
|
|
50
52
|
if (!sig || !appId || !timestamp) {
|
|
51
|
-
throw new
|
|
53
|
+
throw new StatusCodeError(400, 'Missing required headers x-component-sig/x-app-id/x-timestamp');
|
|
52
54
|
}
|
|
53
55
|
const pk = await getPublicKey(appId);
|
|
54
|
-
if (appIdFromPublicKey(pk) !== appId)
|
|
55
|
-
throw new
|
|
56
|
+
if (appIdFromPublicKey(pk) !== appId) {
|
|
57
|
+
throw new StatusCodeError(401, 'appId and public key not match');
|
|
58
|
+
}
|
|
56
59
|
if (!verifyRemoteComponentCall({
|
|
57
60
|
appId,
|
|
58
61
|
sig,
|
|
@@ -61,7 +64,7 @@ export function ensureRemoteComponentCall(getPublicKey, fallback) {
|
|
|
61
64
|
pk,
|
|
62
65
|
userDid,
|
|
63
66
|
})) {
|
|
64
|
-
throw new
|
|
67
|
+
throw new StatusCodeError(401, 'Validate signature error');
|
|
65
68
|
}
|
|
66
69
|
req.appClient = {
|
|
67
70
|
appId,
|
package/lib/types/api/error.d.ts
CHANGED
|
@@ -16,3 +16,7 @@ export declare class CreditError extends Error {
|
|
|
16
16
|
type: CreditErrorType;
|
|
17
17
|
constructor(type: CreditErrorType, link?: string);
|
|
18
18
|
}
|
|
19
|
+
export declare class StatusCodeError extends Error {
|
|
20
|
+
statusCode: number;
|
|
21
|
+
constructor(statusCode: number, message: string);
|
|
22
|
+
}
|