@hapiboo/module-auth 1.2.2 → 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.
@@ -202,8 +202,8 @@ class Password {
202
202
  return this.hash === this.getHash(base64password);
203
203
  }
204
204
  setPassword(base64Password) {
205
- this.length = settings_1.authSettings.passwordHashLength;
206
- this.iterations = settings_1.authSettings.passwordHashIterations;
205
+ this.length = settings_1.authSettings.passwordHashLength();
206
+ this.iterations = settings_1.authSettings.passwordHashIterations();
207
207
  this.salt = crypto_1.cryptoProvider.generateSalt(this.length);
208
208
  this.hash = this.getHash(base64Password);
209
209
  this.lastChangedAt = date_1.DateObject.now().toUtc();
@@ -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;
@@ -217,8 +226,9 @@ var authenticationService;
217
226
  core_1.promise.checkVoid(authorization_1.authorizationService.general.initializeSecurityModel(), reject)
218
227
  .then(() => {
219
228
  //create first admin
220
- console.info('creating first admin user: ' + settings_1.authSettings.firstAdmin.email);
221
- core_1.promise.resolve(admin.invite(settings_1.authSettings.firstAdmin.email, settings_1.authSettings.firstAdmin.email, settings_1.authSettings.firstAdmin.name, baseURL), resolve, reject);
229
+ const to_create = settings_1.authSettings.firstAdmin();
230
+ console.info('... creating first admin user: ' + to_create.email);
231
+ core_1.promise.resolve(admin.invite(to_create.email, to_create.email, to_create.name, baseURL), resolve, reject);
222
232
  });
223
233
  }
224
234
  else {
@@ -228,6 +238,12 @@ var authenticationService;
228
238
  });
229
239
  }
230
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;
231
247
  function get(user_id) {
232
248
  return repository_1.repository.user.getById(user_id);
233
249
  }
@@ -17,8 +17,9 @@ var authorizationValidator;
17
17
  function checkUserPermission(user_id, permission_id) {
18
18
  if (userPermissionCache.has(user_id)) {
19
19
  const permissions = userPermissionCache.get(user_id);
20
- if (settings_1.authSettings.overrulingPermission !== undefined) {
21
- return permissions.includes(permission_id) || permissions.includes(settings_1.authSettings.overrulingPermission);
20
+ const overrulingPermission = settings_1.authSettings.overrulingPermission();
21
+ if (overrulingPermission !== undefined) {
22
+ return permissions.includes(permission_id) || permissions.includes(overrulingPermission);
22
23
  }
23
24
  else {
24
25
  return permissions.includes(permission_id);
@@ -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: {
@@ -28,15 +29,18 @@ export interface IAuthSettings {
28
29
  }
29
30
  export declare namespace authSettings {
30
31
  function init(settings: IAuthSettings, performers: IAuthPerformers): void;
31
- const allowSelfRegistration: boolean;
32
- const allowMultiLogin: boolean;
33
- const passwordHashLength: number;
34
- const passwordHashIterations: number;
35
- const firstAdmin: {
32
+ function allowSelfRegistration(): boolean;
33
+ function allowMultiLogin(): boolean;
34
+ function allowTestMode(): boolean;
35
+ function testMode(): boolean;
36
+ function changeTestMode(): boolean;
37
+ function passwordHashLength(): number;
38
+ function passwordHashIterations(): number;
39
+ function firstAdmin(): {
36
40
  name: string;
37
41
  email: string;
38
42
  };
39
- const overrulingPermission: string | undefined;
43
+ function overrulingPermission(): string | undefined;
40
44
  function getPermissions(): Permission[];
41
45
  function getRoles(): Role[];
42
46
  function sendConfirmation(emailAddress: string, displayName: string, isInvited: boolean, confirmationLink: IAuthEmailLink): IResponsePromiseVoid;
@@ -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: {
@@ -17,33 +19,51 @@ var authSettings;
17
19
  };
18
20
  let _performers = undefined;
19
21
  function init(settings, performers) {
20
- console.log('authSettings.init()');
22
+ console.info('Authentication Settings Initialization');
21
23
  _settings = settings;
22
24
  _performers = performers;
23
25
  }
24
26
  authSettings.init = init;
25
- authSettings.allowSelfRegistration = (() => {
27
+ function allowSelfRegistration() {
26
28
  return _settings.allowSelfRegistration;
27
- })();
28
- authSettings.allowMultiLogin = (() => {
29
+ }
30
+ authSettings.allowSelfRegistration = allowSelfRegistration;
31
+ function allowMultiLogin() {
29
32
  return _settings.allowMultiLogin;
30
- })();
31
- authSettings.passwordHashLength = (() => {
33
+ }
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;
48
+ function passwordHashLength() {
32
49
  return _settings.passwordHashLength;
33
- })();
34
- authSettings.passwordHashIterations = (() => {
50
+ }
51
+ authSettings.passwordHashLength = passwordHashLength;
52
+ function passwordHashIterations() {
35
53
  return _settings.passwordHashIterations;
36
- })();
37
- authSettings.firstAdmin = (() => {
38
- console.info('authSettings.firstAdmin: ' + _settings.firstAdmin.name + ' <' + _settings.firstAdmin.email + '>');
54
+ }
55
+ authSettings.passwordHashIterations = passwordHashIterations;
56
+ function firstAdmin() {
39
57
  return {
40
58
  name: _settings.firstAdmin.name,
41
59
  email: _settings.firstAdmin.email
42
60
  };
43
- })();
44
- authSettings.overrulingPermission = (() => {
61
+ }
62
+ authSettings.firstAdmin = firstAdmin;
63
+ function overrulingPermission() {
45
64
  return _settings.overrulingPermission;
46
- })();
65
+ }
66
+ authSettings.overrulingPermission = overrulingPermission;
47
67
  function getPermissions() {
48
68
  if (_settings.getPermissions) {
49
69
  return _settings.getPermissions();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapiboo/module-auth",
3
- "version": "1.2.2",
3
+ "version": "1.4.0",
4
4
  "description": "MK13 Studio Hapiboo - Auth Module",
5
5
  "author": "MK13 Studio",
6
6
  "license": "ISC",