This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
/
users.ts
364 lines (326 loc) · 8.5 KB
/
users.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import Resource from './resource';
import UserRepresentation from '../defs/userRepresentation';
import UserConsentRepresentation from '../defs/userConsentRepresentation';
import UserSessionRepresentation from '../defs/userSessionRepresentation';
import {KeycloakAdminClient} from '../client';
import MappingsRepresentation from '../defs/mappingsRepresentation';
import RoleRepresentation, {
RoleMappingPayload,
} from '../defs/roleRepresentation';
import {RequiredActionAlias} from '../defs/requiredActionProviderRepresentation';
import FederatedIdentityRepresentation from '../defs/federatedIdentityRepresentation';
import GroupRepresentation from '../defs/groupRepresentation';
import CredentialRepresentation from '../defs/credentialRepresentation';
export interface UserQuery {
email?: string;
first?: number;
firstName?: string;
lastName?: string;
max?: number;
search?: string;
username?: string;
}
export class Users extends Resource<{realm?: string}> {
public find = this.makeRequest<UserQuery, UserRepresentation[]>({
method: 'GET',
});
public create = this.makeRequest<UserRepresentation, {id: string}>({
method: 'POST',
returnResourceIdInLocationHeader: {field: 'id'},
});
/**
* Single user
*/
public findOne = this.makeRequest<{id: string}, UserRepresentation>({
method: 'GET',
path: '/{id}',
urlParamKeys: ['id'],
catchNotFound: true,
});
public update = this.makeUpdateRequest<
{id: string},
UserRepresentation,
void
>({
method: 'PUT',
path: '/{id}',
urlParamKeys: ['id'],
});
public del = this.makeRequest<{id: string}, void>({
method: 'DELETE',
path: '/{id}',
urlParamKeys: ['id'],
});
/**
* role mappings
*/
public listRoleMappings = this.makeRequest<
{id: string},
MappingsRepresentation
>({
method: 'GET',
path: '/{id}/role-mappings',
urlParamKeys: ['id'],
});
public addRealmRoleMappings = this.makeRequest<
{id: string; roles: RoleMappingPayload[]},
void
>({
method: 'POST',
path: '/{id}/role-mappings/realm',
urlParamKeys: ['id'],
payloadKey: 'roles',
});
public listRealmRoleMappings = this.makeRequest<
{id: string},
RoleRepresentation[]
>({
method: 'GET',
path: '/{id}/role-mappings/realm',
urlParamKeys: ['id'],
});
public delRealmRoleMappings = this.makeRequest<
{id: string; roles: RoleMappingPayload[]},
void
>({
method: 'DELETE',
path: '/{id}/role-mappings/realm',
urlParamKeys: ['id'],
payloadKey: 'roles',
});
public listAvailableRealmRoleMappings = this.makeRequest<
{id: string},
RoleRepresentation[]
>({
method: 'GET',
path: '/{id}/role-mappings/realm/available',
urlParamKeys: ['id'],
});
// Get effective realm-level role mappings This will recurse all composite roles to get the result.
public listCompositeRealmRoleMappings = this.makeRequest<
{id: string},
RoleRepresentation[]
>({
method: 'GET',
path: '/{id}/role-mappings/realm/composite',
urlParamKeys: ['id'],
});
/**
* Client role mappings
* https://www.keycloak.org/docs-api/4.1/rest-api/#_client_role_mappings_resource
*/
public listClientRoleMappings = this.makeRequest<
{id: string; clientUniqueId: string},
RoleRepresentation[]
>({
method: 'GET',
path: '/{id}/role-mappings/clients/{clientUniqueId}',
urlParamKeys: ['id', 'clientUniqueId'],
});
public addClientRoleMappings = this.makeRequest<
{id: string; clientUniqueId: string; roles: RoleMappingPayload[]},
void
>({
method: 'POST',
path: '/{id}/role-mappings/clients/{clientUniqueId}',
urlParamKeys: ['id', 'clientUniqueId'],
payloadKey: 'roles',
});
public delClientRoleMappings = this.makeRequest<
{id: string; clientUniqueId: string; roles: RoleMappingPayload[]},
void
>({
method: 'DELETE',
path: '/{id}/role-mappings/clients/{clientUniqueId}',
urlParamKeys: ['id', 'clientUniqueId'],
payloadKey: 'roles',
});
public listAvailableClientRoleMappings = this.makeRequest<
{id: string; clientUniqueId: string},
RoleRepresentation[]
>({
method: 'GET',
path: '/{id}/role-mappings/clients/{clientUniqueId}/available',
urlParamKeys: ['id', 'clientUniqueId'],
});
/**
* Send a update account email to the user
* an email contains a link the user can click to perform a set of required actions.
*/
public executeActionsEmail = this.makeRequest<
{
id: string;
clientId?: string;
lifespan?: number;
redirectUri?: string;
actions?: RequiredActionAlias[];
},
void
>({
method: 'PUT',
path: '/{id}/execute-actions-email',
urlParamKeys: ['id'],
payloadKey: 'actions',
queryParamKeys: ['lifespan', 'redirectUri', 'clientId'],
keyTransform: {
clientId: 'client_id',
redirectUri: 'redirect_uri',
},
});
/**
* Group
*/
public listGroups = this.makeRequest<{id: string}, GroupRepresentation[]>({
method: 'GET',
path: '/{id}/groups',
urlParamKeys: ['id'],
});
public addToGroup = this.makeRequest<
{id: string; groupId: string},
GroupRepresentation[]
>({
method: 'PUT',
path: '/{id}/groups/{groupId}',
urlParamKeys: ['id', 'groupId'],
});
public delFromGroup = this.makeRequest<
{id: string; groupId: string},
GroupRepresentation[]
>({
method: 'DELETE',
path: '/{id}/groups/{groupId}',
urlParamKeys: ['id', 'groupId'],
});
/**
* Federated Identity
*/
public listFederatedIdentities = this.makeRequest<
{id: string},
FederatedIdentityRepresentation[]
>({
method: 'GET',
path: '/{id}/federated-identity',
urlParamKeys: ['id'],
});
public addToFederatedIdentity = this.makeRequest<
{
id: string;
federatedIdentityId: string;
federatedIdentity: FederatedIdentityRepresentation;
},
void
>({
method: 'POST',
path: '/{id}/federated-identity/{federatedIdentityId}',
urlParamKeys: ['id', 'federatedIdentityId'],
payloadKey: 'federatedIdentity',
});
public delFromFederatedIdentity = this.makeRequest<
{id: string; federatedIdentityId: string},
void
>({
method: 'DELETE',
path: '/{id}/federated-identity/{federatedIdentityId}',
urlParamKeys: ['id', 'federatedIdentityId'],
});
/**
* remove totp
*/
public removeTotp = this.makeRequest<{id: string}, void>({
method: 'PUT',
path: '/{id}/remove-totp',
urlParamKeys: ['id'],
});
/**
* reset password
*/
public resetPassword = this.makeRequest<
{id: string; credential: CredentialRepresentation},
void
>({
method: 'PUT',
path: '/{id}/reset-password',
urlParamKeys: ['id'],
payloadKey: 'credential',
});
/**
* send verify email
*/
public sendVerifyEmail = this.makeRequest<
{id: string; clientId?: string; redirectUri?: string},
void
>({
method: 'PUT',
path: '/{id}/send-verify-email',
urlParamKeys: ['id'],
queryParamKeys: ['clientId', 'redirectUri'],
keyTransform: {
clientId: 'client_id',
redirectUri: 'redirect_uri',
},
});
/**
* list user sessions
*/
public listSessions = this.makeRequest<
{id: string},
UserSessionRepresentation[]
>({
method: 'GET',
path: '/{id}/sessions',
urlParamKeys: ['id'],
});
/**
* list offline sessions associated with the user and client
*/
public listOfflineSessions = this.makeRequest<
{id: string, clientId: string},
UserSessionRepresentation[]
>({
method: 'GET',
path: '/{id}/offline-sessions/{clientId}',
urlParamKeys: ['id', 'clientId'],
});
/**
* logout user from all sessions
*/
public logout = this.makeRequest<
{id: string},
void
>({
method: 'POST',
path: '/{id}/logout',
urlParamKeys: ['id'],
});
/**
* list consents granted by the user
*/
public listConsents = this.makeRequest<
{id: string},
UserConsentRepresentation[]
>({
method: 'GET',
path: '/{id}/consents',
urlParamKeys: ['id'],
});
/**
* revoke consent and offline tokens for particular client from user
*/
public revokeConsent = this.makeRequest<
{id: string, clientId: string},
void
>({
method: 'DELETE',
path: '/{id}/consents/{clientId}',
urlParamKeys: ['id', 'clientId'],
});
constructor(client: KeycloakAdminClient) {
super(client, {
path: '/admin/realms/{realm}/users',
getUrlParams: () => ({
realm: client.realmName,
}),
getBaseUrl: () => client.baseUrl,
});
}
}