@hapiboo/module-auth 1.3.0 → 1.4.0

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.
@@ -7,6 +7,9 @@ export declare namespace authController {
7
7
  function start(): IResponsePromise<{
8
8
  id: string;
9
9
  }>;
10
+ function testMode(): IResponsePromise<{
11
+ testMode: boolean;
12
+ }>;
10
13
  }
11
14
  namespace user {
12
15
  function register(request: Request): IResponsePromise<{
@@ -12,6 +12,10 @@ var authController;
12
12
  return authentication_1.authenticationService.general.initData(process.env.FRONTEND_URL);
13
13
  }
14
14
  init.start = start;
15
+ function testMode() {
16
+ return authentication_1.authenticationService.general.testMode();
17
+ }
18
+ init.testMode = testMode;
15
19
  })(init = authController.init || (authController.init = {}));
16
20
  let user;
17
21
  (function (user) {
@@ -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
- //we need to find out if credentialName is not used
82
- core_1.promise.check(searchPassword(credentialName, true), reject)
83
- .then((value) => {
84
- if (value.found) {
85
- reject(core_1.ResponseError.getMessageResponse(409, 'credential name already registered'));
86
- }
87
- else {
88
- if (display && email && credentialName) {
89
- //let's create actual user data
90
- const user_id = (0, uuid_1.v4)();
91
- let confirm = (0, uuid_1.v4)().substring(0, 6).toUpperCase();
92
- let invite = undefined;
93
- if (isInvited) {
94
- confirm = undefined;
95
- invite = (0, uuid_1.v4)().substring(0, 6).toUpperCase();
96
- password = '';
97
- }
98
- 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)
99
- .then((cred) => {
100
- //now, create user
101
- 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)
102
- .then((dataItem) => {
103
- //and send the code to confirm
104
- emails.confirm(email, display, isInvited, isInvited ? cred.invite : cred.confirmation, baseUrl)
105
- .then(() => {
106
- resolve({ user: dataItem, credentials: cred });
107
- }).catch((reason) => {
108
- console.error(reason.message);
109
- //in case of error, we need to remove all created data
110
- repository_1.repository.user.remove(dataItem.id)
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
- repository_1.repository.credential.remove(cred.id)
113
- .then(() => {
114
- reject(reason);
115
- }).catch((reasonCred) => {
116
- //we have 2 errors, and needs to combine them
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
- //we have 2 errors, and needs to combine them
124
- reject(core_1.ResponseError.getCollectionResponse([reason, reasonUser]));
125
- }).catch((reasonCred) => {
126
- //we have 3 errors, and needs to combine them
127
- reject(core_1.ResponseError.getCollectionResponse([reason, reasonUser, reasonCred]));
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
- else {
135
- reject(core_1.ResponseError.getMessageResponse(400, 'data is not valid'));
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(): {
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapiboo/module-auth",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "MK13 Studio Hapiboo - Auth Module",
5
5
  "author": "MK13 Studio",
6
6
  "license": "ISC",