@blocklet/sdk 1.8.32 → 1.8.34
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/component/index.d.ts +35 -0
- package/lib/component/index.js +83 -80
- package/lib/config.d.ts +33 -0
- package/lib/config.js +10 -7
- package/lib/connect/authenticator.d.ts +5 -0
- package/lib/connect/authenticator.js +14 -15
- package/lib/connect/handler.d.ts +2 -0
- package/lib/connect/handler.js +91 -65
- package/lib/connect/shared.d.ts +13 -0
- package/lib/connect/shared.js +26 -31
- package/lib/database/index.d.ts +13 -0
- package/lib/database/index.js +52 -39
- package/lib/env.d.ts +12 -0
- package/lib/env.js +12 -9
- package/lib/error-handler.d.ts +0 -0
- package/lib/error-handler.js +5 -5
- package/lib/index.d.ts +51 -0
- package/lib/index.js +51 -40
- package/lib/middlewares/auth.d.ts +3 -0
- package/lib/middlewares/auth.js +52 -49
- package/lib/middlewares/component.d.ts +7 -0
- package/lib/middlewares/component.js +28 -24
- package/lib/middlewares/index.d.ts +20 -0
- package/lib/middlewares/index.js +16 -8
- package/lib/middlewares/user.d.ts +10 -0
- package/lib/middlewares/user.js +20 -10
- package/lib/security/index.d.ts +9 -0
- package/lib/security/index.js +22 -21
- package/lib/service/auth.d.ts +9 -0
- package/lib/service/auth.js +131 -165
- package/lib/service/notification.d.ts +44 -0
- package/lib/service/notification.js +149 -129
- package/lib/types/notification.d.ts +71 -0
- package/lib/types/notification.js +3 -0
- package/lib/util/check-blocklet-env.d.ts +2 -0
- package/lib/util/check-blocklet-env.js +20 -19
- package/lib/util/constants.d.ts +5 -0
- package/lib/util/constants.js +6 -2
- package/lib/util/send-notification.d.ts +25 -0
- package/lib/util/send-notification.js +118 -115
- package/lib/validators/index.d.ts +12 -0
- package/lib/validators/index.js +13 -7
- package/lib/validators/notification.d.ts +59 -0
- package/lib/validators/notification.js +127 -97
- package/lib/version.d.ts +6 -0
- package/lib/version.js +6 -0
- package/lib/wallet-authenticator.d.ts +5 -0
- package/lib/wallet-authenticator.js +13 -16
- package/lib/wallet-handler.d.ts +17 -0
- package/lib/wallet-handler.js +121 -88
- package/lib/wallet.d.ts +3 -0
- package/lib/wallet.js +9 -10
- package/package.json +40 -18
package/lib/env.d.ts
ADDED
package/lib/env.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
const env = Object.freeze({
|
|
3
|
+
appId: process.env.BLOCKLET_APP_ID,
|
|
4
|
+
appName: process.env.BLOCKLET_APP_NAME,
|
|
5
|
+
appDescription: process.env.BLOCKLET_APP_DESCRIPTION,
|
|
6
|
+
appUrl: process.env.BLOCKLET_APP_URL,
|
|
7
|
+
isComponent: process.env.BLOCKLET_DID !== process.env.BLOCKLET_REAL_DID,
|
|
8
|
+
dataDir: process.env.BLOCKLET_DATA_DIR,
|
|
9
|
+
cacheDir: process.env.BLOCKLET_CACHE_DIR,
|
|
10
|
+
mode: process.env.BLOCKLET_MODE,
|
|
11
|
+
appStorageEndpoint: process.env.BLOCKLET_APP_STORAGE_ENDPOINT,
|
|
10
12
|
});
|
|
13
|
+
module.exports = env;
|
|
File without changes
|
package/lib/error-handler.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
process
|
|
2
|
-
|
|
3
|
-
console.error(
|
|
2
|
+
.on('uncaughtException', (err) => {
|
|
3
|
+
console.error(err.message);
|
|
4
4
|
process.exit(1);
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
})
|
|
6
|
+
.on('unhandledRejection', (reason) => {
|
|
7
7
|
console.error(reason.message);
|
|
8
8
|
process.exit(1);
|
|
9
|
-
|
|
9
|
+
});
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Auth from './service/auth';
|
|
2
|
+
import Notification from './service/notification';
|
|
3
|
+
import WalletAuthenticator from './wallet-authenticator';
|
|
4
|
+
import WalletHandlers from './wallet-handler';
|
|
5
|
+
import BlockletAuthenticator from './connect/authenticator';
|
|
6
|
+
import createConnectHandlers from './connect/handler';
|
|
7
|
+
import connectShared from './connect/shared';
|
|
8
|
+
import Database from './database/index';
|
|
9
|
+
import env from './env';
|
|
10
|
+
import middlewares from './middlewares/index';
|
|
11
|
+
import getWallet from './wallet';
|
|
12
|
+
import Component, { MountPoint } from './component/index';
|
|
13
|
+
import Security from './security/index';
|
|
14
|
+
import config from './config';
|
|
15
|
+
export { Auth as AuthService };
|
|
16
|
+
export { Auth };
|
|
17
|
+
export { Notification as NotificationService };
|
|
18
|
+
export { Notification };
|
|
19
|
+
export { WalletHandlers };
|
|
20
|
+
export { WalletAuthenticator };
|
|
21
|
+
export { BlockletAuthenticator };
|
|
22
|
+
export { createConnectHandlers };
|
|
23
|
+
export { connectShared };
|
|
24
|
+
export { Database };
|
|
25
|
+
export { getWallet };
|
|
26
|
+
export { env };
|
|
27
|
+
export { middlewares };
|
|
28
|
+
export { Component as component };
|
|
29
|
+
export { Component };
|
|
30
|
+
export { Security };
|
|
31
|
+
export { config };
|
|
32
|
+
declare global {
|
|
33
|
+
interface Window {
|
|
34
|
+
blocklet: {
|
|
35
|
+
[x: string]: any;
|
|
36
|
+
appId: string;
|
|
37
|
+
appName: string;
|
|
38
|
+
appDescription: string;
|
|
39
|
+
appLogo: string;
|
|
40
|
+
appUrl: string;
|
|
41
|
+
isComponent: boolean;
|
|
42
|
+
prefix: string;
|
|
43
|
+
groupPrefix: string;
|
|
44
|
+
version: string;
|
|
45
|
+
theme: any;
|
|
46
|
+
navigation: any[];
|
|
47
|
+
componentMountPoints: MountPoint[];
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export * from './component';
|
package/lib/index.js
CHANGED
|
@@ -1,41 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
module.exports = {
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated ES6: use `Auth` instead
|
|
18
|
-
*/
|
|
19
|
-
AuthService: Auth,
|
|
20
|
-
Auth,
|
|
21
|
-
/**
|
|
22
|
-
* @deprecated ES6: use `Notification` instead
|
|
23
|
-
*/
|
|
24
|
-
NotificationService: Notification,
|
|
25
|
-
Notification,
|
|
26
|
-
WalletHandlers,
|
|
27
|
-
WalletAuthenticator,
|
|
28
|
-
BlockletAuthenticator,
|
|
29
|
-
createConnectHandlers,
|
|
30
|
-
Database,
|
|
31
|
-
getWallet,
|
|
32
|
-
env,
|
|
33
|
-
middlewares,
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated ES6: use `Component` instead
|
|
36
|
-
*/
|
|
37
|
-
component: Component,
|
|
38
|
-
Component,
|
|
39
|
-
Security,
|
|
40
|
-
config,
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
41
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.config = exports.Security = exports.Component = exports.component = exports.middlewares = exports.env = exports.getWallet = exports.Database = exports.connectShared = exports.createConnectHandlers = exports.BlockletAuthenticator = exports.WalletAuthenticator = exports.WalletHandlers = exports.Notification = exports.NotificationService = exports.Auth = exports.AuthService = void 0;
|
|
21
|
+
const auth_1 = __importDefault(require("./service/auth"));
|
|
22
|
+
exports.AuthService = auth_1.default;
|
|
23
|
+
exports.Auth = auth_1.default;
|
|
24
|
+
const notification_1 = __importDefault(require("./service/notification"));
|
|
25
|
+
exports.NotificationService = notification_1.default;
|
|
26
|
+
exports.Notification = notification_1.default;
|
|
27
|
+
const wallet_authenticator_1 = __importDefault(require("./wallet-authenticator"));
|
|
28
|
+
exports.WalletAuthenticator = wallet_authenticator_1.default;
|
|
29
|
+
const wallet_handler_1 = __importDefault(require("./wallet-handler"));
|
|
30
|
+
exports.WalletHandlers = wallet_handler_1.default;
|
|
31
|
+
const authenticator_1 = __importDefault(require("./connect/authenticator"));
|
|
32
|
+
exports.BlockletAuthenticator = authenticator_1.default;
|
|
33
|
+
const handler_1 = __importDefault(require("./connect/handler"));
|
|
34
|
+
exports.createConnectHandlers = handler_1.default;
|
|
35
|
+
const shared_1 = __importDefault(require("./connect/shared"));
|
|
36
|
+
exports.connectShared = shared_1.default;
|
|
37
|
+
const index_1 = __importDefault(require("./database/index"));
|
|
38
|
+
exports.Database = index_1.default;
|
|
39
|
+
const env_1 = __importDefault(require("./env"));
|
|
40
|
+
exports.env = env_1.default;
|
|
41
|
+
const index_2 = __importDefault(require("./middlewares/index"));
|
|
42
|
+
exports.middlewares = index_2.default;
|
|
43
|
+
const wallet_1 = __importDefault(require("./wallet"));
|
|
44
|
+
exports.getWallet = wallet_1.default;
|
|
45
|
+
const index_3 = __importDefault(require("./component/index"));
|
|
46
|
+
exports.component = index_3.default;
|
|
47
|
+
exports.Component = index_3.default;
|
|
48
|
+
const index_4 = __importDefault(require("./security/index"));
|
|
49
|
+
exports.Security = index_4.default;
|
|
50
|
+
const config_1 = __importDefault(require("./config"));
|
|
51
|
+
exports.config = config_1.default;
|
|
52
|
+
__exportStar(require("./component"), exports);
|
package/lib/middlewares/auth.js
CHANGED
|
@@ -1,54 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
}
|
|
14
|
-
const res = await client.getPermissionsByRole(role);
|
|
15
|
-
cachedPermissions.set(role, res);
|
|
16
|
-
return res;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
13
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
throw new Error('permissions must be array');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const client = new _AuthClient();
|
|
32
|
-
|
|
33
|
-
return async (req, res, next) => {
|
|
34
|
-
if (!req.headers['x-user-did']) {
|
|
35
|
-
res.status(401).json({ code: 'forbidden', error: 'not authorized' });
|
|
36
|
-
return;
|
|
14
|
+
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
15
|
+
const auth_1 = __importDefault(require("../service/auth"));
|
|
16
|
+
const cachedPermissions = new lru_cache_1.default({
|
|
17
|
+
max: 10,
|
|
18
|
+
maxAge: 60 * 1000, // cache for 2min
|
|
19
|
+
});
|
|
20
|
+
const getPermissionsByRole = (client, role) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
const cached = cachedPermissions.get(role);
|
|
22
|
+
if (cached) {
|
|
23
|
+
return cached;
|
|
37
24
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
const res = yield client.getPermissionsByRole(role);
|
|
26
|
+
cachedPermissions.set(role, res);
|
|
27
|
+
return res;
|
|
28
|
+
});
|
|
29
|
+
// FIXME: 名字待确认, 全局的名称 Auth 已经被用了
|
|
30
|
+
const AuthMiddleware = ({ roles, permissions, _AuthClient = auth_1.default } = {}) => {
|
|
31
|
+
if (roles && !Array.isArray(roles)) {
|
|
32
|
+
throw new Error('roles must be array');
|
|
42
33
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const { permissions: list } = await getPermissionsByRole(client, req.headers['x-user-role']);
|
|
46
|
-
if (!permissions.some((x) => (list || []).some((y) => y.name === x))) {
|
|
47
|
-
res.status(403).json({ code: 'forbidden', error: 'no permission' });
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
34
|
+
if (permissions && !Array.isArray(permissions)) {
|
|
35
|
+
throw new Error('permissions must be array');
|
|
50
36
|
}
|
|
51
|
-
|
|
52
|
-
next()
|
|
53
|
-
|
|
37
|
+
const client = new _AuthClient();
|
|
38
|
+
return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
+
if (!req.headers['x-user-did']) {
|
|
40
|
+
res.status(401).json({ code: 'forbidden', error: 'not authorized' });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (roles && !roles.includes(req.headers['x-user-role'])) {
|
|
44
|
+
res.status(403).json({ code: 'forbidden', error: 'no permission' });
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (permissions) {
|
|
48
|
+
const { permissions: list } = yield getPermissionsByRole(client, req.headers['x-user-role']);
|
|
49
|
+
if (!permissions.some((x) => (list || []).some((y) => y.name === x))) {
|
|
50
|
+
res.status(403).json({ code: 'forbidden', error: 'no permission' });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
next();
|
|
55
|
+
});
|
|
54
56
|
};
|
|
57
|
+
module.exports = AuthMiddleware;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
declare const verifySig: (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
3
|
+
export { verifySig };
|
|
4
|
+
declare const _default: {
|
|
5
|
+
verifySig: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
6
|
+
};
|
|
7
|
+
export default _default;
|
|
@@ -1,28 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.verifySig = void 0;
|
|
7
|
+
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
|
|
8
|
+
const wallet_1 = __importDefault(require("../wallet"));
|
|
9
|
+
const config_1 = require("../config");
|
|
6
10
|
const verifySig = (req, res, next) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
try {
|
|
12
|
+
const wallet = (0, wallet_1.default)();
|
|
13
|
+
const sig = req.get('x-component-sig');
|
|
14
|
+
if (!sig) {
|
|
15
|
+
return res.status(400).json({ error: 'Bad Request' });
|
|
16
|
+
}
|
|
17
|
+
const data = typeof req.body === 'undefined' ? {} : req.body;
|
|
18
|
+
const verified = wallet.verify((0, json_stable_stringify_1.default)(data), sig);
|
|
19
|
+
if (!verified) {
|
|
20
|
+
return res.status(401).json({ error: 'verify sig failed' });
|
|
21
|
+
}
|
|
12
22
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const verified = wallet.verify(stableStringify(data), sig);
|
|
17
|
-
if (!verified) {
|
|
18
|
-
return res.status(401).json({ error: 'verify sig failed' });
|
|
23
|
+
catch (error) {
|
|
24
|
+
config_1.logger.error(error);
|
|
25
|
+
return res.status(401).json({ error: 'verify sig failed' });
|
|
19
26
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return next();
|
|
27
|
+
return next();
|
|
28
|
+
};
|
|
29
|
+
exports.verifySig = verifySig;
|
|
30
|
+
exports.default = {
|
|
31
|
+
verifySig,
|
|
26
32
|
};
|
|
27
|
-
|
|
28
|
-
module.exports = { verifySig };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import user from './user';
|
|
2
|
+
import auth from './auth';
|
|
3
|
+
import component from './component';
|
|
4
|
+
export { user };
|
|
5
|
+
export { auth };
|
|
6
|
+
export { component };
|
|
7
|
+
declare const _default: {
|
|
8
|
+
user: () => (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>> & {
|
|
9
|
+
user?: {
|
|
10
|
+
did: string;
|
|
11
|
+
role: string;
|
|
12
|
+
fullName: string;
|
|
13
|
+
};
|
|
14
|
+
}, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => Promise<void>;
|
|
15
|
+
auth: ({ roles, permissions, _AuthClient }?: any) => (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => Promise<void>;
|
|
16
|
+
component: {
|
|
17
|
+
verifySig: (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => void | import("express").Response<any, Record<string, any>>;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export default _default;
|
package/lib/middlewares/index.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.component = exports.auth = exports.user = void 0;
|
|
7
|
+
const user_1 = __importDefault(require("./user"));
|
|
8
|
+
exports.user = user_1.default;
|
|
9
|
+
const auth_1 = __importDefault(require("./auth"));
|
|
10
|
+
exports.auth = auth_1.default;
|
|
11
|
+
const component_1 = __importDefault(require("./component"));
|
|
12
|
+
exports.component = component_1.default;
|
|
13
|
+
exports.default = {
|
|
14
|
+
user: user_1.default,
|
|
15
|
+
auth: auth_1.default,
|
|
16
|
+
component: component_1.default,
|
|
9
17
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
declare type User = {
|
|
3
|
+
did: string;
|
|
4
|
+
role: string | undefined;
|
|
5
|
+
fullName: string;
|
|
6
|
+
};
|
|
7
|
+
declare const userMiddleware: () => (req: Request & {
|
|
8
|
+
user?: User;
|
|
9
|
+
}, res: Response, next: NextFunction) => Promise<void>;
|
|
10
|
+
export = userMiddleware;
|
package/lib/middlewares/user.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
next();
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
11
10
|
};
|
|
11
|
+
const userMiddleware = () => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
12
|
+
if (req.headers['x-user-did']) {
|
|
13
|
+
req.user = {
|
|
14
|
+
did: req.headers['x-user-did'],
|
|
15
|
+
role: req.headers['x-user-role'],
|
|
16
|
+
fullName: decodeURIComponent(req.headers['x-user-fullname']),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
next();
|
|
20
|
+
});
|
|
21
|
+
module.exports = userMiddleware;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const encrypt: (message: string) => string;
|
|
2
|
+
declare const decrypt: (message: string) => string;
|
|
3
|
+
export { encrypt };
|
|
4
|
+
export { decrypt };
|
|
5
|
+
declare const _default: {
|
|
6
|
+
encrypt: (message: string) => string;
|
|
7
|
+
decrypt: (message: string) => string;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
package/lib/security/index.js
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.decrypt = exports.encrypt = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const aes_1 = __importDefault(require("@ocap/mcrypto/lib/crypter/aes"));
|
|
9
|
+
const AES = { default: aes_1.default }.default;
|
|
4
10
|
const encrypt = (message) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
message,
|
|
10
|
-
crypto.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex')
|
|
11
|
-
);
|
|
11
|
+
if (!process.env.BLOCKLET_DID || !process.env.BLOCKLET_APP_EK) {
|
|
12
|
+
return message;
|
|
13
|
+
}
|
|
14
|
+
return AES.encrypt(message, crypto_1.default.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex'));
|
|
12
15
|
};
|
|
16
|
+
exports.encrypt = encrypt;
|
|
13
17
|
const decrypt = (message) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
message,
|
|
19
|
-
crypto.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex')
|
|
20
|
-
);
|
|
18
|
+
if (!process.env.BLOCKLET_DID || !process.env.BLOCKLET_APP_EK) {
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
return AES.decrypt(message, crypto_1.default.pbkdf2Sync(process.env.BLOCKLET_APP_EK, process.env.BLOCKLET_DID, 256, 32, 'sha512').toString('hex'));
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
exports.decrypt = decrypt;
|
|
24
|
+
exports.default = {
|
|
25
|
+
encrypt,
|
|
26
|
+
decrypt,
|
|
26
27
|
};
|