gemcap-be-common 1.2.135 → 1.2.136
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/models/BorrowerCompliance.model.d.ts +1 -1
- package/models/BorrowerCompliance.model.js +1 -1
- package/models/BorrowerCompliance.model.ts +2 -2
- package/models/UserMobileAccess.model.js +1 -1
- package/models/UserMobileAccess.model.ts +2 -1
- package/package.json +1 -1
- package/services/users.service.d.ts +5 -5
- package/services/users.service.js +111 -200
- package/services/users.service.ts +114 -209
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -4,17 +4,15 @@ import express from 'express';
|
|
|
4
4
|
import axios from 'axios';
|
|
5
5
|
import dayjs from 'dayjs';
|
|
6
6
|
import mongoose from 'mongoose';
|
|
7
|
+
import qs from 'qs';
|
|
7
8
|
|
|
8
9
|
import { IUser, IUserDocument, UserModel } from '../models/User.model';
|
|
10
|
+
import { BorrowerCompliance } from '../models/BorrowerCompliance.model';
|
|
11
|
+
import { UserMobileAccess } from '../models/UserMobileAccess.model';
|
|
12
|
+
import { ELogActionType, ELogType, UserLog } from '../models/UserLog.model';
|
|
9
13
|
import { IGroupedKeycloakRoles, IKeycloakRole } from '../interfaces/keycloak-role.interface';
|
|
10
14
|
import { IKeycloakUser } from '../interfaces/keycloak-user.interface';
|
|
11
15
|
import { createLog, ICreateLogParams } from '../db/user-logs.db';
|
|
12
|
-
import { ELogActionType, ELogType, UserLog } from '../models/UserLog.model';
|
|
13
|
-
|
|
14
|
-
import { BorrowerCompliance } from '../models/BorrowerCompliance.model';
|
|
15
|
-
import { UserMobileAccess } from '../models/UserMobileAccess.model';
|
|
16
|
-
|
|
17
|
-
const request = require('request');
|
|
18
16
|
|
|
19
17
|
interface IKeycloakConfig {
|
|
20
18
|
keycloakHost: string;
|
|
@@ -61,8 +59,7 @@ export class UsersService {
|
|
|
61
59
|
},
|
|
62
60
|
};
|
|
63
61
|
const result = await axios.request(options);
|
|
64
|
-
|
|
65
|
-
return user;
|
|
62
|
+
return result.data;
|
|
66
63
|
}
|
|
67
64
|
|
|
68
65
|
async getUserRepresentationByUsername(authorization: string, username: string): Promise<IKeycloakUser> {
|
|
@@ -79,56 +76,35 @@ export class UsersService {
|
|
|
79
76
|
return users.find((user) => user.username === username);
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
getKeyCloakAdminBearer()
|
|
83
|
-
|
|
84
|
-
console.log('this.config:', this?.config);
|
|
85
|
-
|
|
86
|
-
return new Promise(function(resolve, reject) {
|
|
87
|
-
|
|
88
|
-
const options = {
|
|
89
|
-
method: 'POST',
|
|
90
|
-
url: `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`,
|
|
91
|
-
headers: {
|
|
92
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
93
|
-
},
|
|
94
|
-
form: {
|
|
95
|
-
username: this.config.adminUser,
|
|
96
|
-
password: this.config.adminPass,
|
|
97
|
-
client_id: 'admin-cli',
|
|
98
|
-
grant_type: 'password',
|
|
99
|
-
},
|
|
100
|
-
};
|
|
79
|
+
async getKeyCloakAdminBearer() {
|
|
80
|
+
const url = `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`;
|
|
101
81
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
});
|
|
82
|
+
const data = qs.stringify({
|
|
83
|
+
username: this.config.adminUser,
|
|
84
|
+
password: this.config.adminPass,
|
|
85
|
+
client_id: 'admin-cli',
|
|
86
|
+
grant_type: 'password',
|
|
109
87
|
});
|
|
110
|
-
}
|
|
111
88
|
|
|
112
|
-
|
|
113
|
-
|
|
89
|
+
const headers = {
|
|
90
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
+
};
|
|
114
92
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
headers: {
|
|
119
|
-
'Content-Type': 'application/json',
|
|
120
|
-
Authorization: `Bearer ${authorization}`,
|
|
121
|
-
},
|
|
122
|
-
};
|
|
93
|
+
const response = await axios.post(url, data, { headers });
|
|
94
|
+
return response.data;
|
|
95
|
+
}
|
|
123
96
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
97
|
+
async getUserList(authorization: string): Promise<IKeycloakUser[]> {
|
|
98
|
+
const options = {
|
|
99
|
+
method: 'GET',
|
|
100
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
|
|
101
|
+
headers: {
|
|
102
|
+
'Content-Type': 'application/json',
|
|
103
|
+
Authorization: `Bearer ${authorization}`,
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
const result = await axios.request(options);
|
|
107
|
+
return result.data;
|
|
132
108
|
}
|
|
133
109
|
|
|
134
110
|
async getUsersWithRoles(authorization: string) {
|
|
@@ -147,77 +123,47 @@ export class UsersService {
|
|
|
147
123
|
return usersAndAccess;
|
|
148
124
|
}
|
|
149
125
|
|
|
150
|
-
getUserRoles(authorization: string, userId: string): Promise<{ realmMappings: IKeycloakRole[] }> {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
request(options, (error, _res, body) => {
|
|
163
|
-
if (!error) {
|
|
164
|
-
resolve(JSON.parse(body));
|
|
165
|
-
} else {
|
|
166
|
-
reject(error);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
});
|
|
126
|
+
async getUserRoles(authorization: string, userId: string): Promise<{ realmMappings: IKeycloakRole[] }> {
|
|
127
|
+
const options = {
|
|
128
|
+
method: 'GET',
|
|
129
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings`,
|
|
130
|
+
headers: {
|
|
131
|
+
'Content-Type': 'application/json',
|
|
132
|
+
Authorization: `Bearer ${authorization}`,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
const result = await axios.request(options);
|
|
136
|
+
return result.data;
|
|
170
137
|
}
|
|
171
138
|
|
|
172
|
-
createUser(authorization, user): Promise<any> {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
request(options, (error, res, body) => {
|
|
188
|
-
if (!error) {
|
|
189
|
-
resolve(body);
|
|
190
|
-
} else {
|
|
191
|
-
console.error({ error });
|
|
192
|
-
reject(error);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
});
|
|
139
|
+
async createUser(authorization: string, user): Promise<any> {
|
|
140
|
+
const options = {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users`,
|
|
143
|
+
headers: {
|
|
144
|
+
'Content-Type': 'application/json',
|
|
145
|
+
Authorization: `Bearer ${authorization}`,
|
|
146
|
+
},
|
|
147
|
+
data: {
|
|
148
|
+
...user,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
const result = await axios.request(options);
|
|
152
|
+
return result.data;
|
|
196
153
|
}
|
|
197
154
|
|
|
198
|
-
updateUser(authorization: string, userId: string, keyValue): Promise<any> {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
request(options, (error, res, body) => {
|
|
213
|
-
if (!error) {
|
|
214
|
-
resolve(body);
|
|
215
|
-
} else {
|
|
216
|
-
console.error({ error });
|
|
217
|
-
reject(error);
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
155
|
+
async updateUser(authorization: string, userId: string, keyValue): Promise<any> {
|
|
156
|
+
const options = {
|
|
157
|
+
method: 'PUT',
|
|
158
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}`,
|
|
159
|
+
headers: {
|
|
160
|
+
'Content-Type': 'application/json',
|
|
161
|
+
Authorization: `Bearer ${authorization}`,
|
|
162
|
+
},
|
|
163
|
+
data: keyValue,
|
|
164
|
+
};
|
|
165
|
+
const result = await axios.request(options);
|
|
166
|
+
return result.data;
|
|
221
167
|
}
|
|
222
168
|
|
|
223
169
|
async updateMobileUser(userId: string, keyValue: { [key: string]: string | number | boolean }) {
|
|
@@ -229,78 +175,46 @@ export class UsersService {
|
|
|
229
175
|
.findOneAndUpdate({ keycloakUserId }, userAccess);
|
|
230
176
|
}
|
|
231
177
|
|
|
232
|
-
removeUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
request(options, (error, res, body) => {
|
|
247
|
-
if (!error) {
|
|
248
|
-
resolve(body);
|
|
249
|
-
} else {
|
|
250
|
-
console.error({ error });
|
|
251
|
-
reject(error);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
});
|
|
178
|
+
async removeUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
179
|
+
const options = {
|
|
180
|
+
method: 'DELETE',
|
|
181
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
|
|
182
|
+
headers: {
|
|
183
|
+
'Content-Type': 'application/json',
|
|
184
|
+
Authorization: `Bearer ${authorization}`,
|
|
185
|
+
},
|
|
186
|
+
data: [...roles],
|
|
187
|
+
};
|
|
188
|
+
const result = await axios.request(options);
|
|
189
|
+
return result.data;
|
|
255
190
|
}
|
|
256
191
|
|
|
257
|
-
addUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
request(options, (error, res, body) => {
|
|
272
|
-
if (!error) {
|
|
273
|
-
resolve(body);
|
|
274
|
-
} else {
|
|
275
|
-
console.error({ error });
|
|
276
|
-
reject(error);
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
});
|
|
192
|
+
async addUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
193
|
+
const options = {
|
|
194
|
+
method: 'POST',
|
|
195
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/role-mappings/realm`,
|
|
196
|
+
headers: {
|
|
197
|
+
'Content-Type': 'application/json',
|
|
198
|
+
Authorization: `Bearer ${authorization}`,
|
|
199
|
+
},
|
|
200
|
+
data: [...roles],
|
|
201
|
+
};
|
|
202
|
+
const result = await axios.request(options);
|
|
203
|
+
return result.data;
|
|
280
204
|
}
|
|
281
205
|
|
|
282
|
-
resetPassword(authorization: string, userId: string, temporaryPassword: string): Promise<any> {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
};
|
|
295
|
-
request(options, (error, res, body) => {
|
|
296
|
-
if (!error) {
|
|
297
|
-
resolve(body);
|
|
298
|
-
} else {
|
|
299
|
-
console.error({ error });
|
|
300
|
-
reject(error);
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
});
|
|
206
|
+
async resetPassword(authorization: string, userId: string, temporaryPassword: string): Promise<any> {
|
|
207
|
+
const options = {
|
|
208
|
+
method: 'PUT',
|
|
209
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/users/${userId}/reset-password`,
|
|
210
|
+
headers: {
|
|
211
|
+
'Content-Type': 'application/json',
|
|
212
|
+
Authorization: `Bearer ${authorization}`,
|
|
213
|
+
},
|
|
214
|
+
data: { value: temporaryPassword, temporary: true },
|
|
215
|
+
};
|
|
216
|
+
const result = await axios.request(options);
|
|
217
|
+
return result.data;
|
|
304
218
|
}
|
|
305
219
|
|
|
306
220
|
async createNewPasswordWithEmail(authorization: string, userId: string, newPassword: string) {
|
|
@@ -342,26 +256,17 @@ export class UsersService {
|
|
|
342
256
|
}
|
|
343
257
|
}
|
|
344
258
|
|
|
345
|
-
getRoleList(authorization): Promise<IKeycloakRole[]> {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
request(options, (error, _res, body) => {
|
|
357
|
-
if (!error) {
|
|
358
|
-
resolve(JSON.parse(body));
|
|
359
|
-
} else {
|
|
360
|
-
console.error({ error });
|
|
361
|
-
reject(error);
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
});
|
|
259
|
+
async getRoleList(authorization: string): Promise<IKeycloakRole[]> {
|
|
260
|
+
const options = {
|
|
261
|
+
method: 'GET',
|
|
262
|
+
url: `${this.config.keycloakHost}/admin/realms/${this.config.realm}/roles`,
|
|
263
|
+
headers: {
|
|
264
|
+
'Content-Type': 'application/json',
|
|
265
|
+
Authorization: `Bearer ${authorization}`,
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
const result = await axios.request(options);
|
|
269
|
+
return result.data;
|
|
365
270
|
}
|
|
366
271
|
|
|
367
272
|
groupRoles(roles: IKeycloakRole[]): IGroupedKeycloakRoles {
|
|
@@ -527,6 +432,6 @@ export class UsersService {
|
|
|
527
432
|
lastName: keycloakUser.lastName,
|
|
528
433
|
});
|
|
529
434
|
}
|
|
530
|
-
}))
|
|
435
|
+
}));
|
|
531
436
|
}
|
|
532
437
|
}
|