@nocobase/plugin-auth 1.6.0-alpha.2 → 1.6.0-alpha.21
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/dist/client/{938.index.js → 0091d4359312cb07.js} +1 -1
- package/dist/client/88ef1c520c08b674.js +10 -0
- package/dist/client/{851.index.js → 974ac9de644a1d1f.js} +1 -1
- package/dist/client/{280.index.js → 9e603524b127e7b4.js} +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/interceptors.d.ts +13 -0
- package/dist/client/settings/token-policy/components.d.ts +15 -0
- package/dist/client/settings/token-policy/hooks.d.ts +24 -0
- package/dist/client/settings/token-policy/index.d.ts +10 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.js +45 -0
- package/dist/externalVersion.js +11 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -2
- package/dist/locale/en-US.json +18 -1
- package/dist/locale/zh-CN.json +17 -1
- package/dist/node_modules/cron/package.json +1 -1
- package/dist/node_modules/ms/index.js +1 -0
- package/dist/node_modules/ms/package.json +1 -0
- package/dist/server/actions/auth.js +6 -0
- package/dist/server/basic-auth.js +4 -1
- package/dist/server/collections/authenticators.js +1 -0
- package/dist/server/collections/issued-tokens.d.ts +10 -0
- package/dist/server/collections/issued-tokens.js +70 -0
- package/dist/server/collections/token-blacklist.js +1 -0
- package/dist/server/collections/token-poilcy-config.d.ts +10 -0
- package/dist/server/collections/token-poilcy-config.js +57 -0
- package/dist/server/collections/users-authenticators.js +1 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +8 -2
- package/dist/server/migrations/20241229080941-create-token-policy-config.d.ts +14 -0
- package/dist/server/migrations/20241229080941-create-token-policy-config.js +58 -0
- package/dist/server/plugin.js +107 -54
- package/dist/server/storer.d.ts +8 -2
- package/dist/server/storer.js +28 -3
- package/dist/server/token-blacklist.js +13 -2
- package/dist/server/token-controller.d.ts +40 -0
- package/dist/server/token-controller.js +161 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +24 -0
- package/package.json +5 -3
- /package/dist/client/{890.index.js → 43e9587ca4936ffe.js} +0 -0
|
@@ -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-07T02:39:49.988Z"}
|
|
@@ -45,6 +45,12 @@ var auth_default = {
|
|
|
45
45
|
// await next();
|
|
46
46
|
// },
|
|
47
47
|
changePassword: async (ctx, next) => {
|
|
48
|
+
const systemSettings = ctx.db.getRepository("systemSettings");
|
|
49
|
+
const settings = await systemSettings.findOne();
|
|
50
|
+
const enableChangePassword = settings.get("enableChangePassword");
|
|
51
|
+
if (enableChangePassword === false) {
|
|
52
|
+
ctx.throw(403, ctx.t("Password is not allowed to be changed", { ns: import_preset.namespace }));
|
|
53
|
+
}
|
|
48
54
|
const {
|
|
49
55
|
values: { oldPassword, newPassword, confirmPassword }
|
|
50
56
|
} = ctx.action.params;
|
|
@@ -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
|
}
|
|
@@ -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
|
+
});
|
|
@@ -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
|
+
});
|
package/dist/server/index.d.ts
CHANGED
package/dist/server/index.js
CHANGED
|
@@ -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
|
+
}
|
package/dist/server/plugin.js
CHANGED
|
@@ -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
|
|
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
|
-
["
|
|
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`,
|
|
@@ -127,11 +151,49 @@ class PluginAuthServer extends import_server.Plugin {
|
|
|
127
151
|
this.app.on("cache:del:auth", async ({ userId }) => {
|
|
128
152
|
await this.cache.del(`auth:${userId}`);
|
|
129
153
|
});
|
|
154
|
+
this.app.on("ws:message:auth:token", async ({ clientId, payload }) => {
|
|
155
|
+
if (!payload || !payload.token || !payload.authenticator) {
|
|
156
|
+
this.app.emit(`ws:removeTag`, {
|
|
157
|
+
clientId,
|
|
158
|
+
tagKey: "userId"
|
|
159
|
+
});
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const auth = await this.app.authManager.get(payload.authenticator, {
|
|
163
|
+
getBearerToken: () => payload.token,
|
|
164
|
+
app: this.app,
|
|
165
|
+
db: this.app.db,
|
|
166
|
+
cache: this.app.cache,
|
|
167
|
+
logger: this.app.logger
|
|
168
|
+
});
|
|
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
|
+
}
|
|
181
|
+
}
|
|
182
|
+
this.app.emit(`ws:setTag`, {
|
|
183
|
+
clientId,
|
|
184
|
+
tagKey: "userId",
|
|
185
|
+
tagValue: user.id
|
|
186
|
+
});
|
|
187
|
+
this.app.emit(`ws:authorized`, {
|
|
188
|
+
clientId,
|
|
189
|
+
userId: user.id
|
|
190
|
+
});
|
|
191
|
+
});
|
|
130
192
|
this.app.auditManager.registerActions([
|
|
131
193
|
{
|
|
132
194
|
name: "auth:signIn",
|
|
133
195
|
getMetaData: async (ctx) => {
|
|
134
|
-
var _a
|
|
196
|
+
var _a;
|
|
135
197
|
let body = {};
|
|
136
198
|
if (ctx.status === 200) {
|
|
137
199
|
body = {
|
|
@@ -145,20 +207,10 @@ class PluginAuthServer extends import_server.Plugin {
|
|
|
145
207
|
}
|
|
146
208
|
return {
|
|
147
209
|
request: {
|
|
148
|
-
params: (_a = ctx.request) == null ? void 0 : _a.params,
|
|
149
210
|
body: {
|
|
150
|
-
...(
|
|
211
|
+
...(_a = ctx.request) == null ? void 0 : _a.body,
|
|
151
212
|
password: void 0
|
|
152
|
-
},
|
|
153
|
-
path: (_c = ctx.request) == null ? void 0 : _c.path,
|
|
154
|
-
headers: {
|
|
155
|
-
"x-authenticator": (_d = ctx.request) == null ? void 0 : _d.headers["x-authenticator"],
|
|
156
|
-
"x-locale": (_e = ctx.request) == null ? void 0 : _e.headers["x-locale"],
|
|
157
|
-
"x-timezone": (_f = ctx.request) == null ? void 0 : _f.headers["x-timezone"]
|
|
158
213
|
}
|
|
159
|
-
},
|
|
160
|
-
response: {
|
|
161
|
-
body
|
|
162
214
|
}
|
|
163
215
|
};
|
|
164
216
|
},
|
|
@@ -193,26 +245,13 @@ class PluginAuthServer extends import_server.Plugin {
|
|
|
193
245
|
{
|
|
194
246
|
name: "auth:signUp",
|
|
195
247
|
getMetaData: async (ctx) => {
|
|
196
|
-
var _a
|
|
248
|
+
var _a;
|
|
197
249
|
return {
|
|
198
250
|
request: {
|
|
199
|
-
params: (_a = ctx.request) == null ? void 0 : _a.params,
|
|
200
251
|
body: {
|
|
201
|
-
...(
|
|
252
|
+
...(_a = ctx.request) == null ? void 0 : _a.body,
|
|
202
253
|
password: void 0,
|
|
203
254
|
confirm_password: void 0
|
|
204
|
-
},
|
|
205
|
-
path: (_c = ctx.request) == null ? void 0 : _c.path,
|
|
206
|
-
headers: {
|
|
207
|
-
"x-authenticator": (_d = ctx.request) == null ? void 0 : _d.headers["x-authenticator"],
|
|
208
|
-
"x-locale": (_e = ctx.request) == null ? void 0 : _e.headers["x-locale"],
|
|
209
|
-
"x-timezone": (_f = ctx.request) == null ? void 0 : _f.headers["x-timezone"]
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
response: {
|
|
213
|
-
body: {
|
|
214
|
-
...(_g = ctx.response) == null ? void 0 : _g.body,
|
|
215
|
-
token: void 0
|
|
216
255
|
}
|
|
217
256
|
}
|
|
218
257
|
};
|
|
@@ -221,18 +260,9 @@ class PluginAuthServer extends import_server.Plugin {
|
|
|
221
260
|
{
|
|
222
261
|
name: "auth:changePassword",
|
|
223
262
|
getMetaData: async (ctx) => {
|
|
224
|
-
var _a, _b, _c;
|
|
225
263
|
return {
|
|
226
264
|
request: {
|
|
227
|
-
|
|
228
|
-
query: ctx.request.query,
|
|
229
|
-
body: {},
|
|
230
|
-
path: ctx.request.path,
|
|
231
|
-
headers: {
|
|
232
|
-
"x-authenticator": (_a = ctx.request) == null ? void 0 : _a.headers["x-authenticator"],
|
|
233
|
-
"x-locale": (_b = ctx.request) == null ? void 0 : _b.headers["x-locale"],
|
|
234
|
-
"x-timezone": (_c = ctx.request) == null ? void 0 : _c.headers["x-timezone"]
|
|
235
|
-
}
|
|
265
|
+
body: {}
|
|
236
266
|
},
|
|
237
267
|
response: {
|
|
238
268
|
body: {}
|
|
@@ -248,24 +278,47 @@ class PluginAuthServer extends import_server.Plugin {
|
|
|
248
278
|
},
|
|
249
279
|
"auth:signOut"
|
|
250
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
|
+
});
|
|
251
289
|
}
|
|
252
290
|
async install(options) {
|
|
253
|
-
const
|
|
254
|
-
const exist = await
|
|
255
|
-
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) {
|
|
256
311
|
return;
|
|
257
312
|
}
|
|
258
|
-
|
|
313
|
+
const config = {
|
|
314
|
+
tokenExpirationTime: "1d",
|
|
315
|
+
sessionExpirationTime: "7d",
|
|
316
|
+
expiredTokenRenewLimit: "1d"
|
|
317
|
+
};
|
|
318
|
+
await tokenPolicyRepo.create({
|
|
259
319
|
values: {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
description: "Sign in with username/email.",
|
|
263
|
-
enabled: true,
|
|
264
|
-
options: {
|
|
265
|
-
public: {
|
|
266
|
-
allowSignUp: true
|
|
267
|
-
}
|
|
268
|
-
}
|
|
320
|
+
key: import_constants.tokenPolicyRecordKey,
|
|
321
|
+
config
|
|
269
322
|
}
|
|
270
323
|
});
|
|
271
324
|
}
|
package/dist/server/storer.d.ts
CHANGED
|
@@ -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>;
|
package/dist/server/storer.js
CHANGED
|
@@ -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({
|
|
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];
|