@abtnode/auth 1.7.26 → 1.7.27

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.
Files changed (2) hide show
  1. package/lib/server.js +37 -62
  2. package/package.json +7 -6
package/lib/server.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const get = require('lodash/get');
2
+ const isEmpty = require('lodash/isEmpty');
2
3
  const last = require('lodash/last');
3
4
  const Client = require('@ocap/client');
4
5
  const { fromPublicKey } = require('@ocap/wallet');
@@ -10,9 +11,9 @@ const {
10
11
  ROLES,
11
12
  VC_TYPE_GENERAL_PASSPORT,
12
13
  VC_TYPE_NODE_PASSPORT,
13
- VC_TYPE_BLOCKLET_PURCHASE,
14
14
  NFT_TYPE_SERVER_OWNERSHIP,
15
15
  } = require('@abtnode/constant');
16
+ const { sign } = require('@arcblock/jwt');
16
17
  const {
17
18
  messages,
18
19
  getVCFromClaims,
@@ -29,13 +30,11 @@ const {
29
30
  getRoleFromExternalPassport,
30
31
  createUserPassport,
31
32
  } = require('./passport');
32
-
33
33
  const logger = require('./logger');
34
34
 
35
35
  const secret = process.env.ABT_NODE_SESSION_SECRET;
36
36
  const LAUNCH_BLOCKLET_TOKEN_EXPIRE = '1d';
37
37
  const abtnodeVcTypes = [VC_TYPE_GENERAL_PASSPORT, VC_TYPE_NODE_PASSPORT];
38
- const blockletVcTypes = [VC_TYPE_BLOCKLET_PURCHASE];
39
38
 
40
39
  const authenticateByVc = async ({ node, locale, userDid, claims, challenge, requireNodeInitialized = true }) => {
41
40
  if (requireNodeInitialized) {
@@ -184,7 +183,7 @@ const getAuthVcClaim =
184
183
  return claim;
185
184
  };
186
185
 
187
- const getLaunchFreeBlockletClaims = (node, authMethod) => {
186
+ const getLaunchBlockletClaims = (node, authMethod) => {
188
187
  if (authMethod === 'vc') {
189
188
  return {
190
189
  serverPassport: ['verifiableCredential', getAuthVcClaim(node)],
@@ -202,31 +201,6 @@ const getLaunchFreeBlockletClaims = (node, authMethod) => {
202
201
  };
203
202
  };
204
203
 
205
- const getLaunchPaidBlockletClaims = (node, authMethod) => {
206
- const claims = getLaunchFreeBlockletClaims(node, authMethod);
207
-
208
- claims.blockletPurchaseNft = [
209
- 'verifiableCredential',
210
- async ({ extraParams: { locale, blockletMetaUrl }, context: { didwallet } }) => {
211
- checkWalletVersion({ didwallet, locale });
212
- const registryUrl = new URL(blockletMetaUrl).origin;
213
- const [registry, { meta }] = await Promise.all([
214
- node.getRegistryMeta(registryUrl),
215
- node.getBlockletMetaFromUrl({ url: blockletMetaUrl }),
216
- ]);
217
-
218
- return {
219
- description: messages.requestBlockletNft[locale],
220
- item: blockletVcTypes,
221
- trustedIssuers: [registry.id],
222
- tag: meta.did,
223
- };
224
- },
225
- ];
226
-
227
- return claims;
228
- };
229
-
230
204
  const getOwnershipNFTClaim = async (node, locale) => {
231
205
  const info = await node.getNodeInfo();
232
206
  if (!info.ownerNft && !info.ownerNft.issuer) {
@@ -281,8 +255,12 @@ const ensureBlockletPermission = async ({ authMethod, node, userDid, claims, cha
281
255
 
282
256
  const createLaunchBlockletHandler =
283
257
  (node, authMethod) =>
284
- async ({ claims, challenge, userDid, updateSession, req, extraParams: { locale, blockletMetaUrl } }) => {
258
+ async ({ claims, challenge, userDid, updateSession, req, extraParams }) => {
259
+ const { locale, blockletMetaUrl } = extraParams;
260
+ logger.info('createLaunchBlockletHandler', extraParams);
261
+
285
262
  if (!blockletMetaUrl) {
263
+ logger.error('blockletMetaUrl must be provided');
286
264
  throw new Error(messages.invalidParams[locale]);
287
265
  }
288
266
 
@@ -295,37 +273,32 @@ const createLaunchBlockletHandler =
295
273
  locale,
296
274
  });
297
275
 
298
- const result = await node.getBlockletMetaFromUrl({ url: blockletMetaUrl, checkPrice: true });
299
- if (!result.meta) {
276
+ const blocklet = await node.getBlockletMetaFromUrl({ url: blockletMetaUrl, checkPrice: true });
277
+ if (!blocklet.meta) {
300
278
  throw new Error(messages.invalidBlocklet[locale]);
301
279
  }
302
280
 
303
- const { did } = result.meta;
304
-
305
- let blockletPurchaseVerified;
306
- if (!result.isFree) {
307
- const registryUrl = new URL(blockletMetaUrl).origin;
308
- const registryMeta = await node.getRegistryMeta(registryUrl);
309
-
310
- const { vc: blockletVc } = await getVCFromClaims({
311
- claims,
312
- challenge,
313
- trustedIssuers: [registryMeta.id],
314
- vcTypes: blockletVcTypes,
315
- locale,
316
- });
317
-
318
- if (!blockletVc) {
319
- throw new Error(messages.missingBlockletCredentialClaim[locale]);
320
- }
281
+ const info = await node.getNodeInfo();
282
+ const headers = {};
321
283
 
322
- if (get(blockletVc, 'credentialSubject.purchased.blocklet.id') !== did) {
323
- throw new Error(messages.invalidBlockletVc[locale]);
284
+ if (!blocklet.isFree) {
285
+ if (isEmpty(extraParams?.previousWorkflowData?.downloadToken)) {
286
+ logger.error('downloadToken must be provided');
287
+ throw new Error(messages.invalidParams[locale]);
324
288
  }
325
289
 
326
- blockletPurchaseVerified = true;
290
+ Object.assign(headers, {
291
+ 'x-server-did': info.did,
292
+ 'x-download-token': extraParams.previousWorkflowData.downloadToken,
293
+ 'x-server-publick-key': info.pk,
294
+ 'x-server-signature': sign(info.did, info.sk, {
295
+ exp: (Date.now() + 5 * 60 * 1000) / 1000,
296
+ }),
297
+ });
327
298
  }
328
299
 
300
+ const { did } = blocklet.meta;
301
+
329
302
  let sessionToken = '';
330
303
  if (authMethod === 'vc') {
331
304
  sessionToken = createAuthToken({
@@ -351,13 +324,13 @@ const createLaunchBlockletHandler =
351
324
  if (existedBlocklet) {
352
325
  const storageData = { did: userDid };
353
326
 
354
- if (semver.gt(result.meta.version, existedBlocklet.meta.version)) {
327
+ if (semver.gt(blocklet.meta.version, existedBlocklet.meta.version)) {
355
328
  const appDidEnv = existedBlocklet.environments.find((e) => e.key === 'BLOCKLET_APP_ID');
356
329
  storageData.upgradeAvailable = {
357
330
  appDid: appDidEnv ? appDidEnv.value : '',
358
331
  did: existedBlocklet.meta.did,
359
332
  currentVersion: existedBlocklet.meta.version,
360
- version: result.meta.version,
333
+ version: blocklet.meta.version,
361
334
  };
362
335
  }
363
336
 
@@ -366,12 +339,15 @@ const createLaunchBlockletHandler =
366
339
  return;
367
340
  }
368
341
 
369
- const context = {};
370
- if (typeof blockletPurchaseVerified !== 'undefined') {
371
- context.blockletPurchaseVerified = blockletPurchaseVerified;
372
- }
342
+ const tmp = await node.installBlocklet(
343
+ {
344
+ url: blockletMetaUrl,
345
+ },
346
+ {
347
+ headers,
348
+ }
349
+ );
373
350
 
374
- const tmp = await node.installBlocklet({ url: blockletMetaUrl }, context);
375
351
  await node.createAuditLog(
376
352
  {
377
353
  action: 'installBlocklet',
@@ -389,8 +365,7 @@ module.exports = {
389
365
  authenticateByVc,
390
366
  authenticateByNFT,
391
367
  getOwnershipNFTClaim,
392
- getLaunchFreeBlockletClaims,
393
- getLaunchPaidBlockletClaims,
368
+ getLaunchBlockletClaims,
394
369
  createLaunchBlockletHandler,
395
370
  ensureBlockletPermission,
396
371
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.7.26",
6
+ "version": "1.7.27",
7
7
  "description": "Simple lib to manage auth in ABT Node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -20,12 +20,13 @@
20
20
  "author": "linchen <linchen1987@foxmail.com> (http://github.com/linchen1987)",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@abtnode/constant": "1.7.26",
24
- "@abtnode/logger": "1.7.26",
25
- "@abtnode/util": "1.7.26",
23
+ "@abtnode/constant": "1.7.27",
24
+ "@abtnode/logger": "1.7.27",
25
+ "@abtnode/util": "1.7.27",
26
26
  "@arcblock/did": "1.17.0",
27
+ "@arcblock/jwt": "^1.17.0",
27
28
  "@arcblock/vc": "1.17.0",
28
- "@blocklet/meta": "1.7.26",
29
+ "@blocklet/meta": "1.7.27",
29
30
  "@ocap/client": "1.17.0",
30
31
  "@ocap/mcrypto": "1.17.0",
31
32
  "@ocap/util": "1.17.0",
@@ -40,5 +41,5 @@
40
41
  "devDependencies": {
41
42
  "jest": "^27.4.5"
42
43
  },
43
- "gitHead": "b7ef9b4ddb18f7a0c3898177fe06d9cefe966566"
44
+ "gitHead": "81a5492df66389b0aede13f033d1e493450833bc"
44
45
  }