@hapiboo/module-auth 1.3.0 → 1.4.1
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/router/controller.d.ts +3 -0
- package/dist/router/controller.js +31 -18
- package/dist/router/init.js +3 -0
- package/dist/service/authentication.d.ts +3 -0
- package/dist/service/authentication.js +66 -51
- package/dist/service/settings.d.ts +4 -0
- package/dist/service/settings.js +15 -0
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.authController = void 0;
|
|
4
|
+
const core_1 = require("@hapiboo/core");
|
|
4
5
|
const server_1 = require("@hapiboo/server");
|
|
5
6
|
const authentication_1 = require("../service/authentication");
|
|
6
7
|
const authorization_1 = require("../service/authorization");
|
|
@@ -12,29 +13,36 @@ var authController;
|
|
|
12
13
|
return authentication_1.authenticationService.general.initData(process.env.FRONTEND_URL);
|
|
13
14
|
}
|
|
14
15
|
init.start = start;
|
|
16
|
+
function testMode() {
|
|
17
|
+
return authentication_1.authenticationService.general.testMode();
|
|
18
|
+
}
|
|
19
|
+
init.testMode = testMode;
|
|
15
20
|
})(init = authController.init || (authController.init = {}));
|
|
16
21
|
let user;
|
|
17
22
|
(function (user) {
|
|
18
23
|
function register(request) {
|
|
19
24
|
return server_1.server.createValidatedPromise(request, (req) => {
|
|
20
|
-
return server_1.validator.
|
|
25
|
+
return server_1.validator.validateBody(request) &&
|
|
26
|
+
server_1.validator.validateString(req.body.name) &&
|
|
21
27
|
server_1.validator.validateString(req.body.login) &&
|
|
22
28
|
server_1.validator.validateString(req.body.password);
|
|
23
|
-
}, authentication_1.authenticationService.user.registerPassword(request.body.login, request.body.name, request.body.login, request.body.password, request.body.name, request.body.login, process.env.FRONTEND_URL));
|
|
29
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.user.registerPassword(request.body.login, request.body.name, request.body.login, request.body.password, request.body.name, request.body.login, process.env.FRONTEND_URL).then(resolve).catch(reject)));
|
|
24
30
|
}
|
|
25
31
|
user.register = register;
|
|
26
32
|
function registrationConfirm(request) {
|
|
27
33
|
return server_1.server.createValidatedPromise(request, (req) => {
|
|
28
|
-
return server_1.validator.
|
|
34
|
+
return server_1.validator.validateBody(request) &&
|
|
35
|
+
server_1.validator.validateString(req.body.code) &&
|
|
29
36
|
server_1.validator.validateString(req.body.password);
|
|
30
|
-
}, authentication_1.authenticationService.actions.confirmRegistration(request.body.code, request.body.password));
|
|
37
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.actions.confirmRegistration(request.body.code, request.body.password).then(resolve).catch(reject)));
|
|
31
38
|
}
|
|
32
39
|
user.registrationConfirm = registrationConfirm;
|
|
33
40
|
function invitationConfirm(request) {
|
|
34
41
|
return server_1.server.createValidatedPromise(request, (req) => {
|
|
35
|
-
return server_1.validator.
|
|
42
|
+
return server_1.validator.validateBody(request) &&
|
|
43
|
+
server_1.validator.validateString(req.body.code) &&
|
|
36
44
|
server_1.validator.validateString(req.body.password);
|
|
37
|
-
}, authentication_1.authenticationService.actions.confirmInvitation(request.body.code, request.body.password));
|
|
45
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.actions.confirmInvitation(request.body.code, request.body.password).then(resolve).catch(reject)));
|
|
38
46
|
}
|
|
39
47
|
user.invitationConfirm = invitationConfirm;
|
|
40
48
|
})(user = authController.user || (authController.user = {}));
|
|
@@ -42,32 +50,36 @@ var authController;
|
|
|
42
50
|
(function (password) {
|
|
43
51
|
function login(request) {
|
|
44
52
|
return server_1.server.createValidatedPromise(request, (req) => {
|
|
45
|
-
return server_1.validator.
|
|
53
|
+
return server_1.validator.validateBody(request) &&
|
|
54
|
+
server_1.validator.validateString(req.body.login) &&
|
|
46
55
|
server_1.validator.validateString(req.body.password);
|
|
47
|
-
}, authentication_1.authenticationService.me.passwordLogin(request.body.login, request.body.password));
|
|
56
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.me.passwordLogin(request.body.login, request.body.password).then(resolve).catch(reject)));
|
|
48
57
|
}
|
|
49
58
|
password.login = login;
|
|
50
59
|
function forgot(request) {
|
|
51
60
|
return server_1.server.createValidatedVoidPromise(request, (req) => {
|
|
52
|
-
return server_1.validator.
|
|
53
|
-
|
|
61
|
+
return server_1.validator.validateBody(request) &&
|
|
62
|
+
server_1.validator.validateString(req.body.email);
|
|
63
|
+
}, core_1.promise.createDelayedVoidPromise((resolve, reject) => authentication_1.authenticationService.actions.forgotPassword(request.body.email, process.env.FRONTEND_URL).then(resolve).catch(reject)));
|
|
54
64
|
}
|
|
55
65
|
password.forgot = forgot;
|
|
56
66
|
function recover(request) {
|
|
57
67
|
return server_1.server.createValidatedPromise(request, (req) => {
|
|
58
|
-
return server_1.validator.
|
|
68
|
+
return server_1.validator.validateBody(request) &&
|
|
69
|
+
server_1.validator.validateString(req.body.recovery) &&
|
|
59
70
|
server_1.validator.validateString(req.body.password);
|
|
60
|
-
}, authentication_1.authenticationService.actions.recoverPassword(request.body.recovery, request.body.password));
|
|
71
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.actions.recoverPassword(request.body.recovery, request.body.password).then(resolve).catch(reject)));
|
|
61
72
|
}
|
|
62
73
|
password.recover = recover;
|
|
63
74
|
function change(request) {
|
|
64
75
|
return server_1.validator.createValidatedPromise(() => {
|
|
65
|
-
return request
|
|
76
|
+
return server_1.validator.validateBody(request) &&
|
|
77
|
+
request.payload !== undefined &&
|
|
66
78
|
server_1.validator.validateString(request.payload.user_id) &&
|
|
67
79
|
server_1.validator.validateString(request.body.email) &&
|
|
68
80
|
server_1.validator.validateString(request.body.oldPassword) &&
|
|
69
81
|
server_1.validator.validateString(request.body.newPassword);
|
|
70
|
-
}, authentication_1.authenticationService.me.changePassword(request.payload.user_id, request.body.email, request.body.oldPassword, request.body.newPassword));
|
|
82
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.me.changePassword(request.payload.user_id, request.body.email, request.body.oldPassword, request.body.newPassword).then(resolve).catch(reject)));
|
|
71
83
|
}
|
|
72
84
|
password.change = change;
|
|
73
85
|
function refresh(request) {
|
|
@@ -75,7 +87,7 @@ var authController;
|
|
|
75
87
|
return request.payload !== undefined &&
|
|
76
88
|
server_1.validator.validateString(request.payload.user_id) &&
|
|
77
89
|
server_1.validator.validateString(request.payload.code);
|
|
78
|
-
}, authentication_1.authenticationService.me.refreshTokenLogin(request.payload.user_id, request.payload.code));
|
|
90
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.me.refreshTokenLogin(request.payload.user_id, request.payload.code).then(resolve).catch(reject)));
|
|
79
91
|
}
|
|
80
92
|
password.refresh = refresh;
|
|
81
93
|
})(password = authController.password || (authController.password = {}));
|
|
@@ -85,15 +97,16 @@ var authController;
|
|
|
85
97
|
return server_1.validator.createValidatedPromise(() => {
|
|
86
98
|
return request.payload !== undefined &&
|
|
87
99
|
server_1.validator.validateString(request.payload.user_id);
|
|
88
|
-
}, authentication_1.authenticationService.me.get(request.payload.user_id));
|
|
100
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.me.get(request.payload.user_id).then(resolve).catch(reject)));
|
|
89
101
|
}
|
|
90
102
|
profile.get = get;
|
|
91
103
|
function update(request) {
|
|
92
104
|
return server_1.validator.createValidatedVoidPromise(() => {
|
|
93
|
-
return request
|
|
105
|
+
return server_1.validator.validateBody(request) &&
|
|
106
|
+
request.payload !== undefined &&
|
|
94
107
|
server_1.validator.validateString(request.payload.user_id) &&
|
|
95
108
|
server_1.validator.validateString(request.body.name);
|
|
96
|
-
}, authentication_1.authenticationService.me.update(request.payload.user_id, request.body.name));
|
|
109
|
+
}, core_1.promise.createDelayedPromise((resolve, reject) => authentication_1.authenticationService.me.update(request.payload.user_id, request.body.name).then(resolve).catch(reject)));
|
|
97
110
|
}
|
|
98
111
|
profile.update = update;
|
|
99
112
|
})(profile = authController.profile || (authController.profile = {}));
|
package/dist/router/init.js
CHANGED
|
@@ -8,4 +8,7 @@ const router = (0, express_1.Router)();
|
|
|
8
8
|
router.put('', async (_request, response) => {
|
|
9
9
|
server_1.server.prepareJsonPromiseResponse(response, controller_1.authController.init.start());
|
|
10
10
|
});
|
|
11
|
+
router.post('/test', async (_request, response) => {
|
|
12
|
+
server_1.server.prepareJsonPromiseResponse(response, controller_1.authController.init.testMode());
|
|
13
|
+
});
|
|
11
14
|
exports.init = server_1.routingProvider.createRoutingItem('/init', router);
|
|
@@ -6,6 +6,9 @@ export declare namespace authenticationService {
|
|
|
6
6
|
function initData(baseURL: string): IResponsePromise<{
|
|
7
7
|
id: string;
|
|
8
8
|
}>;
|
|
9
|
+
function testMode(): IResponsePromise<{
|
|
10
|
+
testMode: boolean;
|
|
11
|
+
}>;
|
|
9
12
|
function get(user_id: string): IResponsePromise<User>;
|
|
10
13
|
}
|
|
11
14
|
namespace me {
|
|
@@ -78,64 +78,73 @@ var helper;
|
|
|
78
78
|
(function (users) {
|
|
79
79
|
function registerPassword(display, email, credentialName, password, admin, isInvited, account_id, baseUrl) {
|
|
80
80
|
return core_1.promise.createPromise((resolve, reject) => {
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
81
|
+
//let's check if we could move forward with current settings
|
|
82
|
+
if (!isInvited && !settings_1.authSettings.allowSelfRegistration()) {
|
|
83
|
+
reject(core_1.ResponseError.getMessageResponse(403, 'Self registration is not allowed.'));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
//we need to find out if credentialName is not used
|
|
87
|
+
core_1.promise.check(searchPassword(credentialName, true), reject)
|
|
88
|
+
.then((value) => {
|
|
89
|
+
if (value.found) {
|
|
90
|
+
reject(core_1.ResponseError.getMessageResponse(409, 'credential name already registered'));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
if (display && email && credentialName) {
|
|
94
|
+
//let's create actual user data
|
|
95
|
+
const user_id = (0, uuid_1.v4)();
|
|
96
|
+
let invite = undefined;
|
|
97
|
+
let confirm = (0, uuid_1.v4)().substring(0, 6).toUpperCase();
|
|
98
|
+
if (settings_1.authSettings.allowTestMode() && settings_1.authSettings.testMode()) {
|
|
99
|
+
confirm = 'M6K513';
|
|
100
|
+
}
|
|
101
|
+
if (isInvited) {
|
|
102
|
+
invite = confirm;
|
|
103
|
+
confirm = undefined;
|
|
104
|
+
password = '';
|
|
105
|
+
}
|
|
106
|
+
core_1.promise.check(repository_1.repository.credential.create(new models_1.Credential().createPassword((0, uuid_1.v4)(), credentialName, user_id, 'pending', password, confirm, invite)), reject)
|
|
107
|
+
.then((cred) => {
|
|
108
|
+
//now, create user
|
|
109
|
+
core_1.promise.check(repository_1.repository.user.create(new models_1.User().create(user_id, account_id, admin ? 'admin' : 'standard', 'created', display, email)), reject)
|
|
110
|
+
.then((dataItem) => {
|
|
111
|
+
//and send the code to confirm
|
|
112
|
+
emails.confirm(email, display, isInvited, isInvited ? cred.invite : cred.confirmation, baseUrl)
|
|
111
113
|
.then(() => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
reject(core_1.ResponseError.getCollectionResponse([reason, reasonCred]));
|
|
118
|
-
});
|
|
119
|
-
}).catch((reasonUser) => {
|
|
120
|
-
//despite error, let's try to clean up other data
|
|
121
|
-
repository_1.repository.credential.remove(cred.id)
|
|
114
|
+
resolve({ user: dataItem, credentials: cred });
|
|
115
|
+
}).catch((reason) => {
|
|
116
|
+
console.error(reason.message);
|
|
117
|
+
//in case of error, we need to remove all created data
|
|
118
|
+
repository_1.repository.user.remove(dataItem.id)
|
|
122
119
|
.then(() => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
120
|
+
repository_1.repository.credential.remove(cred.id)
|
|
121
|
+
.then(() => {
|
|
122
|
+
reject(reason);
|
|
123
|
+
}).catch((reasonCred) => {
|
|
124
|
+
//we have 2 errors, and needs to combine them
|
|
125
|
+
reject(core_1.ResponseError.getCollectionResponse([reason, reasonCred]));
|
|
126
|
+
});
|
|
127
|
+
}).catch((reasonUser) => {
|
|
128
|
+
//despite error, let's try to clean up other data
|
|
129
|
+
repository_1.repository.credential.remove(cred.id)
|
|
130
|
+
.then(() => {
|
|
131
|
+
//we have 2 errors, and needs to combine them
|
|
132
|
+
reject(core_1.ResponseError.getCollectionResponse([reason, reasonUser]));
|
|
133
|
+
}).catch((reasonCred) => {
|
|
134
|
+
//we have 3 errors, and needs to combine them
|
|
135
|
+
reject(core_1.ResponseError.getCollectionResponse([reason, reasonUser, reasonCred]));
|
|
136
|
+
});
|
|
128
137
|
});
|
|
129
138
|
});
|
|
130
139
|
});
|
|
131
140
|
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
reject(core_1.ResponseError.getMessageResponse(400, 'data is not valid'));
|
|
144
|
+
}
|
|
136
145
|
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
139
148
|
});
|
|
140
149
|
}
|
|
141
150
|
users.registerPassword = registerPassword;
|
|
@@ -229,6 +238,12 @@ var authenticationService;
|
|
|
229
238
|
});
|
|
230
239
|
}
|
|
231
240
|
general.initData = initData;
|
|
241
|
+
function testMode() {
|
|
242
|
+
return core_1.promise.createDelayedPromise((resolve, _reject) => {
|
|
243
|
+
resolve({ testMode: settings_1.authSettings.changeTestMode() });
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
general.testMode = testMode;
|
|
232
247
|
function get(user_id) {
|
|
233
248
|
return repository_1.repository.user.getById(user_id);
|
|
234
249
|
}
|
|
@@ -16,6 +16,7 @@ export interface IAuthPerformers {
|
|
|
16
16
|
export interface IAuthSettings {
|
|
17
17
|
allowSelfRegistration: boolean;
|
|
18
18
|
allowMultiLogin: boolean;
|
|
19
|
+
allowTestMode: boolean;
|
|
19
20
|
passwordHashLength: number;
|
|
20
21
|
passwordHashIterations: number;
|
|
21
22
|
firstAdmin: {
|
|
@@ -30,6 +31,9 @@ export declare namespace authSettings {
|
|
|
30
31
|
function init(settings: IAuthSettings, performers: IAuthPerformers): void;
|
|
31
32
|
function allowSelfRegistration(): boolean;
|
|
32
33
|
function allowMultiLogin(): boolean;
|
|
34
|
+
function allowTestMode(): boolean;
|
|
35
|
+
function testMode(): boolean;
|
|
36
|
+
function changeTestMode(): boolean;
|
|
33
37
|
function passwordHashLength(): number;
|
|
34
38
|
function passwordHashIterations(): number;
|
|
35
39
|
function firstAdmin(): {
|
package/dist/service/settings.js
CHANGED
|
@@ -4,9 +4,11 @@ exports.authSettings = void 0;
|
|
|
4
4
|
const core_1 = require("@hapiboo/core");
|
|
5
5
|
var authSettings;
|
|
6
6
|
(function (authSettings) {
|
|
7
|
+
let _test = false;
|
|
7
8
|
let _settings = {
|
|
8
9
|
allowSelfRegistration: true,
|
|
9
10
|
allowMultiLogin: false,
|
|
11
|
+
allowTestMode: false,
|
|
10
12
|
passwordHashLength: 1024,
|
|
11
13
|
passwordHashIterations: 32,
|
|
12
14
|
firstAdmin: {
|
|
@@ -30,6 +32,19 @@ var authSettings;
|
|
|
30
32
|
return _settings.allowMultiLogin;
|
|
31
33
|
}
|
|
32
34
|
authSettings.allowMultiLogin = allowMultiLogin;
|
|
35
|
+
function allowTestMode() {
|
|
36
|
+
return _settings.allowTestMode;
|
|
37
|
+
}
|
|
38
|
+
authSettings.allowTestMode = allowTestMode;
|
|
39
|
+
function testMode() {
|
|
40
|
+
return _test;
|
|
41
|
+
}
|
|
42
|
+
authSettings.testMode = testMode;
|
|
43
|
+
function changeTestMode() {
|
|
44
|
+
_test = !_test;
|
|
45
|
+
return _test;
|
|
46
|
+
}
|
|
47
|
+
authSettings.changeTestMode = changeTestMode;
|
|
33
48
|
function passwordHashLength() {
|
|
34
49
|
return _settings.passwordHashLength;
|
|
35
50
|
}
|