@nocobase/plugin-auth 1.6.0-beta.2 → 1.6.0-beta.4

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 (41) hide show
  1. package/dist/client/{0994a09f2c3ca18d.js → 0091d4359312cb07.js} +1 -1
  2. package/dist/client/88ef1c520c08b674.js +10 -0
  3. package/dist/client/{1c5e32ff8bef5e4a.js → 974ac9de644a1d1f.js} +1 -1
  4. package/dist/client/{6992b2fd9b0275b1.js → 9e603524b127e7b4.js} +1 -1
  5. package/dist/client/index.d.ts +1 -1
  6. package/dist/client/index.js +1 -1
  7. package/dist/client/interceptors.d.ts +13 -0
  8. package/dist/client/settings/token-policy/components.d.ts +15 -0
  9. package/dist/client/settings/token-policy/hooks.d.ts +24 -0
  10. package/dist/client/settings/token-policy/index.d.ts +10 -0
  11. package/dist/constants.d.ts +12 -0
  12. package/dist/constants.js +45 -0
  13. package/dist/externalVersion.js +11 -8
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.js +4 -2
  16. package/dist/locale/en-US.json +17 -1
  17. package/dist/locale/zh-CN.json +16 -1
  18. package/dist/node_modules/cron/package.json +1 -1
  19. package/dist/node_modules/ms/index.js +1 -0
  20. package/dist/node_modules/ms/package.json +1 -0
  21. package/dist/server/basic-auth.js +4 -1
  22. package/dist/server/collections/authenticators.js +1 -0
  23. package/dist/server/collections/issued-tokens.d.ts +10 -0
  24. package/dist/server/collections/issued-tokens.js +70 -0
  25. package/dist/server/collections/token-blacklist.js +1 -0
  26. package/dist/server/collections/token-poilcy-config.d.ts +10 -0
  27. package/dist/server/collections/token-poilcy-config.js +57 -0
  28. package/dist/server/collections/users-authenticators.js +1 -0
  29. package/dist/server/index.d.ts +2 -0
  30. package/dist/server/index.js +8 -2
  31. package/dist/server/migrations/20241229080941-create-token-policy-config.d.ts +14 -0
  32. package/dist/server/migrations/20241229080941-create-token-policy-config.js +58 -0
  33. package/dist/server/plugin.js +76 -25
  34. package/dist/server/storer.d.ts +8 -2
  35. package/dist/server/storer.js +28 -3
  36. package/dist/server/token-blacklist.js +13 -2
  37. package/dist/server/token-controller.d.ts +40 -0
  38. package/dist/server/token-controller.js +161 -0
  39. package/dist/types.d.ts +9 -0
  40. package/dist/types.js +24 -0
  41. package/package.json +5 -3
@@ -0,0 +1 @@
1
+ {"name":"ms","version":"2.1.3","description":"Tiny millisecond conversion utility","repository":"vercel/ms","main":"./index","files":["index.js"],"scripts":{"precommit":"lint-staged","lint":"eslint lib/* bin/*","test":"mocha tests.js"},"eslintConfig":{"extends":"eslint:recommended","env":{"node":true,"es6":true}},"lint-staged":{"*.js":["npm run lint","prettier --single-quote --write","git add"]},"license":"MIT","devDependencies":{"eslint":"4.18.2","expect.js":"0.3.1","husky":"0.14.3","lint-staged":"5.0.0","mocha":"4.0.1","prettier":"2.0.5"},"_lastModified":"2025-02-11T04:45:10.179Z"}
@@ -72,7 +72,10 @@ class BasicAuth extends import_auth.BaseAuth {
72
72
  const field = this.userCollection.getField("password");
73
73
  const valid = await field.verify(password, user.password);
74
74
  if (!valid) {
75
- ctx.throw(401, ctx.t("The username/email or password is incorrect, please re-enter", { ns: import_preset.namespace }));
75
+ ctx.throw(401, ctx.t("The username/email or password is incorrect, please re-enter", { ns: import_preset.namespace }), {
76
+ code: "INCORRECT_PASSWORD",
77
+ user
78
+ });
76
79
  }
77
80
  return user;
78
81
  }
@@ -34,6 +34,7 @@ var authenticators_default = (0, import_database.defineCollection)({
34
34
  dumpRules: {
35
35
  group: "third-party"
36
36
  },
37
+ migrationRules: ["overwrite", "schema-only"],
37
38
  shared: true,
38
39
  name: "authenticators",
39
40
  sortable: true,
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ declare const _default: import("@nocobase/database").CollectionOptions;
10
+ export default _default;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var issued_tokens_exports = {};
28
+ __export(issued_tokens_exports, {
29
+ default: () => issued_tokens_default
30
+ });
31
+ module.exports = __toCommonJS(issued_tokens_exports);
32
+ var import_database = require("@nocobase/database");
33
+ var import_constants = require("../../constants");
34
+ var issued_tokens_default = (0, import_database.defineCollection)({
35
+ name: import_constants.issuedTokensCollectionName,
36
+ migrationRules: ["schema-only"],
37
+ autoGenId: false,
38
+ createdAt: true,
39
+ updatedAt: true,
40
+ fields: [
41
+ {
42
+ name: "id",
43
+ type: "uuid",
44
+ primaryKey: true,
45
+ allowNull: false,
46
+ interface: "input"
47
+ },
48
+ {
49
+ type: "bigInt",
50
+ name: "signInTime",
51
+ allowNull: false
52
+ },
53
+ {
54
+ name: "jti",
55
+ type: "uuid",
56
+ allowNull: false,
57
+ index: true
58
+ },
59
+ {
60
+ type: "bigInt",
61
+ name: "issuedTime",
62
+ allowNull: false
63
+ },
64
+ {
65
+ type: "bigInt",
66
+ name: "userId",
67
+ allowNull: false
68
+ }
69
+ ]
70
+ });
@@ -34,6 +34,7 @@ var token_blacklist_default = (0, import_database.defineCollection)({
34
34
  dumpRules: {
35
35
  group: "log"
36
36
  },
37
+ migrationRules: ["schema-only"],
37
38
  shared: true,
38
39
  name: "tokenBlacklist",
39
40
  model: "TokenBlacklistModel",
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ declare const _default: import("@nocobase/database").CollectionOptions;
10
+ export default _default;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var token_poilcy_config_exports = {};
28
+ __export(token_poilcy_config_exports, {
29
+ default: () => token_poilcy_config_default
30
+ });
31
+ module.exports = __toCommonJS(token_poilcy_config_exports);
32
+ var import_database = require("@nocobase/database");
33
+ var import_constants = require("../../constants");
34
+ var token_poilcy_config_default = (0, import_database.defineCollection)({
35
+ name: import_constants.tokenPolicyCollectionName,
36
+ migrationRules: ["overwrite", "schema-only"],
37
+ autoGenId: false,
38
+ createdAt: true,
39
+ createdBy: true,
40
+ updatedAt: true,
41
+ updatedBy: true,
42
+ fields: [
43
+ {
44
+ name: "key",
45
+ type: "string",
46
+ primaryKey: true,
47
+ allowNull: false,
48
+ interface: "input"
49
+ },
50
+ {
51
+ type: "json",
52
+ name: "config",
53
+ allowNull: false,
54
+ defaultValue: {}
55
+ }
56
+ ]
57
+ });
@@ -35,6 +35,7 @@ var users_authenticators_default = (0, import_database.defineCollection)({
35
35
  group: "user"
36
36
  },
37
37
  shared: true,
38
+ migrationRules: ["schema-only", "overwrite"],
38
39
  name: "usersAuthenticators",
39
40
  model: "UserAuthModel",
40
41
  createdBy: true,
@@ -8,4 +8,6 @@
8
8
  */
9
9
  export { BasicAuth } from './basic-auth';
10
10
  export { AuthModel } from './model/authenticator';
11
+ export { presetAuthType } from '../preset';
11
12
  export { default } from './plugin';
13
+ export * from '../constants';
@@ -25,6 +25,7 @@ var __copyProps = (to, from, except, desc) => {
25
25
  }
26
26
  return to;
27
27
  };
28
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
28
29
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
30
  // If the importer is in node compatibility mode or this is not an ESM
30
31
  // file that has been converted to a CommonJS file using a Babel-
@@ -38,14 +39,19 @@ var server_exports = {};
38
39
  __export(server_exports, {
39
40
  AuthModel: () => import_authenticator.AuthModel,
40
41
  BasicAuth: () => import_basic_auth.BasicAuth,
41
- default: () => import_plugin.default
42
+ default: () => import_plugin.default,
43
+ presetAuthType: () => import_preset.presetAuthType
42
44
  });
43
45
  module.exports = __toCommonJS(server_exports);
44
46
  var import_basic_auth = require("./basic-auth");
45
47
  var import_authenticator = require("./model/authenticator");
48
+ var import_preset = require("../preset");
46
49
  var import_plugin = __toESM(require("./plugin"));
50
+ __reExport(server_exports, require("../constants"), module.exports);
47
51
  // Annotate the CommonJS export names for ESM import in node:
48
52
  0 && (module.exports = {
49
53
  AuthModel,
50
- BasicAuth
54
+ BasicAuth,
55
+ presetAuthType,
56
+ ...require("../constants")
51
57
  });
@@ -0,0 +1,14 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Migration } from '@nocobase/server';
10
+ export default class extends Migration {
11
+ on: string;
12
+ appVersion: string;
13
+ up(): Promise<void>;
14
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var create_token_policy_config_exports = {};
28
+ __export(create_token_policy_config_exports, {
29
+ default: () => create_token_policy_config_default
30
+ });
31
+ module.exports = __toCommonJS(create_token_policy_config_exports);
32
+ var import_server = require("@nocobase/server");
33
+ var import_constants = require("../../constants");
34
+ class create_token_policy_config_default extends import_server.Migration {
35
+ on = "afterLoad";
36
+ // 'beforeLoad' or 'afterLoad'
37
+ appVersion = "<1.6.1";
38
+ async up() {
39
+ const tokenPolicyRepo = this.app.db.getRepository(import_constants.tokenPolicyCollectionName);
40
+ const tokenPolicy = await tokenPolicyRepo.findOne({ filterByTk: import_constants.tokenPolicyRecordKey });
41
+ if (tokenPolicy) {
42
+ this.app.authManager.tokenController.setConfig(tokenPolicy.config);
43
+ } else {
44
+ const config = {
45
+ tokenExpirationTime: "1d",
46
+ sessionExpirationTime: "7d",
47
+ expiredTokenRenewLimit: "1d"
48
+ };
49
+ await tokenPolicyRepo.create({
50
+ values: {
51
+ key: import_constants.tokenPolicyRecordKey,
52
+ config
53
+ }
54
+ });
55
+ this.app.authManager.tokenController.setConfig(config);
56
+ }
57
+ }
58
+ }
@@ -41,6 +41,7 @@ __export(plugin_exports, {
41
41
  });
42
42
  module.exports = __toCommonJS(plugin_exports);
43
43
  var import_server = require("@nocobase/server");
44
+ var import_utils = require("@nocobase/utils");
44
45
  var import_preset = require("../preset");
45
46
  var import_auth = __toESM(require("./actions/auth"));
46
47
  var import_authenticators = __toESM(require("./actions/authenticators"));
@@ -48,10 +49,31 @@ var import_basic_auth = require("./basic-auth");
48
49
  var import_authenticator = require("./model/authenticator");
49
50
  var import_storer = require("./storer");
50
51
  var import_token_blacklist = require("./token-blacklist");
51
- var import_utils = require("@nocobase/utils");
52
+ var import_token_controller = require("./token-controller");
53
+ var import_constants = require("../constants");
52
54
  class PluginAuthServer extends import_server.Plugin {
53
55
  cache;
54
56
  afterAdd() {
57
+ this.app.on("afterLoad", async () => {
58
+ if (this.app.authManager.tokenController) {
59
+ return;
60
+ }
61
+ const cache = await this.app.cacheManager.createCache({
62
+ name: "auth-token-controller",
63
+ prefix: "auth-token-controller"
64
+ });
65
+ const tokenController = new import_token_controller.TokenController({ cache, app: this.app, logger: this.app.log });
66
+ this.app.authManager.setTokenControlService(tokenController);
67
+ const tokenPolicyRepo = this.app.db.getRepository(import_constants.tokenPolicyCollectionName);
68
+ try {
69
+ const res = await tokenPolicyRepo.findOne({ filterByTk: import_constants.tokenPolicyRecordKey });
70
+ if (res) {
71
+ this.app.authManager.tokenController.setConfig(res.config);
72
+ }
73
+ } catch (error) {
74
+ this.app.logger.warn("access control config not exist, use default value");
75
+ }
76
+ });
55
77
  }
56
78
  async beforeLoad() {
57
79
  this.app.db.registerModels({ AuthModel: import_authenticator.AuthModel });
@@ -63,8 +85,10 @@ class PluginAuthServer extends import_server.Plugin {
63
85
  store: "memory"
64
86
  });
65
87
  const storer = new import_storer.Storer({
88
+ app: this.app,
66
89
  db: this.db,
67
- cache: this.cache
90
+ cache: this.cache,
91
+ authManager: this.app.authManager
68
92
  });
69
93
  this.app.authManager.setStorer(storer);
70
94
  if (!this.app.authManager.jwt.blacklist) {
@@ -109,8 +133,8 @@ class PluginAuthServer extends import_server.Plugin {
109
133
  Object.entries(import_authenticators.default).forEach(
110
134
  ([action, handler]) => this.app.resourceManager.registerActionHandler(`authenticators:${action}`, handler)
111
135
  );
112
- ["check", "signIn", "signUp"].forEach((action) => this.app.acl.allow("auth", action));
113
- ["signOut", "changePassword"].forEach((action) => this.app.acl.allow("auth", action, "loggedIn"));
136
+ ["signIn", "signUp"].forEach((action) => this.app.acl.allow("auth", action));
137
+ ["check", "signOut", "changePassword"].forEach((action) => this.app.acl.allow("auth", action, "loggedIn"));
114
138
  this.app.acl.allow("authenticators", "publicList");
115
139
  this.app.acl.registerSnippet({
116
140
  name: `pm.${this.name}.authenticators`,
@@ -142,14 +166,18 @@ class PluginAuthServer extends import_server.Plugin {
142
166
  cache: this.app.cache,
143
167
  logger: this.app.logger
144
168
  });
145
- const user = await auth.check();
146
- if (!user) {
147
- this.app.logger.error(`Invalid token: ${payload.token}`);
148
- this.app.emit(`ws:removeTag`, {
149
- clientId,
150
- tagKey: "userId"
151
- });
152
- return;
169
+ let user;
170
+ try {
171
+ user = await auth.check();
172
+ } catch (error) {
173
+ if (!user) {
174
+ this.app.logger.error(error);
175
+ this.app.emit(`ws:removeTag`, {
176
+ clientId,
177
+ tagKey: "userId"
178
+ });
179
+ return;
180
+ }
153
181
  }
154
182
  this.app.emit(`ws:setTag`, {
155
183
  clientId,
@@ -250,24 +278,47 @@ class PluginAuthServer extends import_server.Plugin {
250
278
  },
251
279
  "auth:signOut"
252
280
  ]);
281
+ this.app.acl.registerSnippet({
282
+ name: `pm.security.token-policy`,
283
+ actions: [`${import_constants.tokenPolicyCollectionName}:*`]
284
+ });
285
+ this.app.db.on(`${import_constants.tokenPolicyCollectionName}.afterSave`, async (model) => {
286
+ var _a;
287
+ (_a = this.app.authManager.tokenController) == null ? void 0 : _a.setConfig(model.config);
288
+ });
253
289
  }
254
290
  async install(options) {
255
- const repository = this.db.getRepository("authenticators");
256
- const exist = await repository.findOne({ filter: { name: import_preset.presetAuthenticator } });
257
- if (exist) {
291
+ const authRepository = this.db.getRepository("authenticators");
292
+ const exist = await authRepository.findOne({ filter: { name: import_preset.presetAuthenticator } });
293
+ if (!exist) {
294
+ await authRepository.create({
295
+ values: {
296
+ name: import_preset.presetAuthenticator,
297
+ authType: import_preset.presetAuthType,
298
+ description: "Sign in with username/email.",
299
+ enabled: true,
300
+ options: {
301
+ public: {
302
+ allowSignUp: true
303
+ }
304
+ }
305
+ }
306
+ });
307
+ }
308
+ const tokenPolicyRepo = this.app.db.getRepository(import_constants.tokenPolicyCollectionName);
309
+ const res = await tokenPolicyRepo.findOne({ filterByTk: import_constants.tokenPolicyRecordKey });
310
+ if (res) {
258
311
  return;
259
312
  }
260
- await repository.create({
313
+ const config = {
314
+ tokenExpirationTime: "1d",
315
+ sessionExpirationTime: "7d",
316
+ expiredTokenRenewLimit: "1d"
317
+ };
318
+ await tokenPolicyRepo.create({
261
319
  values: {
262
- name: import_preset.presetAuthenticator,
263
- authType: import_preset.presetAuthType,
264
- description: "Sign in with username/email.",
265
- enabled: true,
266
- options: {
267
- public: {
268
- allowSignUp: true
269
- }
270
- }
320
+ key: import_constants.tokenPolicyRecordKey,
321
+ config
271
322
  }
272
323
  });
273
324
  }
@@ -6,18 +6,24 @@
6
6
  * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
- import { Storer as IStorer } from '@nocobase/auth';
9
+ import { AuthManager, Storer as IStorer } from '@nocobase/auth';
10
10
  import { Cache } from '@nocobase/cache';
11
11
  import { Database } from '@nocobase/database';
12
+ import { Application } from '@nocobase/server';
12
13
  import { AuthModel } from './model/authenticator';
13
14
  export declare class Storer implements IStorer {
14
15
  db: Database;
15
16
  cache: Cache;
17
+ app: Application;
18
+ authManager: AuthManager;
16
19
  key: string;
17
- constructor({ db, cache }: {
20
+ constructor({ app, db, cache, authManager, }: {
21
+ app?: Application;
18
22
  db: Database;
19
23
  cache: Cache;
24
+ authManager: AuthManager;
20
25
  });
26
+ renderJsonTemplate(authenticator: any): any;
21
27
  getCache(): Promise<AuthModel[]>;
22
28
  setCache(authenticators: AuthModel[]): Promise<void>;
23
29
  get(name: string): Promise<AuthModel>;
@@ -32,21 +32,45 @@ module.exports = __toCommonJS(storer_exports);
32
32
  class Storer {
33
33
  db;
34
34
  cache;
35
+ app;
36
+ authManager;
35
37
  key = "authenticators";
36
- constructor({ db, cache }) {
38
+ constructor({
39
+ app,
40
+ db,
41
+ cache,
42
+ authManager
43
+ }) {
44
+ this.app = app;
37
45
  this.db = db;
38
46
  this.cache = cache;
47
+ this.authManager = authManager;
39
48
  this.db.on("authenticators.afterSave", async (model) => {
40
49
  if (!model.enabled) {
41
50
  await this.cache.delValueInObject(this.key, model.name);
42
51
  return;
43
52
  }
44
- await this.cache.setValueInObject(this.key, model.name, model);
53
+ await this.cache.setValueInObject(this.key, model.name, this.renderJsonTemplate(model));
45
54
  });
46
55
  this.db.on("authenticators.afterDestroy", async (model) => {
47
56
  await this.cache.delValueInObject(this.key, model.name);
48
57
  });
49
58
  }
59
+ renderJsonTemplate(authenticator) {
60
+ var _a, _b;
61
+ if (!authenticator) {
62
+ return authenticator;
63
+ }
64
+ const $env = (_a = this.app) == null ? void 0 : _a.environment;
65
+ if (!$env) {
66
+ return authenticator;
67
+ }
68
+ const config = this.authManager.getAuthConfig(authenticator.authType);
69
+ authenticator.dataValues.options = $env.renderJsonTemplate(authenticator.dataValues.options, {
70
+ omit: (_b = config == null ? void 0 : config.auth) == null ? void 0 : _b["optionsKeysNotAllowedInEnv"]
71
+ });
72
+ return authenticator;
73
+ }
50
74
  async getCache() {
51
75
  const authenticators = await this.cache.get(this.key);
52
76
  if (!authenticators) {
@@ -56,7 +80,7 @@ class Storer {
56
80
  }
57
81
  async setCache(authenticators) {
58
82
  const obj = authenticators.reduce((obj2, authenticator) => {
59
- obj2[authenticator.name] = authenticator;
83
+ obj2[authenticator.name] = this.renderJsonTemplate(authenticator);
60
84
  return obj2;
61
85
  }, {});
62
86
  await this.cache.set(this.key, obj);
@@ -67,6 +91,7 @@ class Storer {
67
91
  const repo = this.db.getRepository("authenticators");
68
92
  authenticators = await repo.find({ filter: { enabled: true } });
69
93
  await this.setCache(authenticators);
94
+ authenticators = await this.getCache();
70
95
  }
71
96
  const authenticator = authenticators.find((authenticator2) => authenticator2.name === name);
72
97
  return authenticator || authenticators[0];
@@ -37,11 +37,22 @@ class TokenBlacklistService {
37
37
  try {
38
38
  this.bloomFilter = await plugin.app.cacheManager.createBloomFilter();
39
39
  await this.bloomFilter.reserve(this.cacheKey, 1e-3, 1e6);
40
- const data = await this.repo.find({ fields: ["token"], raw: true });
40
+ const data = await this.repo.find({
41
+ fields: ["token"],
42
+ filter: {
43
+ expiration: {
44
+ $dateAfter: /* @__PURE__ */ new Date()
45
+ }
46
+ },
47
+ raw: true
48
+ });
41
49
  const tokens = data.map((item) => item.token);
50
+ if (!tokens.length) {
51
+ return;
52
+ }
42
53
  await this.bloomFilter.mAdd(this.cacheKey, tokens);
43
54
  } catch (error) {
44
- plugin.app.logger.error("token-blacklist: create bloom filter failed", error);
55
+ plugin.app.logger.warn("token-blacklist: create bloom filter failed", error);
45
56
  this.bloomFilter = null;
46
57
  }
47
58
  });
@@ -0,0 +1,40 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { ITokenControlService, TokenPolicyConfig, NumericTokenPolicyConfig, TokenInfo } from '@nocobase/auth';
10
+ import type { SystemLogger } from '@nocobase/logger';
11
+ import { Cache } from '@nocobase/cache';
12
+ import Application from '@nocobase/server';
13
+ import Database from '@nocobase/database';
14
+ type TokenControlService = ITokenControlService;
15
+ export declare class TokenController implements TokenControlService {
16
+ cache: Cache;
17
+ app: Application;
18
+ db: Database;
19
+ logger: SystemLogger;
20
+ constructor({ cache, app, logger }: {
21
+ cache: Cache;
22
+ app: Application;
23
+ logger: SystemLogger;
24
+ });
25
+ setTokenInfo(id: string, value: TokenInfo): Promise<void>;
26
+ getConfig(): Promise<NumericTokenPolicyConfig>;
27
+ setConfig(config: TokenPolicyConfig): Promise<void>;
28
+ removeSessionExpiredTokens(userId: number): Promise<any>;
29
+ add({ userId }: {
30
+ userId: number;
31
+ }): Promise<{
32
+ jti: `${string}-${string}-${string}-${string}-${string}`;
33
+ issuedTime: number;
34
+ signInTime: number;
35
+ renewed: boolean;
36
+ userId: number;
37
+ }>;
38
+ renew: TokenControlService['renew'];
39
+ }
40
+ export {};