@abtnode/auth 1.16.6-beta-7cbab489 → 1.16.6-beta-61cf68d3

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/auth.js CHANGED
@@ -399,8 +399,8 @@ const createAuthTokenByOwnershipNFT = ({ did, role, secret, expiresIn } = {}) =>
399
399
  return jwt.sign(payload, secret, { expiresIn });
400
400
  };
401
401
 
402
- const getUser = async (node, teamDid, userDid) => {
403
- const user = await node.getUser({ teamDid, user: { did: userDid } });
402
+ const getUser = async (node, teamDid, userDid, options = {}) => {
403
+ const user = await node.getUser({ teamDid, user: { did: userDid }, options });
404
404
  return user;
405
405
  };
406
406
 
@@ -541,23 +541,27 @@ const handleInvitationReceive = async ({
541
541
  const role = getRoleFromLocalPassport(get(vc, 'credentialSubject.passport'));
542
542
  const passport = createUserPassport(vc, { role });
543
543
 
544
- const user = await getUser(node, teamDid, userDid);
544
+ const user = await getUser(node, teamDid, userDid, {
545
+ enableConnectedAccount: true,
546
+ });
545
547
 
548
+ // NOTICE: owner 目前必须是 did-wallet 的账户,暂不做额外 did 判断
546
549
  if (role === 'owner') {
547
550
  if (issuerType === TEAM_TYPE.blocklet) {
548
551
  // should not be here
549
552
  throw new Error('not allowed to transfer application ownership');
550
553
  }
551
554
 
552
- // TODO: @zhanghan 判断方式应该是 userDid === nodeInfo.nodeOwner.did
553
- // by @linchen
554
- if (user && user.role === 'owner') {
555
+ if (userDid === nodeInfo.nodeOwner.did) {
555
556
  throw new Error(messages.alreadyTransferred[locale](userDid));
556
557
  }
557
558
 
558
559
  await node.updateNodeOwner({ nodeOwner: { did: userDid, pk: userPk } });
559
560
 
560
- const originalOwner = await getUser(node, teamDid, nodeInfo.nodeOwner.did);
561
+ // NOTICE: 这里理论上不需要查询 connectedAccount (owner 必须是 did-wallet 的账户),加上后也不影响
562
+ const originalOwner = await getUser(node, teamDid, nodeInfo.nodeOwner.did, {
563
+ enableConnectedAccount: true,
564
+ });
561
565
  const originalOwnerPassport = (originalOwner.passports || []).find((p) => p.role === 'owner');
562
566
  if (originalOwnerPassport) {
563
567
  await node.revokeUserPassport({ teamDid, userDid: nodeInfo.nodeOwner.did, passportId: originalOwnerPassport.id });
@@ -579,8 +583,8 @@ const handleInvitationReceive = async ({
579
583
  user: {
580
584
  ...profile,
581
585
  avatar,
582
- did: userDid,
583
- pk: userPk,
586
+ did: user.did,
587
+ pk: user.pk,
584
588
  locale,
585
589
  passport,
586
590
  lastLoginIp: get(req, 'headers[x-real-ip]') || '',
@@ -595,7 +599,7 @@ const handleInvitationReceive = async ({
595
599
  await node.createAuditLog(
596
600
  {
597
601
  action: 'updateUser',
598
- args: { teamDid, userDid, passport, inviteId, reason: 'accepted invitation' },
602
+ args: { teamDid, userDid: user.did, passport, inviteId, reason: 'accepted invitation' },
599
603
  context: formatContext(Object.assign(req, { user })),
600
604
  result: doc,
601
605
  },
@@ -623,7 +627,7 @@ const handleInvitationReceive = async ({
623
627
  await node.createAuditLog(
624
628
  {
625
629
  action: 'addUser',
626
- args: { teamDid, userDid, passport, inviteId, reason: 'accepted invitation' },
630
+ args: { teamDid, userDid: user?.did || userDid, passport, inviteId, reason: 'accepted invitation' },
627
631
  context: formatContext(Object.assign(req, { user: doc })),
628
632
  result: doc,
629
633
  },
@@ -631,14 +635,14 @@ const handleInvitationReceive = async ({
631
635
  );
632
636
  }
633
637
 
634
- logger.info('invite success', { userDid });
638
+ logger.info('invite success', { userDid: user?.did || userDid });
635
639
 
636
640
  // await node.closeInvitation({ teamDid, inviteId, status: 'success', receiver: { did: userDid, role } });
637
641
  await node.closeInvitation({
638
642
  teamDid,
639
643
  inviteId,
640
644
  status: 'success',
641
- receiver: { did: userDid, role, timeout: 1000 * 9999 },
645
+ receiver: { did: user?.did || userDid, role, timeout: 1000 * 9999 },
642
646
  });
643
647
 
644
648
  return {
@@ -725,6 +729,7 @@ const createIssuePassportRequest = async ({ node, nodeInfo, teamDid, id, locale
725
729
  throw new Error('Cannot receive owner passport because the owner already exists');
726
730
  }
727
731
 
732
+ // NOTICE: 这里是给指定用户颁发 passport,绑定的 did 无需查询 connectedAccount
728
733
  const user = await getUser(node, teamDid, issuanceInfo.ownerDid);
729
734
 
730
735
  const passport = await createPassport({
@@ -774,7 +779,11 @@ const handleIssuePassportResponse = async ({
774
779
  const claim = claims.find((x) => x.type === 'signature');
775
780
  verifySignature(claim, userDid, userPk, locale);
776
781
 
777
- const user = await getUser(node, teamDid, userDid);
782
+ const user = await getUser(node, teamDid, userDid, {
783
+ enableConnectedAccount: true,
784
+ });
785
+ const realDid = user.did || userDid;
786
+ const realPk = user.pk || userPk;
778
787
  if (user && !user.approved) {
779
788
  throw new Error(
780
789
  {
@@ -802,7 +811,7 @@ const handleIssuePassportResponse = async ({
802
811
  throw new Error('Cannot receive Owner Passport because the owner already exists');
803
812
  }
804
813
 
805
- if (ownerDid !== userDid) {
814
+ if (ownerDid !== realDid) {
806
815
  throw new Error(messages.notOwner[locale]);
807
816
  }
808
817
 
@@ -844,8 +853,8 @@ const handleIssuePassportResponse = async ({
844
853
  await node.updateUser({
845
854
  teamDid,
846
855
  user: {
847
- did: userDid,
848
- pk: userPk,
856
+ did: realDid,
857
+ pk: realPk,
849
858
  passports: upsertToPassports(user.passports || [], passport),
850
859
  },
851
860
  });
@@ -856,7 +865,7 @@ const handleIssuePassportResponse = async ({
856
865
  await node.createAuditLog(
857
866
  {
858
867
  action: 'processPassportIssuance',
859
- args: { teamDid, userDid, ...result, sessionId: id, reason: 'claimed passport' },
868
+ args: { teamDid, userDid: realDid, ...result, sessionId: id, reason: 'claimed passport' },
860
869
  context: formatContext(Object.assign(req, { user })),
861
870
  result,
862
871
  },
@@ -864,8 +873,8 @@ const handleIssuePassportResponse = async ({
864
873
  );
865
874
 
866
875
  if (name === ROLES.OWNER && issuerType === TEAM_TYPE.BLOCKLET) {
867
- logger.info('Bind owner for blocklet', { teamDid, userDid });
868
- await node.setBlockletInitialized({ did: teamDid, owner: { did: userDid, pk: userPk } });
876
+ logger.info('Bind owner for blocklet', { teamDid, userDid: realDid });
877
+ await node.setBlockletInitialized({ did: teamDid, owner: { did: realDid, pk: userPk } });
869
878
  }
870
879
 
871
880
  await updateSession({ passportId: vc.id });
@@ -969,7 +978,8 @@ const getPassportStatus = async ({ node, teamDid, userDid, vcId, locale = 'en' }
969
978
  },
970
979
  };
971
980
 
972
- const user = await node.getUser({ teamDid, user: { did: userDid } });
981
+ // NOTICE: 该方法使用的地方是外部通过 api 来使用,无法控制输入的 userDid,所以需要查询 connectedAccount,加上后也不影响任何逻辑
982
+ const user = await node.getUser({ teamDid, user: { did: userDid }, options: { enableConnectedAccount: true } });
973
983
 
974
984
  if (!user) {
975
985
  throw new Error(messages.userNotFound[locale]);
package/lib/invitation.js CHANGED
@@ -63,11 +63,13 @@ module.exports = {
63
63
  role.permissions = [];
64
64
  }
65
65
 
66
+ // NOTICE: 邀请人的 did 为永久 did,无需查询 connectedAccount
66
67
  let user = await node.getUser({ teamDid: info.did, user: { did: invitation.inviter.did } });
67
68
  let avatar = user && (await parseUserAvatar(user.avatar, { dataDir: info.dataDir }));
68
69
 
69
70
  // blocklet 邀请链接可能是 server 的 member
70
71
  if (!user && type === 'blocklet') {
72
+ // NOTICE: 邀请人的 did 为永久 did,无需查询 connectedAccount
71
73
  user = await node.getUser({ teamDid: nodeInfo.did, user: { did: invitation.inviter.did } });
72
74
  avatar =
73
75
  user &&
@@ -10,6 +10,7 @@ const { getDisplayName, getBlockletAppIdList } = require('@blocklet/meta/lib/uti
10
10
  const { VC_TYPE_NODE_PASSPORT, PASSPORT_STATUS, NODE_DATA_DIR_NAME } = require('@abtnode/constant');
11
11
  const get = require('lodash/get');
12
12
  const { parseUserAvatar } = require('@abtnode/util/lib/user-avatar');
13
+ const { getWalletDid } = require('@blocklet/meta/lib/did-utils');
13
14
 
14
15
  const logger = require('./logger');
15
16
  const { messages, getUser, checkWalletVersion, getPassportStatusEndpoint } = require('./auth');
@@ -103,7 +104,9 @@ const createLostPassportListRoute = ({ node, type }) => ({
103
104
  const { teamDid, issuerDidList, dataDir } = await getApplicationInfo({ node, req, type });
104
105
 
105
106
  // check user approved
106
- const user = await getUser(node, teamDid, userDid);
107
+ const user = await getUser(node, teamDid, userDid, {
108
+ enableConnectedAccount: true,
109
+ });
107
110
 
108
111
  if (!user) {
109
112
  throw new Error(messages.userNotFound[locale]);
@@ -131,7 +134,7 @@ const createLostPassportListRoute = ({ node, type }) => ({
131
134
  throw new Error(messages.passportNotFound[locale]);
132
135
  }
133
136
 
134
- logger.info('get passport type list', { userDid });
137
+ logger.info('get passport type list', { userDid: user.did });
135
138
 
136
139
  user.avatar = await parseUserAvatar(user.avatar, { did: teamDid, dataDir });
137
140
 
@@ -149,10 +152,19 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
149
152
  claims: [
150
153
  {
151
154
  authPrincipal: async ({ extraParams }) => {
152
- const { receiverDid } = extraParams;
155
+ // HACK: authPrincipal 中无法拿到 request 对象,只能由前端传来 teamDid
156
+ const { receiverDid, teamDid } = extraParams;
157
+ let walletDid = receiverDid;
158
+ // 兼容不包含 teamDid 字段的情况
159
+ if (teamDid) {
160
+ const user = await getUser(node, teamDid, receiverDid, {
161
+ enableConnectedAccount: true,
162
+ });
163
+ walletDid = getWalletDid(user);
164
+ }
153
165
  return {
154
166
  description: 'Please select the required DID',
155
- target: receiverDid,
167
+ target: walletDid,
156
168
  };
157
169
  },
158
170
  },
@@ -166,7 +178,9 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
166
178
  req: request,
167
179
  type,
168
180
  });
169
- const user = await getUser(node, teamDid, receiverDid);
181
+ const user = await getUser(node, teamDid, receiverDid, {
182
+ enableConnectedAccount: true,
183
+ });
170
184
 
171
185
  const passport = await createPassport({
172
186
  name: passportName,
@@ -210,7 +224,21 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
210
224
  logger.info('claim.signature.onAuth', { userPk, userDid, claim });
211
225
  verifySignature(claim, userDid, userPk, locale);
212
226
 
213
- if (receiverDid !== userDid) {
227
+ // check user approved
228
+ const user = await getUser(node, teamDid, userDid, {
229
+ enableConnectedAccount: true,
230
+ });
231
+
232
+ // 二次校验用户是否存在
233
+ if (!user) {
234
+ throw new Error(messages.userNotFound[locale]);
235
+ }
236
+ if (!user.approved) {
237
+ throw new Error(messages.notAllowed[locale]);
238
+ }
239
+
240
+ // NOTICE: 实际测试过程中,receiverDid 是当前登录用户的永久 did,而 userDid 是 wallet did,所以需要经过转换才能比较
241
+ if (receiverDid !== user.did) {
214
242
  // should not be here
215
243
  throw new Error(
216
244
  {
@@ -220,12 +248,6 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
220
248
  );
221
249
  }
222
250
 
223
- // check user approved
224
- const user = await getUser(node, teamDid, userDid);
225
- if (!user.approved) {
226
- throw new Error(messages.notAllowed[locale]);
227
- }
228
-
229
251
  // check passport
230
252
  const exist = (user.passports || []).find(
231
253
  (x) =>
@@ -280,15 +302,15 @@ const createLostPassportIssueRoute = ({ node, type, authServicePrefix }) => ({
280
302
  const result = await node.updateUser({
281
303
  teamDid,
282
304
  user: {
283
- did: userDid,
284
- pk: userPk,
305
+ did: user.did,
306
+ pk: user.pk,
285
307
  passports: upsertToPassports(user.passports || [], passport),
286
308
  },
287
309
  });
288
310
  await node.createAuditLog(
289
311
  {
290
312
  action: 'updateUser',
291
- args: { teamDid, userDid, passport, reason: 'recovered passport' },
313
+ args: { teamDid, userDid: user.did, passport, reason: 'recovered passport' },
292
314
  context: formatContext(Object.assign(req, { user })),
293
315
  result,
294
316
  },
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.6-beta-7cbab489",
6
+ "version": "1.16.6-beta-61cf68d3",
7
7
  "description": "Simple lib to manage auth in ABT Node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -20,16 +20,16 @@
20
20
  "author": "linchen <linchen1987@foxmail.com> (http://github.com/linchen1987)",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@abtnode/constant": "1.16.6-beta-7cbab489",
24
- "@abtnode/logger": "1.16.6-beta-7cbab489",
25
- "@abtnode/util": "1.16.6-beta-7cbab489",
26
- "@arcblock/did": "1.18.75",
27
- "@arcblock/vc": "1.18.75",
28
- "@blocklet/constant": "1.16.6-beta-7cbab489",
29
- "@blocklet/meta": "1.16.6-beta-7cbab489",
30
- "@ocap/mcrypto": "1.18.75",
31
- "@ocap/util": "1.18.75",
32
- "@ocap/wallet": "1.18.75",
23
+ "@abtnode/constant": "1.16.6-beta-61cf68d3",
24
+ "@abtnode/logger": "1.16.6-beta-61cf68d3",
25
+ "@abtnode/util": "1.16.6-beta-61cf68d3",
26
+ "@arcblock/did": "1.18.76",
27
+ "@arcblock/vc": "1.18.76",
28
+ "@blocklet/constant": "1.16.6-beta-61cf68d3",
29
+ "@blocklet/meta": "1.16.6-beta-61cf68d3",
30
+ "@ocap/mcrypto": "1.18.76",
31
+ "@ocap/util": "1.18.76",
32
+ "@ocap/wallet": "1.18.76",
33
33
  "joi": "17.7.0",
34
34
  "jsonwebtoken": "^9.0.0",
35
35
  "lodash": "^4.17.21",
@@ -41,5 +41,5 @@
41
41
  "devDependencies": {
42
42
  "jest": "^27.5.1"
43
43
  },
44
- "gitHead": "36b58c0651762472ac34ad880965c47510eb9768"
44
+ "gitHead": "574a27436b429a97f443fc9ea1b44988807206bd"
45
45
  }