gemcap-be-common 1.2.134 → 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 +3 -3
- package/services/users.service.js +111 -198
- package/services/users.service.ts +114 -206
- 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,53 +76,35 @@ export class UsersService {
|
|
|
79
76
|
return users.find((user) => user.username === username);
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
getKeyCloakAdminBearer()
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const options = {
|
|
86
|
-
method: 'POST',
|
|
87
|
-
url: `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`,
|
|
88
|
-
headers: {
|
|
89
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
90
|
-
},
|
|
91
|
-
form: {
|
|
92
|
-
username: this.config.adminUser,
|
|
93
|
-
password: this.config.adminPass,
|
|
94
|
-
client_id: 'admin-cli',
|
|
95
|
-
grant_type: 'password',
|
|
96
|
-
},
|
|
97
|
-
};
|
|
79
|
+
async getKeyCloakAdminBearer() {
|
|
80
|
+
const url = `${this.config.keycloakHost}/realms/master/protocol/openid-connect/token`;
|
|
98
81
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
});
|
|
82
|
+
const data = qs.stringify({
|
|
83
|
+
username: this.config.adminUser,
|
|
84
|
+
password: this.config.adminPass,
|
|
85
|
+
client_id: 'admin-cli',
|
|
86
|
+
grant_type: 'password',
|
|
106
87
|
});
|
|
107
|
-
}
|
|
108
88
|
|
|
109
|
-
|
|
110
|
-
|
|
89
|
+
const headers = {
|
|
90
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
+
};
|
|
111
92
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
headers: {
|
|
116
|
-
'Content-Type': 'application/json',
|
|
117
|
-
Authorization: `Bearer ${authorization}`,
|
|
118
|
-
},
|
|
119
|
-
};
|
|
93
|
+
const response = await axios.post(url, data, { headers });
|
|
94
|
+
return response.data;
|
|
95
|
+
}
|
|
120
96
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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;
|
|
129
108
|
}
|
|
130
109
|
|
|
131
110
|
async getUsersWithRoles(authorization: string) {
|
|
@@ -144,77 +123,47 @@ export class UsersService {
|
|
|
144
123
|
return usersAndAccess;
|
|
145
124
|
}
|
|
146
125
|
|
|
147
|
-
getUserRoles(authorization: string, userId: string): Promise<{ realmMappings: IKeycloakRole[] }> {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
request(options, (error, _res, body) => {
|
|
160
|
-
if (!error) {
|
|
161
|
-
resolve(JSON.parse(body));
|
|
162
|
-
} else {
|
|
163
|
-
reject(error);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
});
|
|
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;
|
|
167
137
|
}
|
|
168
138
|
|
|
169
|
-
createUser(authorization, user): Promise<any> {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
request(options, (error, res, body) => {
|
|
185
|
-
if (!error) {
|
|
186
|
-
resolve(body);
|
|
187
|
-
} else {
|
|
188
|
-
console.error({ error });
|
|
189
|
-
reject(error);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
});
|
|
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;
|
|
193
153
|
}
|
|
194
154
|
|
|
195
|
-
updateUser(authorization: string, userId: string, keyValue): Promise<any> {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
request(options, (error, res, body) => {
|
|
210
|
-
if (!error) {
|
|
211
|
-
resolve(body);
|
|
212
|
-
} else {
|
|
213
|
-
console.error({ error });
|
|
214
|
-
reject(error);
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
});
|
|
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;
|
|
218
167
|
}
|
|
219
168
|
|
|
220
169
|
async updateMobileUser(userId: string, keyValue: { [key: string]: string | number | boolean }) {
|
|
@@ -226,78 +175,46 @@ export class UsersService {
|
|
|
226
175
|
.findOneAndUpdate({ keycloakUserId }, userAccess);
|
|
227
176
|
}
|
|
228
177
|
|
|
229
|
-
removeUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
request(options, (error, res, body) => {
|
|
244
|
-
if (!error) {
|
|
245
|
-
resolve(body);
|
|
246
|
-
} else {
|
|
247
|
-
console.error({ error });
|
|
248
|
-
reject(error);
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
});
|
|
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;
|
|
252
190
|
}
|
|
253
191
|
|
|
254
|
-
addUserRoles(authorization: string, userId: string, roles: IKeycloakRole[]): Promise<any> {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
request(options, (error, res, body) => {
|
|
269
|
-
if (!error) {
|
|
270
|
-
resolve(body);
|
|
271
|
-
} else {
|
|
272
|
-
console.error({ error });
|
|
273
|
-
reject(error);
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
});
|
|
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;
|
|
277
204
|
}
|
|
278
205
|
|
|
279
|
-
resetPassword(authorization: string, userId: string, temporaryPassword: string): Promise<any> {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
};
|
|
292
|
-
request(options, (error, res, body) => {
|
|
293
|
-
if (!error) {
|
|
294
|
-
resolve(body);
|
|
295
|
-
} else {
|
|
296
|
-
console.error({ error });
|
|
297
|
-
reject(error);
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
});
|
|
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;
|
|
301
218
|
}
|
|
302
219
|
|
|
303
220
|
async createNewPasswordWithEmail(authorization: string, userId: string, newPassword: string) {
|
|
@@ -339,26 +256,17 @@ export class UsersService {
|
|
|
339
256
|
}
|
|
340
257
|
}
|
|
341
258
|
|
|
342
|
-
getRoleList(authorization): Promise<IKeycloakRole[]> {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
request(options, (error, _res, body) => {
|
|
354
|
-
if (!error) {
|
|
355
|
-
resolve(JSON.parse(body));
|
|
356
|
-
} else {
|
|
357
|
-
console.error({ error });
|
|
358
|
-
reject(error);
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
});
|
|
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;
|
|
362
270
|
}
|
|
363
271
|
|
|
364
272
|
groupRoles(roles: IKeycloakRole[]): IGroupedKeycloakRoles {
|
|
@@ -524,6 +432,6 @@ export class UsersService {
|
|
|
524
432
|
lastName: keycloakUser.lastName,
|
|
525
433
|
});
|
|
526
434
|
}
|
|
527
|
-
}))
|
|
435
|
+
}));
|
|
528
436
|
}
|
|
529
437
|
}
|