@abtnode/auth 1.16.0-beta-8ee536d7 → 1.16.0-beta-62b42401
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 +74 -50
- package/lib/server.js +40 -1
- package/package.json +14 -14
package/lib/auth.js
CHANGED
|
@@ -16,6 +16,7 @@ const {
|
|
|
16
16
|
NODE_DATA_DIR_NAME,
|
|
17
17
|
AUTH_CERT_TYPE,
|
|
18
18
|
WELLKNOWN_BLOCKLET_ADMIN_PATH,
|
|
19
|
+
USER_TYPE,
|
|
19
20
|
} = require('@abtnode/constant');
|
|
20
21
|
const axios = require('@abtnode/util/lib/axios');
|
|
21
22
|
const { extractUserAvatar, parseUserAvatar } = require('@abtnode/util/lib/user-avatar');
|
|
@@ -398,16 +399,7 @@ const getUser = async (node, teamDid, userDid) => {
|
|
|
398
399
|
};
|
|
399
400
|
|
|
400
401
|
const beforeInvitationRequest = async ({ node, teamDid, inviteId, locale = 'en' }) => {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if (!inviteInfo) {
|
|
404
|
-
throw new Error(
|
|
405
|
-
{
|
|
406
|
-
en: 'The invitation link does not exist or has been used',
|
|
407
|
-
zh: '邀请链接不存在或已被使用',
|
|
408
|
-
}[locale]
|
|
409
|
-
);
|
|
410
|
-
}
|
|
402
|
+
await node.checkInvitation({ teamDid, inviteId });
|
|
411
403
|
|
|
412
404
|
const count = await node.getUsersCount({ teamDid });
|
|
413
405
|
if (count === 0) {
|
|
@@ -422,10 +414,8 @@ const beforeInvitationRequest = async ({ node, teamDid, inviteId, locale = 'en'
|
|
|
422
414
|
|
|
423
415
|
const createInvitationRequest = async ({ node, nodeInfo, teamDid, inviteId, locale = 'en' }) => {
|
|
424
416
|
// verify invite id
|
|
417
|
+
await node.checkInvitation({ teamDid, inviteId });
|
|
425
418
|
const inviteInfo = await node.getInvitation({ teamDid, inviteId });
|
|
426
|
-
if (!inviteInfo) {
|
|
427
|
-
throw new Error('The invitation does not exist or has been used');
|
|
428
|
-
}
|
|
429
419
|
|
|
430
420
|
const {
|
|
431
421
|
name: issuerName,
|
|
@@ -457,55 +447,51 @@ const createInvitationRequest = async ({ node, nodeInfo, teamDid, inviteId, loca
|
|
|
457
447
|
};
|
|
458
448
|
};
|
|
459
449
|
|
|
460
|
-
const
|
|
461
|
-
req = {},
|
|
450
|
+
const handleInvitationReceive = async ({
|
|
462
451
|
node,
|
|
452
|
+
req,
|
|
463
453
|
nodeInfo,
|
|
454
|
+
locale = 'en',
|
|
464
455
|
teamDid,
|
|
456
|
+
inviteId,
|
|
465
457
|
userDid,
|
|
466
458
|
userPk,
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
claims,
|
|
459
|
+
userSource = USER_TYPE.WALLET,
|
|
460
|
+
newNftOwner,
|
|
470
461
|
statusEndpointBaseUrl,
|
|
471
462
|
endpoint,
|
|
472
|
-
|
|
463
|
+
profile,
|
|
473
464
|
}) => {
|
|
474
465
|
if (!nodeInfo.nodeOwner) {
|
|
475
466
|
throw new Error(messages.notInitialized[locale]);
|
|
476
467
|
}
|
|
477
|
-
|
|
478
|
-
const claim = claims.find((x) => x.type === 'signature');
|
|
479
|
-
verifySignature(claim, userDid, userPk, locale);
|
|
480
|
-
|
|
481
|
-
const inviteInfo = await node.getInvitation({ teamDid, inviteId });
|
|
482
|
-
if (!inviteInfo) {
|
|
483
|
-
throw new Error(`The invitation does not exist: ${inviteId}`);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
if (inviteInfo.role === 'owner' && userDid === nodeInfo.nodeOwner.did) {
|
|
487
|
-
throw new Error(messages.notAllowedTransferToSelf[locale]);
|
|
488
|
-
}
|
|
489
|
-
|
|
490
468
|
await node.checkInvitation({ teamDid, inviteId });
|
|
491
469
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (assetState.owner !== newNftOwner) {
|
|
499
|
-
const hash = await client.transfer({
|
|
500
|
-
delegator: get(nodeInfo, 'ownerNft.holder'),
|
|
501
|
-
to: newNftOwner,
|
|
502
|
-
assets: [ownerNftDid],
|
|
503
|
-
wallet: getNodeWallet(nodeInfo.sk),
|
|
504
|
-
});
|
|
470
|
+
const inviteInfo = await node.getInvitation({ teamDid, inviteId });
|
|
471
|
+
if (inviteInfo.role === 'owner') {
|
|
472
|
+
// 禁止将 owner 转移给自己的逻辑
|
|
473
|
+
if (userDid === nodeInfo.nodeOwner.did) {
|
|
474
|
+
throw new Error(messages.notAllowedTransferToSelf[locale]);
|
|
475
|
+
}
|
|
505
476
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
477
|
+
if (get(nodeInfo, 'ownerNft.holder')) {
|
|
478
|
+
// 这种情况下是 Transfer 有 Owner NFT 的 Blocklet Server
|
|
479
|
+
const client = new Client(nodeInfo.launcher.chainHost);
|
|
480
|
+
const ownerNftDid = get(nodeInfo, 'ownerNft.did');
|
|
481
|
+
|
|
482
|
+
const { state: assetState } = await client.getAssetState({ address: ownerNftDid });
|
|
483
|
+
if (assetState.owner !== newNftOwner) {
|
|
484
|
+
const hash = await client.transfer({
|
|
485
|
+
delegator: get(nodeInfo, 'ownerNft.holder'),
|
|
486
|
+
to: newNftOwner,
|
|
487
|
+
assets: [ownerNftDid],
|
|
488
|
+
wallet: getNodeWallet(nodeInfo.sk),
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
logger.info('transferred nft', { hash, nft: ownerNftDid });
|
|
492
|
+
await node.updateNftHolder(newNftOwner);
|
|
493
|
+
logger.info('updated owner nft holder', { holder: newNftOwner, nft: ownerNftDid });
|
|
494
|
+
}
|
|
509
495
|
}
|
|
510
496
|
}
|
|
511
497
|
|
|
@@ -519,8 +505,6 @@ const handleInvitationResponse = async ({
|
|
|
519
505
|
|
|
520
506
|
const { remark } = inviteInfo;
|
|
521
507
|
|
|
522
|
-
const profile = claims.find((x) => x.type === 'profile');
|
|
523
|
-
|
|
524
508
|
const vcParams = {
|
|
525
509
|
issuerName,
|
|
526
510
|
issuerWallet,
|
|
@@ -582,6 +566,7 @@ const handleInvitationResponse = async ({
|
|
|
582
566
|
user: {
|
|
583
567
|
...profile,
|
|
584
568
|
avatar,
|
|
569
|
+
source: userSource,
|
|
585
570
|
did: userDid,
|
|
586
571
|
pk: userPk,
|
|
587
572
|
locale,
|
|
@@ -605,6 +590,7 @@ const handleInvitationResponse = async ({
|
|
|
605
590
|
teamDid,
|
|
606
591
|
user: {
|
|
607
592
|
...profile,
|
|
593
|
+
source: userSource,
|
|
608
594
|
avatar,
|
|
609
595
|
did: userDid,
|
|
610
596
|
pk: userPk,
|
|
@@ -649,6 +635,43 @@ const handleInvitationResponse = async ({
|
|
|
649
635
|
};
|
|
650
636
|
};
|
|
651
637
|
|
|
638
|
+
const handleInvitationResponse = async ({
|
|
639
|
+
req = {},
|
|
640
|
+
node,
|
|
641
|
+
nodeInfo,
|
|
642
|
+
teamDid,
|
|
643
|
+
userDid,
|
|
644
|
+
userPk,
|
|
645
|
+
inviteId,
|
|
646
|
+
locale = 'en',
|
|
647
|
+
claims,
|
|
648
|
+
statusEndpointBaseUrl,
|
|
649
|
+
endpoint,
|
|
650
|
+
newNftOwner,
|
|
651
|
+
}) => {
|
|
652
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
653
|
+
verifySignature(claim, userDid, userPk, locale);
|
|
654
|
+
|
|
655
|
+
const profile = claims.find((x) => x.type === 'profile');
|
|
656
|
+
|
|
657
|
+
const receiveResult = await handleInvitationReceive({
|
|
658
|
+
nodeInfo,
|
|
659
|
+
locale,
|
|
660
|
+
req,
|
|
661
|
+
node,
|
|
662
|
+
inviteId,
|
|
663
|
+
teamDid,
|
|
664
|
+
newNftOwner,
|
|
665
|
+
userDid,
|
|
666
|
+
userPk,
|
|
667
|
+
statusEndpointBaseUrl,
|
|
668
|
+
endpoint,
|
|
669
|
+
profile,
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
return receiveResult;
|
|
673
|
+
};
|
|
674
|
+
|
|
652
675
|
const beforeIssuePassportRequest = async ({ node, teamDid, id, locale = 'en' }) => {
|
|
653
676
|
const issuanceInfo = await node.getPassportIssuance({ teamDid, sessionId: id });
|
|
654
677
|
|
|
@@ -1049,6 +1072,7 @@ module.exports = {
|
|
|
1049
1072
|
beforeInvitationRequest,
|
|
1050
1073
|
createInvitationRequest,
|
|
1051
1074
|
handleInvitationResponse,
|
|
1075
|
+
handleInvitationReceive,
|
|
1052
1076
|
beforeIssuePassportRequest,
|
|
1053
1077
|
createIssuePassportRequest,
|
|
1054
1078
|
handleIssuePassportResponse,
|
package/lib/server.js
CHANGED
|
@@ -189,6 +189,7 @@ const authenticateByNFT = async ({ node, claims, userDid, challenge, locale, isA
|
|
|
189
189
|
if (state.owner !== ownerDid) {
|
|
190
190
|
throw new Error(messages.invalidNftHolder[locale]);
|
|
191
191
|
}
|
|
192
|
+
|
|
192
193
|
if (state.issuer !== info.launcher.did) {
|
|
193
194
|
throw new Error(messages.invalidNftIssuer[locale]);
|
|
194
195
|
}
|
|
@@ -557,6 +558,7 @@ const createLaunchBlockletHandler =
|
|
|
557
558
|
}
|
|
558
559
|
|
|
559
560
|
let blocklet;
|
|
561
|
+
let blockletWalletType;
|
|
560
562
|
if (blockletMetaUrl) {
|
|
561
563
|
blocklet = await node.getBlockletMetaFromUrl({ url: blockletMetaUrl, checkPrice: true });
|
|
562
564
|
if (!blocklet.meta) {
|
|
@@ -569,6 +571,11 @@ const createLaunchBlockletHandler =
|
|
|
569
571
|
throw new Error(messages.invalidParams[locale]);
|
|
570
572
|
}
|
|
571
573
|
}
|
|
574
|
+
|
|
575
|
+
const blockletWalletTypeEnv = (blocklet.meta.environments || []).find((x) => x.name === 'BLOCKLET_WALLET_TYPE');
|
|
576
|
+
if (blockletWalletTypeEnv) {
|
|
577
|
+
blockletWalletType = blockletWalletTypeEnv.default;
|
|
578
|
+
}
|
|
572
579
|
}
|
|
573
580
|
|
|
574
581
|
const { role, passport, user, extra, nft } = await ensureBlockletPermission({
|
|
@@ -620,7 +627,7 @@ const createLaunchBlockletHandler =
|
|
|
620
627
|
}
|
|
621
628
|
|
|
622
629
|
const appSk = toHex(keyPair.secret);
|
|
623
|
-
const appDid = getApplicationWallet(appSk).address;
|
|
630
|
+
const appDid = getApplicationWallet(appSk, undefined, blockletWalletType).address;
|
|
624
631
|
await updateSession({ appDid });
|
|
625
632
|
|
|
626
633
|
if (blocklet) {
|
|
@@ -723,6 +730,37 @@ const createRotateKeyPairHandler =
|
|
|
723
730
|
);
|
|
724
731
|
};
|
|
725
732
|
|
|
733
|
+
const createRestoreOnServerlessHandler =
|
|
734
|
+
(node, authMethod) =>
|
|
735
|
+
async ({ claims, challenge, userDid, updateSession, extraParams: { chainHost, locale } }) => {
|
|
736
|
+
const { role, extra } = await ensureBlockletPermission({
|
|
737
|
+
authMethod,
|
|
738
|
+
node,
|
|
739
|
+
userDid,
|
|
740
|
+
claims,
|
|
741
|
+
challenge,
|
|
742
|
+
locale,
|
|
743
|
+
isAuth: false,
|
|
744
|
+
chainHost,
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
const sessionToken = createBlockletControllerAuthToken({
|
|
748
|
+
did: userDid,
|
|
749
|
+
role,
|
|
750
|
+
secret,
|
|
751
|
+
controller: extra.controller,
|
|
752
|
+
expiresIn: EXTERNAL_LAUNCH_BLOCKLET_TOKEN_EXPIRE,
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
await updateSession({ sessionToken }, true);
|
|
756
|
+
|
|
757
|
+
return {
|
|
758
|
+
nextWorkflowData: {
|
|
759
|
+
controller: extra.controller,
|
|
760
|
+
},
|
|
761
|
+
};
|
|
762
|
+
};
|
|
763
|
+
|
|
726
764
|
module.exports = {
|
|
727
765
|
getAuthVcClaim,
|
|
728
766
|
getKeyPairClaim,
|
|
@@ -734,6 +772,7 @@ module.exports = {
|
|
|
734
772
|
getLaunchBlockletClaims,
|
|
735
773
|
createLaunchBlockletHandler,
|
|
736
774
|
createRotateKeyPairHandler,
|
|
775
|
+
createRestoreOnServerlessHandler,
|
|
737
776
|
ensureBlockletPermission,
|
|
738
777
|
getBlockletPermissionChecker,
|
|
739
778
|
getSetupBlockletClaims,
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.0-beta-
|
|
6
|
+
"version": "1.16.0-beta-62b42401",
|
|
7
7
|
"description": "Simple lib to manage auth in ABT Node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -20,18 +20,18 @@
|
|
|
20
20
|
"author": "linchen <linchen1987@foxmail.com> (http://github.com/linchen1987)",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@abtnode/constant": "1.16.0-beta-
|
|
24
|
-
"@abtnode/logger": "1.16.0-beta-
|
|
25
|
-
"@abtnode/util": "1.16.0-beta-
|
|
26
|
-
"@arcblock/did": "1.18.
|
|
27
|
-
"@arcblock/jwt": "^1.18.
|
|
28
|
-
"@arcblock/vc": "1.18.
|
|
29
|
-
"@blocklet/constant": "1.16.0-beta-
|
|
30
|
-
"@blocklet/meta": "1.16.0-beta-
|
|
31
|
-
"@ocap/client": "1.18.
|
|
32
|
-
"@ocap/mcrypto": "1.18.
|
|
33
|
-
"@ocap/util": "1.18.
|
|
34
|
-
"@ocap/wallet": "1.18.
|
|
23
|
+
"@abtnode/constant": "1.16.0-beta-62b42401",
|
|
24
|
+
"@abtnode/logger": "1.16.0-beta-62b42401",
|
|
25
|
+
"@abtnode/util": "1.16.0-beta-62b42401",
|
|
26
|
+
"@arcblock/did": "1.18.63",
|
|
27
|
+
"@arcblock/jwt": "^1.18.63",
|
|
28
|
+
"@arcblock/vc": "1.18.63",
|
|
29
|
+
"@blocklet/constant": "1.16.0-beta-62b42401",
|
|
30
|
+
"@blocklet/meta": "1.16.0-beta-62b42401",
|
|
31
|
+
"@ocap/client": "1.18.63",
|
|
32
|
+
"@ocap/mcrypto": "1.18.63",
|
|
33
|
+
"@ocap/util": "1.18.63",
|
|
34
|
+
"@ocap/wallet": "1.18.63",
|
|
35
35
|
"axios": "^0.27.2",
|
|
36
36
|
"joi": "17.7.0",
|
|
37
37
|
"jsonwebtoken": "^9.0.0",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"jest": "^27.5.1"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "6ef1c3601d0cfdcf5da0b55b4c54ef1fa9fce7d2"
|
|
47
47
|
}
|