@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/service/auth.js
CHANGED
|
@@ -1,175 +1,141 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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 };
|
|
13
|
+
};
|
|
14
|
+
/* eslint-disable @typescript-eslint/indent */
|
|
15
|
+
const pickBy_1 = __importDefault(require("lodash/pickBy"));
|
|
16
|
+
const url_join_1 = __importDefault(require("url-join"));
|
|
17
|
+
const client_1 = __importDefault(require("@abtnode/client"));
|
|
18
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
19
|
+
const wallet_1 = require("@ocap/wallet");
|
|
20
|
+
const constant_1 = __importDefault(require("@abtnode/constant"));
|
|
21
|
+
const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
|
|
22
|
+
const version_1 = require("../version");
|
|
23
|
+
const { NODE_SERVICES, WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_URL_PREFIX, USER_AVATAR_PATH_PREFIX } = constant_1.default;
|
|
24
|
+
const VERSION = version_1.version; // version of notification sdk
|
|
16
25
|
// Auth 相关的功能 API 都在这里
|
|
17
26
|
// core/state/lib/api/team.js L42
|
|
18
|
-
|
|
19
27
|
// 所有可配置调用的函数在这里,如果需要额外增加,则需要在这里新增对应的函数
|
|
20
28
|
// core/gql/lib/config.js L25
|
|
21
|
-
|
|
22
29
|
const isNotNullOrUndefined = (x) => ![null, undefined].includes(x);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
process.env.BLOCKLET_WALLET_TYPE !== 'eth'
|
|
26
|
-
? WalletType({ role: types.RoleType.ROLE_APPLICATION, pk: types.KeyType.ED25519, hash: types.HashType.SHA3 })
|
|
30
|
+
const type = process.env.BLOCKLET_WALLET_TYPE !== 'eth'
|
|
31
|
+
? (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 })
|
|
27
32
|
: 'eth';
|
|
28
|
-
|
|
29
33
|
const fixAvatar = (user) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
avatar.replace(USER_AVATAR_URL_PREFIX, '')
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return user;
|
|
34
|
+
const avatar = user === null || user === void 0 ? void 0 : user.avatar;
|
|
35
|
+
if (avatar && avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
|
|
36
|
+
user.avatar = (0, url_join_1.default)(WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_PATH_PREFIX, avatar.replace(USER_AVATAR_URL_PREFIX, ''));
|
|
37
|
+
}
|
|
38
|
+
return user;
|
|
39
39
|
};
|
|
40
|
-
|
|
41
|
-
class AuthService extends
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
(fn) =>
|
|
141
|
-
({ name, description }) =>
|
|
142
|
-
fn({ input: { ...pickBy({ name, description }, isNotNullOrUndefined), teamDid } }),
|
|
143
|
-
updatePermission:
|
|
144
|
-
(fn) =>
|
|
145
|
-
(name, { description }) =>
|
|
146
|
-
fn({ input: { permission: pickBy({ name, description }, isNotNullOrUndefined), teamDid } }), // todo
|
|
147
|
-
deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
148
|
-
hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
apiList.forEach((api) => {
|
|
152
|
-
const fn = this[api];
|
|
153
|
-
this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
// eslint-disable-next-line no-constructor-return
|
|
157
|
-
return new Proxy(this, {
|
|
158
|
-
get(target, propKey) {
|
|
159
|
-
if (!apiList.includes(propKey)) {
|
|
160
|
-
return undefined;
|
|
161
|
-
}
|
|
162
|
-
return target[propKey];
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
_getAuthHeaders() {
|
|
168
|
-
const headers = super._getAuthHeaders();
|
|
169
|
-
headers['x-access-blocklet'] = this._blockletDid;
|
|
170
|
-
headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
|
|
171
|
-
return headers;
|
|
172
|
-
}
|
|
40
|
+
// FIXME: types from @abtnode/types
|
|
41
|
+
class AuthService extends client_1.default {
|
|
42
|
+
constructor(httpEndpoint) {
|
|
43
|
+
(0, check_blocklet_env_1.default)();
|
|
44
|
+
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 2.
|
|
45
|
+
super(httpEndpoint || `http://127.0.0.1:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
|
|
46
|
+
this.setAuthAccessKey({
|
|
47
|
+
accessKeyId: process.env.BLOCKLET_APP_ID,
|
|
48
|
+
accessKeySecret: process.env.BLOCKLET_APP_SK,
|
|
49
|
+
type,
|
|
50
|
+
});
|
|
51
|
+
this._blockletDid = process.env.BLOCKLET_DID;
|
|
52
|
+
const apiList = [
|
|
53
|
+
// user
|
|
54
|
+
'getUsers',
|
|
55
|
+
'getUser',
|
|
56
|
+
'getOwner',
|
|
57
|
+
// 'removeUser',
|
|
58
|
+
'updateUserRole',
|
|
59
|
+
// 'updateUserApproval',
|
|
60
|
+
// invitation
|
|
61
|
+
// 'getInvitations',
|
|
62
|
+
// 'createMemberInvitation',
|
|
63
|
+
// 'deleteInvitation',
|
|
64
|
+
// rbac
|
|
65
|
+
'getRoles',
|
|
66
|
+
'createRole',
|
|
67
|
+
'updateRole',
|
|
68
|
+
'deleteRole',
|
|
69
|
+
'grantPermissionForRole',
|
|
70
|
+
'revokePermissionFromRole',
|
|
71
|
+
'updatePermissionsForRole',
|
|
72
|
+
'hasPermission',
|
|
73
|
+
// user passport
|
|
74
|
+
'issuePassportToUser',
|
|
75
|
+
'revokeUserPassport',
|
|
76
|
+
'enableUserPassport',
|
|
77
|
+
// 'getPassportIssuances',
|
|
78
|
+
// 'createPassportIssuance',
|
|
79
|
+
// 'deletePassportIssuance',
|
|
80
|
+
// permission
|
|
81
|
+
'getPermissions',
|
|
82
|
+
'createPermission',
|
|
83
|
+
'updatePermission',
|
|
84
|
+
'deletePermission',
|
|
85
|
+
'getPermissionsByRole',
|
|
86
|
+
// disabled in current
|
|
87
|
+
// 'configTrustedPassports',
|
|
88
|
+
];
|
|
89
|
+
const teamDid = this._blockletDid;
|
|
90
|
+
const apiFallback = (fn) => (params = {}, ...args) => fn(Object.assign({ input: Object.assign(Object.assign({}, params), { teamDid }) }, args));
|
|
91
|
+
const apiConvertDid = (fn) => (did) => fn({ input: { user: { did }, teamDid } });
|
|
92
|
+
const apiFnMap = {
|
|
93
|
+
getUser: (fn) => (did) => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
const res = yield fn({ input: { user: { did }, teamDid } });
|
|
95
|
+
fixAvatar(res.user);
|
|
96
|
+
return res;
|
|
97
|
+
}),
|
|
98
|
+
getUsers: (fn) => (args) => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const res = yield fn({ input: Object.assign({ teamDid }, args) });
|
|
100
|
+
(res.users || []).forEach(fixAvatar);
|
|
101
|
+
return res;
|
|
102
|
+
}),
|
|
103
|
+
removeUser: apiConvertDid,
|
|
104
|
+
updateUserRole: (fn) => (did, role) => fn({ input: { user: { did, role }, teamDid } }),
|
|
105
|
+
updateUserApproval: (fn) => (did, approved) => fn({ input: { user: { did, approved }, teamDid } }),
|
|
106
|
+
getPermissionsByRole: (fn) => (name) => fn({ input: { role: { name }, teamDid } }),
|
|
107
|
+
createRole: (fn) => ({ name, title, description }) => fn({ input: Object.assign(Object.assign({}, (0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined)), { teamDid }) }),
|
|
108
|
+
updateRole: (fn) => (name, { title, description }) => fn({ input: { role: (0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined), teamDid } }),
|
|
109
|
+
deleteRole: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
110
|
+
grantPermissionForRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
111
|
+
revokePermissionFromRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
|
|
112
|
+
updatePermissionsForRole: (fn) => (roleName, permissionNames) => fn({ input: { teamDid, roleName, grantNames: permissionNames } }),
|
|
113
|
+
createPermission: (fn) => ({ name, description }) => fn({ input: Object.assign(Object.assign({}, (0, pickBy_1.default)({ name, description }, isNotNullOrUndefined)), { teamDid }) }),
|
|
114
|
+
updatePermission: (fn) => (name, { description }) => fn({ input: { permission: (0, pickBy_1.default)({ name, description }, isNotNullOrUndefined), teamDid } }),
|
|
115
|
+
deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
|
|
116
|
+
hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
|
|
117
|
+
};
|
|
118
|
+
apiList.forEach((api) => {
|
|
119
|
+
const fn = this[api];
|
|
120
|
+
this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
|
|
121
|
+
});
|
|
122
|
+
// eslint-disable-next-line no-constructor-return
|
|
123
|
+
return new Proxy(this, {
|
|
124
|
+
get(target, propKey) {
|
|
125
|
+
// @ts-expect-error TS(2345) FIXME: Argument of type 'string | symbol' is not assignab... Remove this comment to see the full error message
|
|
126
|
+
if (!apiList.includes(propKey)) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return target[propKey];
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
_getAuthHeaders() {
|
|
134
|
+
// @ts-expect-error TS(2339) FIXME: Property '_getAuthHeaders' does not exist on type ... Remove this comment to see the full error message
|
|
135
|
+
const headers = super._getAuthHeaders();
|
|
136
|
+
headers['x-access-blocklet'] = this._blockletDid;
|
|
137
|
+
headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
|
|
138
|
+
return headers;
|
|
139
|
+
}
|
|
173
140
|
}
|
|
174
|
-
|
|
175
141
|
module.exports = AuthService;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
import { TNotificationInput, TSendOptions } from '../types/notification';
|
|
4
|
+
declare type $TSFixMe = any;
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {Notification} notification
|
|
8
|
+
* @param {{
|
|
9
|
+
* keepForOfflineUser: Boolean
|
|
10
|
+
* }} options
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
declare const doSendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param {Notification} notification
|
|
17
|
+
* @param {{
|
|
18
|
+
* channel: String
|
|
19
|
+
* event: String
|
|
20
|
+
* socketId: String
|
|
21
|
+
* socketDid: String
|
|
22
|
+
* }} options
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
declare const broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
26
|
+
export declare const on: (event: string, cb?: $TSFixMe) => EventEmitter;
|
|
27
|
+
export declare const off: any;
|
|
28
|
+
export declare const _message: {
|
|
29
|
+
on: (event: string, cb: $TSFixMe) => EventEmitter;
|
|
30
|
+
off: any;
|
|
31
|
+
};
|
|
32
|
+
export { doSendToUser as sendToUser };
|
|
33
|
+
export { broadcast };
|
|
34
|
+
declare const _default: {
|
|
35
|
+
sendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
36
|
+
broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
|
|
37
|
+
on: (event: string, cb?: any) => EventEmitter;
|
|
38
|
+
off: any;
|
|
39
|
+
_message: {
|
|
40
|
+
on: (event: string, cb: any) => EventEmitter;
|
|
41
|
+
off: any;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export default _default;
|