@nocobase/plugin-auth 0.10.0-alpha.2
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/README.md +117 -0
- package/client.d.ts +3 -0
- package/client.js +65 -0
- package/lib/client/basic/Options.d.ts +2 -0
- package/lib/client/basic/Options.js +51 -0
- package/lib/client/basic/SigninPage.d.ts +6 -0
- package/lib/client/basic/SigninPage.js +100 -0
- package/lib/client/basic/SignupPage.d.ts +5 -0
- package/lib/client/basic/SignupPage.js +131 -0
- package/lib/client/index.d.ts +3 -0
- package/lib/client/index.js +56 -0
- package/lib/client/locale/index.d.ts +2 -0
- package/lib/client/locale/index.js +19 -0
- package/lib/client/locale/zh-CN.d.ts +9 -0
- package/lib/client/locale/zh-CN.js +16 -0
- package/lib/client/settings/Authenticator.d.ts +2 -0
- package/lib/client/settings/Authenticator.js +159 -0
- package/lib/client/settings/Options.d.ts +3 -0
- package/lib/client/settings/Options.js +56 -0
- package/lib/client/settings/authType.d.ts +16 -0
- package/lib/client/settings/authType.js +27 -0
- package/lib/client/settings/schemas/authenticators.d.ts +3 -0
- package/lib/client/settings/schemas/authenticators.js +438 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +20 -0
- package/lib/preset.d.ts +3 -0
- package/lib/preset.js +12 -0
- package/lib/server/actions/auth.d.ts +8 -0
- package/lib/server/actions/auth.js +51 -0
- package/lib/server/actions/authenticators.d.ts +8 -0
- package/lib/server/actions/authenticators.js +131 -0
- package/lib/server/basic-auth.d.ts +10 -0
- package/lib/server/basic-auth.js +183 -0
- package/lib/server/collections/authenticators.d.ts +6 -0
- package/lib/server/collections/authenticators.js +93 -0
- package/lib/server/collections/users-authenticators.d.ts +7 -0
- package/lib/server/collections/users-authenticators.js +75 -0
- package/lib/server/index.d.ts +2 -0
- package/lib/server/index.js +20 -0
- package/lib/server/locale/en-US.d.ts +9 -0
- package/lib/server/locale/en-US.js +15 -0
- package/lib/server/locale/index.d.ts +3 -0
- package/lib/server/locale/index.js +27 -0
- package/lib/server/locale/ja-JP.d.ts +5 -0
- package/lib/server/locale/ja-JP.js +11 -0
- package/lib/server/locale/pt-BR.d.ts +9 -0
- package/lib/server/locale/pt-BR.js +15 -0
- package/lib/server/locale/zh-CN.d.ts +10 -0
- package/lib/server/locale/zh-CN.js +16 -0
- package/lib/server/migrations/20230506152253-basic-authenticator.d.ts +5 -0
- package/lib/server/migrations/20230506152253-basic-authenticator.js +40 -0
- package/lib/server/migrations/20230607174500-update-basic.d.ts +5 -0
- package/lib/server/migrations/20230607174500-update-basic.js +43 -0
- package/lib/server/model/authenticator.d.ts +6 -0
- package/lib/server/model/authenticator.js +72 -0
- package/lib/server/plugin.d.ts +11 -0
- package/lib/server/plugin.js +130 -0
- package/package.json +17 -0
- package/server.d.ts +3 -0
- package/server.js +65 -0
- package/src/client/basic/Options.tsx +31 -0
- package/src/client/basic/SigninPage.tsx +65 -0
- package/src/client/basic/SignupPage.tsx +91 -0
- package/src/client/index.tsx +41 -0
- package/src/client/locale/index.ts +7 -0
- package/src/client/locale/zh-CN.ts +10 -0
- package/src/client/settings/Authenticator.tsx +95 -0
- package/src/client/settings/Options.tsx +35 -0
- package/src/client/settings/authType.ts +18 -0
- package/src/client/settings/schemas/authenticators.ts +402 -0
- package/src/index.ts +1 -0
- package/src/preset.ts +4 -0
- package/src/server/__tests__/actions.test.ts +142 -0
- package/src/server/actions/auth.ts +20 -0
- package/src/server/actions/authenticators.ts +85 -0
- package/src/server/basic-auth.ts +128 -0
- package/src/server/collections/.gitkeep +0 -0
- package/src/server/collections/authenticators.ts +97 -0
- package/src/server/collections/users-authenticators.ts +73 -0
- package/src/server/index.ts +2 -0
- package/src/server/locale/en-US.ts +10 -0
- package/src/server/locale/index.ts +3 -0
- package/src/server/locale/ja-JP.ts +4 -0
- package/src/server/locale/pt-BR.ts +10 -0
- package/src/server/locale/zh-CN.ts +9 -0
- package/src/server/migrations/20230506152253-basic-authenticator.ts +22 -0
- package/src/server/migrations/20230607174500-update-basic.ts +25 -0
- package/src/server/model/authenticator.ts +48 -0
- package/src/server/plugin.ts +92 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { AuthConfig, BaseAuth } from '@nocobase/auth';
|
|
2
|
+
import { namespace } from '../preset';
|
|
3
|
+
import { PasswordField } from '@nocobase/database';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
|
|
6
|
+
export class BasicAuth extends BaseAuth {
|
|
7
|
+
constructor(config: AuthConfig) {
|
|
8
|
+
const userCollection = config.ctx.db.getCollection('users');
|
|
9
|
+
super({ ...config, userCollection });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async validate() {
|
|
13
|
+
const ctx = this.ctx;
|
|
14
|
+
const { uniqueField = 'email', values } = ctx.action.params;
|
|
15
|
+
|
|
16
|
+
if (!values[uniqueField]) {
|
|
17
|
+
ctx.throw(400, ctx.t('Please fill in your email address', { ns: namespace }));
|
|
18
|
+
}
|
|
19
|
+
const user = await this.userCollection.repository.findOne({
|
|
20
|
+
where: {
|
|
21
|
+
[uniqueField]: values[uniqueField],
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (!user) {
|
|
26
|
+
ctx.throw(401, ctx.t('The email is incorrect, please re-enter', { ns: namespace }));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const field = this.userCollection.getField<PasswordField>('password');
|
|
30
|
+
const valid = await field.verify(values.password, user.password);
|
|
31
|
+
if (!valid) {
|
|
32
|
+
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
|
|
33
|
+
}
|
|
34
|
+
return user;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async signUp() {
|
|
38
|
+
const ctx = this.ctx;
|
|
39
|
+
const options = this.authenticator.options?.public || {};
|
|
40
|
+
if (!options.allowSignUp) {
|
|
41
|
+
ctx.throw(403, ctx.t('Not allowed to sign up', { ns: namespace }));
|
|
42
|
+
}
|
|
43
|
+
const User = ctx.db.getRepository('users');
|
|
44
|
+
const { values } = ctx.action.params;
|
|
45
|
+
const user = await User.create({ values });
|
|
46
|
+
return user;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async lostPassword() {
|
|
50
|
+
const ctx = this.ctx;
|
|
51
|
+
const {
|
|
52
|
+
values: { email },
|
|
53
|
+
} = ctx.action.params;
|
|
54
|
+
if (!email) {
|
|
55
|
+
ctx.throw(400, ctx.t('Please fill in your email address', { ns: namespace }));
|
|
56
|
+
}
|
|
57
|
+
const user = await this.userCollection.repository.findOne({
|
|
58
|
+
where: {
|
|
59
|
+
email,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
if (!user) {
|
|
63
|
+
ctx.throw(401, ctx.t('The email is incorrect, please re-enter', { ns: namespace }));
|
|
64
|
+
}
|
|
65
|
+
user.resetToken = crypto.randomBytes(20).toString('hex');
|
|
66
|
+
await user.save();
|
|
67
|
+
return user;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async resetPassword() {
|
|
71
|
+
const ctx = this.ctx;
|
|
72
|
+
const {
|
|
73
|
+
values: { email, password, resetToken },
|
|
74
|
+
} = ctx.action.params;
|
|
75
|
+
const user = await this.userCollection.repository.findOne({
|
|
76
|
+
where: {
|
|
77
|
+
email,
|
|
78
|
+
resetToken,
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
if (!user) {
|
|
82
|
+
ctx.throw(404);
|
|
83
|
+
}
|
|
84
|
+
user.token = null;
|
|
85
|
+
user.resetToken = null;
|
|
86
|
+
user.password = password;
|
|
87
|
+
await user.save();
|
|
88
|
+
return user;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async getUserByResetToken() {
|
|
92
|
+
const ctx = this.ctx;
|
|
93
|
+
const { token } = ctx.action.params;
|
|
94
|
+
const user = await this.userCollection.repository.findOne({
|
|
95
|
+
where: {
|
|
96
|
+
resetToken: token,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
if (!user) {
|
|
100
|
+
ctx.throw(401);
|
|
101
|
+
}
|
|
102
|
+
return user;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async changePassword() {
|
|
106
|
+
const ctx = this.ctx;
|
|
107
|
+
const {
|
|
108
|
+
values: { oldPassword, newPassword },
|
|
109
|
+
} = ctx.action.params;
|
|
110
|
+
const currentUser = ctx.auth.user;
|
|
111
|
+
if (!currentUser) {
|
|
112
|
+
ctx.throw(401);
|
|
113
|
+
}
|
|
114
|
+
const user = await this.userCollection.repository.findOne({
|
|
115
|
+
where: {
|
|
116
|
+
email: currentUser.email,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
const pwd = this.userCollection.getField<PasswordField>('password');
|
|
120
|
+
const isValid = await pwd.verify(oldPassword, user.password);
|
|
121
|
+
if (!isValid) {
|
|
122
|
+
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
|
|
123
|
+
}
|
|
124
|
+
user.password = newPassword;
|
|
125
|
+
await user.save();
|
|
126
|
+
return currentUser;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection for extended authentication methods,
|
|
5
|
+
*/
|
|
6
|
+
export default {
|
|
7
|
+
namespace: 'auth.auth',
|
|
8
|
+
duplicator: 'optional',
|
|
9
|
+
name: 'authenticators',
|
|
10
|
+
sortable: true,
|
|
11
|
+
title: '{{t("Authenticators")}}',
|
|
12
|
+
model: 'AuthModel',
|
|
13
|
+
createdBy: true,
|
|
14
|
+
updatedBy: true,
|
|
15
|
+
logging: true,
|
|
16
|
+
fields: [
|
|
17
|
+
{
|
|
18
|
+
name: 'id',
|
|
19
|
+
type: 'bigInt',
|
|
20
|
+
autoIncrement: true,
|
|
21
|
+
primaryKey: true,
|
|
22
|
+
allowNull: false,
|
|
23
|
+
interface: 'id',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
interface: 'input',
|
|
27
|
+
type: 'string',
|
|
28
|
+
name: 'name',
|
|
29
|
+
allowNull: false,
|
|
30
|
+
unique: true,
|
|
31
|
+
uiSchema: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
title: '{{t("Name")}}',
|
|
34
|
+
'x-component': 'Input',
|
|
35
|
+
required: true,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
interface: 'input',
|
|
40
|
+
type: 'string',
|
|
41
|
+
name: 'authType',
|
|
42
|
+
allowNull: false,
|
|
43
|
+
uiSchema: {
|
|
44
|
+
type: 'string',
|
|
45
|
+
title: '{{t("Auth Type")}}',
|
|
46
|
+
'x-component': 'Input',
|
|
47
|
+
required: true,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
interface: 'input',
|
|
52
|
+
type: 'string',
|
|
53
|
+
name: 'title',
|
|
54
|
+
uiSchema: {
|
|
55
|
+
type: 'string',
|
|
56
|
+
title: '{{t("Title")}}',
|
|
57
|
+
'x-component': 'Input',
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
interface: 'textarea',
|
|
62
|
+
type: 'string',
|
|
63
|
+
name: 'description',
|
|
64
|
+
allowNull: false,
|
|
65
|
+
defaultValue: '',
|
|
66
|
+
uiSchema: {
|
|
67
|
+
type: 'string',
|
|
68
|
+
title: '{{t("Description")}}',
|
|
69
|
+
'x-component': 'Input',
|
|
70
|
+
required: true,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: 'json',
|
|
75
|
+
name: 'options',
|
|
76
|
+
allowNull: false,
|
|
77
|
+
defaultValue: {},
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
type: 'boolean',
|
|
81
|
+
name: 'enabled',
|
|
82
|
+
defaultValue: false,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
interface: 'm2m',
|
|
86
|
+
type: 'belongsToMany',
|
|
87
|
+
name: 'users',
|
|
88
|
+
target: 'users',
|
|
89
|
+
foreignKey: 'authenticator',
|
|
90
|
+
otherKey: 'userId',
|
|
91
|
+
onDelete: 'CASCADE',
|
|
92
|
+
sourceKey: 'name',
|
|
93
|
+
targetKey: 'id',
|
|
94
|
+
through: 'usersAuthenticators',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
} as CollectionOptions;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection for user information of extended authentication methods,
|
|
5
|
+
* such as saml, oicd, oauth, sms, etc.
|
|
6
|
+
*/
|
|
7
|
+
export default {
|
|
8
|
+
namespace: 'auth.auth',
|
|
9
|
+
duplicator: {
|
|
10
|
+
dumpable: 'optional',
|
|
11
|
+
/**
|
|
12
|
+
* When dump this collection, the users collection is required to be dumped.
|
|
13
|
+
*/
|
|
14
|
+
with: 'users',
|
|
15
|
+
},
|
|
16
|
+
name: 'usersAuthenticators',
|
|
17
|
+
title: '{{t("Users Authenticators")}}',
|
|
18
|
+
model: 'UserAuthModel',
|
|
19
|
+
createdBy: true,
|
|
20
|
+
updatedBy: true,
|
|
21
|
+
logging: true,
|
|
22
|
+
fields: [
|
|
23
|
+
/**
|
|
24
|
+
* uuid:
|
|
25
|
+
* Unique user id of the authentication method, such as wechat openid, phone number, etc.
|
|
26
|
+
*/
|
|
27
|
+
{
|
|
28
|
+
name: 'uuid',
|
|
29
|
+
interface: 'input',
|
|
30
|
+
type: 'string',
|
|
31
|
+
allowNull: false,
|
|
32
|
+
uiSchema: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
title: '{{t("UUID")}}',
|
|
35
|
+
'x-component': 'Input',
|
|
36
|
+
required: true,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
interface: 'input',
|
|
41
|
+
type: 'string',
|
|
42
|
+
name: 'nickname',
|
|
43
|
+
allowNull: false,
|
|
44
|
+
defaultValue: '',
|
|
45
|
+
uiSchema: {
|
|
46
|
+
type: 'string',
|
|
47
|
+
title: '{{t("Nickname")}}',
|
|
48
|
+
'x-component': 'Input',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
interface: 'attachment',
|
|
53
|
+
type: 'string',
|
|
54
|
+
name: 'avatar',
|
|
55
|
+
allowNull: false,
|
|
56
|
+
defaultValue: '',
|
|
57
|
+
uiSchema: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
title: '{{t("Avatar")}}',
|
|
60
|
+
'x-component': 'Upload',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* meta:
|
|
65
|
+
* Metadata, some other information of the authentication method.
|
|
66
|
+
*/
|
|
67
|
+
{
|
|
68
|
+
type: 'json',
|
|
69
|
+
name: 'meta',
|
|
70
|
+
defaultValue: {},
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
} as CollectionOptions;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
'The email is incorrect, please re-enter': 'The email is incorrect, please re-enter',
|
|
3
|
+
'Please fill in your email address': 'Please fill in your email address',
|
|
4
|
+
'The password is incorrect, please re-enter': 'The password is incorrect, please re-enter',
|
|
5
|
+
'Not a valid cellphone number, please re-enter': 'Not a valid cellphone number, please re-enter',
|
|
6
|
+
'The phone number has been registered, please login directly':
|
|
7
|
+
'The phone number has been registered, please login directly',
|
|
8
|
+
'The phone number is not registered, please register first':
|
|
9
|
+
'The phone number is not registered, please register first',
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
'The email is incorrect, please re-enter': 'O e-mail está incorreto, por favor, digite novamente',
|
|
3
|
+
'Please fill in your email address': 'Por favor, preencha o seu endereço de e-mail',
|
|
4
|
+
'The password is incorrect, please re-enter': 'A senha está incorreta, por favor, digite novamente',
|
|
5
|
+
'Not a valid cellphone number, please re-enter': 'Número de celular inválido, por favor, digite novamente',
|
|
6
|
+
'The phone number has been registered, please login directly':
|
|
7
|
+
'O número de celular já está registrado, por favor, faça login diretamente',
|
|
8
|
+
'The phone number is not registered, please register first':
|
|
9
|
+
'O número de celular não está registrado, por favor, registre-se primeiro',
|
|
10
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
'The email is incorrect, please re-enter': '邮箱有误,请重新输入',
|
|
3
|
+
'Please fill in your email address': '请填写邮箱',
|
|
4
|
+
'The password is incorrect, please re-enter': '密码有误,请重新输入',
|
|
5
|
+
'Not a valid cellphone number, please re-enter': '不是有效的手机号,请重新输入',
|
|
6
|
+
'The phone number has been registered, please login directly': '手机号已注册,请直接登录',
|
|
7
|
+
'The phone number is not registered, please register first': '手机号未注册,请先注册',
|
|
8
|
+
'Please keep and enable at least one authenticator': '请至少保留并启用一个认证器',
|
|
9
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Migration } from '@nocobase/server';
|
|
2
|
+
import { presetAuthType, presetAuthenticator } from '../../preset';
|
|
3
|
+
|
|
4
|
+
export default class AddBasicAuthMigration extends Migration {
|
|
5
|
+
async up() {
|
|
6
|
+
const repo = this.context.db.getRepository('authenticators');
|
|
7
|
+
const existed = await repo.count();
|
|
8
|
+
if (existed) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
await repo.create({
|
|
12
|
+
values: {
|
|
13
|
+
name: presetAuthenticator,
|
|
14
|
+
authType: presetAuthType,
|
|
15
|
+
description: 'Sign in with email and password.',
|
|
16
|
+
enabled: true,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async down() {}
|
|
22
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Migration } from '@nocobase/server';
|
|
2
|
+
import { presetAuthenticator } from '../../preset';
|
|
3
|
+
|
|
4
|
+
export default class UpdateBasicAuthMigration extends Migration {
|
|
5
|
+
async up() {
|
|
6
|
+
const SystemSetting = this.context.db.getRepository('systemSettings');
|
|
7
|
+
const setting = await SystemSetting.findOne();
|
|
8
|
+
const allowSignUp = setting.get('allowSignUp') ? true : false;
|
|
9
|
+
const repo = this.context.db.getRepository('authenticators');
|
|
10
|
+
await repo.update({
|
|
11
|
+
values: {
|
|
12
|
+
options: {
|
|
13
|
+
public: {
|
|
14
|
+
allowSignUp,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
filter: {
|
|
19
|
+
name: presetAuthenticator,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async down() {}
|
|
25
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Database, Model } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export class AuthModel extends Model {
|
|
4
|
+
async findUser(uuid: string) {
|
|
5
|
+
let user: Model;
|
|
6
|
+
const users = await this.getUsers({
|
|
7
|
+
through: {
|
|
8
|
+
where: { uuid },
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
if (users.length) {
|
|
12
|
+
user = users[0];
|
|
13
|
+
return user;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async newUser(uuid: string, values?: any) {
|
|
18
|
+
let user: Model;
|
|
19
|
+
const db: Database = (this.constructor as any).database;
|
|
20
|
+
await this.sequelize.transaction(async (transaction) => {
|
|
21
|
+
// Create a new user if not exists
|
|
22
|
+
user = await this.createUser(
|
|
23
|
+
values || {
|
|
24
|
+
nickname: uuid,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
through: {
|
|
28
|
+
uuid: uuid,
|
|
29
|
+
},
|
|
30
|
+
transaction,
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
await db.emitAsync(`users.afterCreateWithAssociations`, user, {
|
|
34
|
+
transaction,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return user;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async findOrCreateUser(uuid: string, userValues?: any) {
|
|
41
|
+
const user = await this.findUser(uuid);
|
|
42
|
+
if (user) {
|
|
43
|
+
return user;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return await this.newUser(uuid, userValues);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { BasicAuth } from './basic-auth';
|
|
4
|
+
import { presetAuthType, presetAuthenticator } from '../preset';
|
|
5
|
+
import authActions from './actions/auth';
|
|
6
|
+
import authenticatorsActions from './actions/authenticators';
|
|
7
|
+
import { enUS, zhCN } from './locale';
|
|
8
|
+
import { namespace } from '../preset';
|
|
9
|
+
import { AuthModel } from './model/authenticator';
|
|
10
|
+
import { Model } from '@nocobase/database';
|
|
11
|
+
|
|
12
|
+
export class AuthPlugin extends Plugin {
|
|
13
|
+
afterAdd() {}
|
|
14
|
+
|
|
15
|
+
async beforeLoad() {
|
|
16
|
+
this.app.i18n.addResources('zh-CN', namespace, zhCN);
|
|
17
|
+
this.app.i18n.addResources('en-US', namespace, enUS);
|
|
18
|
+
|
|
19
|
+
this.app.db.registerModels({ AuthModel });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async load() {
|
|
23
|
+
// Set up database
|
|
24
|
+
await this.db.import({
|
|
25
|
+
directory: resolve(__dirname, 'collections'),
|
|
26
|
+
});
|
|
27
|
+
this.db.addMigrations({
|
|
28
|
+
namespace: 'auth',
|
|
29
|
+
directory: resolve(__dirname, 'migrations'),
|
|
30
|
+
context: {
|
|
31
|
+
plugin: this,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
// Set up auth manager and register preset auth type
|
|
35
|
+
this.app.authManager.setStorer({
|
|
36
|
+
get: async (name: string) => {
|
|
37
|
+
const repo = this.db.getRepository('authenticators');
|
|
38
|
+
const authenticators = await repo.find({ filter: { enabled: true } });
|
|
39
|
+
const authenticator = authenticators.find((authenticator: Model) => authenticator.name === name);
|
|
40
|
+
return authenticator || authenticators[0];
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
this.app.authManager.registerTypes(presetAuthType, {
|
|
44
|
+
auth: BasicAuth,
|
|
45
|
+
});
|
|
46
|
+
// Register actions
|
|
47
|
+
Object.entries(authActions).forEach(([action, handler]) =>
|
|
48
|
+
this.app.resourcer.registerAction(`auth:${action}`, handler),
|
|
49
|
+
);
|
|
50
|
+
Object.entries(authenticatorsActions).forEach(([action, handler]) =>
|
|
51
|
+
this.app.resourcer.registerAction(`authenticators:${action}`, handler),
|
|
52
|
+
);
|
|
53
|
+
// Set up ACL
|
|
54
|
+
['check', 'signIn', 'signUp'].forEach((action) => this.app.acl.allow('auth', action));
|
|
55
|
+
['signOut', 'changePassword'].forEach((action) => this.app.acl.allow('auth', action, 'loggedIn'));
|
|
56
|
+
this.app.acl.allow('authenticators', 'publicList');
|
|
57
|
+
this.app.acl.registerSnippet({
|
|
58
|
+
name: `pm.${this.name}.authenticators`,
|
|
59
|
+
actions: ['authenticators:*'],
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async install(options?: InstallOptions) {
|
|
64
|
+
const repository = this.db.getRepository('authenticators');
|
|
65
|
+
const exist = await repository.findOne({ filter: { name: presetAuthenticator } });
|
|
66
|
+
if (exist) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await repository.create({
|
|
71
|
+
values: {
|
|
72
|
+
name: presetAuthenticator,
|
|
73
|
+
authType: presetAuthType,
|
|
74
|
+
description: 'Sign in with email and password.',
|
|
75
|
+
enabled: true,
|
|
76
|
+
options: {
|
|
77
|
+
public: {
|
|
78
|
+
allowSignUp: true,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async afterEnable() {}
|
|
86
|
+
|
|
87
|
+
async afterDisable() {}
|
|
88
|
+
|
|
89
|
+
async remove() {}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default AuthPlugin;
|