@blocklet/sdk 1.16.32-beta-5463e017 → 1.16.32-beta-93e1a798

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TNavigation, TTheme } from '@blocklet/meta/lib/types';
1
+ import { TTheme, type TNavigationItem } from '@blocklet/meta/lib/types';
2
2
  import env from '@blocklet/env';
3
3
  import Auth from './service/auth';
4
4
  import Notification from './service/notification';
@@ -28,34 +28,65 @@ export { Component as component };
28
28
  export { Component };
29
29
  export { Security };
30
30
  export { config };
31
+ export type BlockletSettings = {
32
+ session: {
33
+ ttl: number;
34
+ cacheTtl: number;
35
+ };
36
+ federated: {
37
+ master: {
38
+ appId: string;
39
+ appPid: string;
40
+ appName: string;
41
+ appDescription: string;
42
+ appUrl: string;
43
+ appLogo: string;
44
+ version: string;
45
+ };
46
+ config: Record<string, any>;
47
+ };
48
+ oauth: Record<string, {
49
+ enabled: boolean;
50
+ [x: string]: any;
51
+ }>;
52
+ };
31
53
  export interface WindowBlocklet {
32
54
  [x: string]: any;
55
+ serverDid: string;
56
+ serverVersion: string;
57
+ did: string;
33
58
  appId: string;
34
59
  appIds: string[];
35
60
  appPid: string;
61
+ appPk: string;
36
62
  appName: string;
37
63
  appDescription: string;
38
64
  appLogo: string;
39
65
  appLogoRect: string;
40
66
  appUrl: string;
67
+ webWalletUrl: string;
41
68
  isComponent: boolean;
42
69
  prefix: string;
43
70
  groupPrefix: string;
44
71
  pageGroup: string;
45
72
  version: string;
46
73
  mode: string;
74
+ status: string;
47
75
  tenantMode: 'single' | 'multiple';
48
76
  theme: TTheme;
49
- navigation: TNavigation[];
77
+ navigation: TNavigationItem[];
50
78
  preferences: Record<string, any>;
51
79
  languages: {
52
80
  code: string;
53
81
  name: string;
54
82
  }[];
55
83
  passportColor: string;
84
+ componentId: string;
56
85
  componentMountPoints: TComponent[];
57
86
  alsoKnownAs: string[];
58
87
  trustedFactories: string[];
88
+ updatedAt: number;
89
+ settings: BlockletSettings;
59
90
  }
60
91
  declare global {
61
92
  interface Window {
@@ -0,0 +1,14 @@
1
+ import type { Request, RequestHandler, Response } from 'express';
2
+ export interface CSRFOptionsResponse extends Response<any, {
3
+ generateToken: typeof defaultGenerateToken;
4
+ verifyToken: typeof defaultVerifyToken;
5
+ } & Record<string, any>> {
6
+ }
7
+ export interface CSRFOptions {
8
+ generateToken?: (req: Request, res: CSRFOptionsResponse) => void | Promise<void>;
9
+ verifyToken?: (req: Request, res: CSRFOptionsResponse) => Promise<void> | void;
10
+ }
11
+ declare function defaultGenerateToken(req: Request, res: Response): void;
12
+ declare function defaultVerifyToken(req: Request): void;
13
+ export declare function csrf(options?: CSRFOptions): RequestHandler;
14
+ export {};
@@ -0,0 +1,64 @@
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.csrf = csrf;
7
+ const lodash_1 = require("lodash");
8
+ const joi_1 = __importDefault(require("joi"));
9
+ const digest_1 = require("../util/digest");
10
+ const wallet_1 = require("../util/wallet");
11
+ function defaultGenerateToken(req, res) {
12
+ if (req.cookies.login_token && !req.cookies['x-csrf-token']) {
13
+ const xCsrfTokenMd5 = (0, digest_1.hmac)(req.cookies.login_token);
14
+ const xCsrfTokenSigned = (0, digest_1.hmac)(xCsrfTokenMd5);
15
+ res.cookie('x-csrf-token', [xCsrfTokenMd5, xCsrfTokenSigned].join('.'), {
16
+ sameSite: 'none',
17
+ secure: true,
18
+ });
19
+ }
20
+ }
21
+ function defaultVerifyToken(req) {
22
+ if (!(0, lodash_1.isEmpty)(req.cookies['x-csrf-token']) && req.cookies['x-csrf-token'] === req.headers['x-csrf-token']) {
23
+ const [xCsrfTokenMd5, xCsrfTokenSigned] = req.headers['x-csrf-token'].split('.');
24
+ if ((0, digest_1.hmac)(xCsrfTokenMd5) === xCsrfTokenSigned) {
25
+ return;
26
+ }
27
+ }
28
+ throw new Error('Current request status is abnormal, please retry later');
29
+ }
30
+ function shouldGenerateToken(req) {
31
+ return ['GET'].includes(req.method);
32
+ }
33
+ /**
34
+ *
35
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
36
+ */
37
+ function shouldVerifyToken(req) {
38
+ return (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method) &&
39
+ !(0, lodash_1.isEmpty)(req.cookies['x-csrf-token']) &&
40
+ !(0, wallet_1.isDidWalletConnect)(req.headers));
41
+ }
42
+ const csrfOptionsSchema = joi_1.default.object({
43
+ generateToken: joi_1.default.func().required(),
44
+ verifyToken: joi_1.default.func().required(),
45
+ });
46
+ function csrf(options = { generateToken: defaultGenerateToken, verifyToken: defaultVerifyToken }) {
47
+ options.generateToken = typeof options.generateToken === 'function' ? options.generateToken : defaultGenerateToken;
48
+ options.verifyToken = typeof options.verifyToken === 'function' ? options.verifyToken : defaultVerifyToken;
49
+ const { value: data, error } = csrfOptionsSchema.validate(options);
50
+ if (error) {
51
+ throw new Error(error.message);
52
+ }
53
+ return async (req, res, next) => {
54
+ res.locals.generateToken = defaultGenerateToken;
55
+ res.locals.verifyToken = defaultVerifyToken;
56
+ if (shouldGenerateToken(req)) {
57
+ await data.generateToken(req, res);
58
+ }
59
+ else if (shouldVerifyToken(req)) {
60
+ await data.verifyToken(req, res);
61
+ }
62
+ return next();
63
+ };
64
+ }
@@ -3,11 +3,13 @@ import auth from './auth';
3
3
  import component from './component';
4
4
  import fallback from './fallback';
5
5
  import sitemap from './sitemap';
6
+ import { csrf } from './csrf';
6
7
  export { user };
7
8
  export { auth };
8
9
  export { component };
9
10
  export { fallback };
10
11
  export { sitemap };
12
+ export { csrf };
11
13
  declare const _default: {
12
14
  user: () => (req: import("express").Request & {
13
15
  user?: {
@@ -72,5 +74,6 @@ declare const _default: {
72
74
  stock_tickers: string;
73
75
  };
74
76
  }) => void, req?: import("express").Request) => Promise<void>) => (req: import("express").Request, res: import("express").Response) => Promise<void>;
77
+ csrf: typeof csrf;
75
78
  };
76
79
  export default _default;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sitemap = exports.fallback = exports.component = exports.auth = exports.user = void 0;
6
+ exports.csrf = exports.sitemap = exports.fallback = exports.component = exports.auth = exports.user = void 0;
7
7
  const user_1 = __importDefault(require("./user"));
8
8
  exports.user = user_1.default;
9
9
  const auth_1 = __importDefault(require("./auth"));
@@ -14,10 +14,13 @@ const fallback_1 = __importDefault(require("./fallback"));
14
14
  exports.fallback = fallback_1.default;
15
15
  const sitemap_1 = __importDefault(require("./sitemap"));
16
16
  exports.sitemap = sitemap_1.default;
17
+ const csrf_1 = require("./csrf");
18
+ Object.defineProperty(exports, "csrf", { enumerable: true, get: function () { return csrf_1.csrf; } });
17
19
  exports.default = {
18
20
  user: user_1.default,
19
21
  auth: auth_1.default,
20
22
  component: component_1.default,
21
23
  fallback: fallback_1.default,
22
24
  sitemap: sitemap_1.default,
25
+ csrf: csrf_1.csrf,
23
26
  };
@@ -0,0 +1,2 @@
1
+ import type { LiteralUnion } from 'type-fest';
2
+ export declare function hmac(message: string, algorithm?: LiteralUnion<'md5' | 'sha256', string>): string;
@@ -0,0 +1,13 @@
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.hmac = hmac;
7
+ const crypto_1 = require("crypto");
8
+ const wallet_1 = __importDefault(require("../wallet"));
9
+ function hmac(message, algorithm = 'md5') {
10
+ const wallet = (0, wallet_1.default)();
11
+ const hmacFunc = (0, crypto_1.createHmac)(algorithm, wallet.secretKey);
12
+ return hmacFunc.update(message).digest('base64url');
13
+ }
@@ -0,0 +1,7 @@
1
+ import type { Request } from 'express';
2
+ /**
3
+ *
4
+ * @description 判断请求是否来自钱包的 DID Connect
5
+ * @export
6
+ */
7
+ export declare function isDidWalletConnect(headers: Request['headers']): boolean;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDidWalletConnect = isDidWalletConnect;
4
+ /**
5
+ *
6
+ * @description 判断请求是否来自钱包的 DID Connect
7
+ * @export
8
+ */
9
+ function isDidWalletConnect(headers) {
10
+ const userAgent = (headers['user-agent'] || '').toLowerCase();
11
+ const isMatch = userAgent.split(/\s+/).find((x) => x.startsWith('arcwallet/') || x.startsWith('abtwallet/'));
12
+ if (isMatch) {
13
+ return true;
14
+ }
15
+ const arcWalletVersion = headers['arcwallet-version'];
16
+ if (arcWalletVersion) {
17
+ return true;
18
+ }
19
+ return false;
20
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.32-beta-5463e017",
6
+ "version": "1.16.32-beta-93e1a798",
7
7
  "description": "graphql client to read/write data on abt node",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
@@ -27,15 +27,15 @@
27
27
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
28
28
  "license": "Apache-2.0",
29
29
  "dependencies": {
30
- "@abtnode/client": "1.16.32-beta-5463e017",
31
- "@abtnode/constant": "1.16.32-beta-5463e017",
30
+ "@abtnode/client": "1.16.32-beta-93e1a798",
31
+ "@abtnode/constant": "1.16.32-beta-93e1a798",
32
32
  "@arcblock/did": "1.18.135",
33
33
  "@arcblock/did-auth": "1.18.135",
34
34
  "@arcblock/jwt": "1.18.135",
35
35
  "@arcblock/ws": "1.18.135",
36
- "@blocklet/constant": "1.16.32-beta-5463e017",
37
- "@blocklet/env": "1.16.32-beta-5463e017",
38
- "@blocklet/meta": "1.16.32-beta-5463e017",
36
+ "@blocklet/constant": "1.16.32-beta-93e1a798",
37
+ "@blocklet/env": "1.16.32-beta-93e1a798",
38
+ "@blocklet/meta": "1.16.32-beta-93e1a798",
39
39
  "@did-connect/authenticator": "^2.2.4",
40
40
  "@did-connect/handler": "^2.2.4",
41
41
  "@nedb/core": "^2.1.5",
@@ -75,5 +75,5 @@
75
75
  "ts-node": "^10.9.1",
76
76
  "typescript": "^5.0.4"
77
77
  },
78
- "gitHead": "aff2d89d663ccd842757ff664fb80e1d7ae05029"
78
+ "gitHead": "608fab914e6ee5cb5659c11ab3d0713945ab99a0"
79
79
  }