-
Notifications
You must be signed in to change notification settings - Fork 6
/
UserService.ts
459 lines (439 loc) · 17.2 KB
/
UserService.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import {
UserEntity,
ResetPasswordEntity,
FindUserEntity,
IUserLinkEntity,
ChangePasswordEntity,
ValidateResetPasswordEntity,
AcceptResetPasswordEntity
} from "./Entities"
import { Helper, CustomException } from "./Helper";
export namespace UserService {
/**
* To get the user profile information by using cidaas internal api, call **getUserProfile()**.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/2zfvjx3vtq6g6-get-user-info for more details.
* @example
* ```js
* const options = {
* access_token: 'your access token'
* }
* cidaas.getUserProfile(options)
* .then(function () {
* // the response will give you user profile information.
* }).catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function getUserProfile(options: { access_token: string }) {
if (!options.access_token) {
throw new CustomException("access_token cannot be empty", 417);
}
const _serviceURL = window.webAuthSettings.authority + "/users-srv/userinfo";
return Helper.createHttpPromise(undefined, _serviceURL, undefined, "GET", options.access_token);
};
/**
* To register user, call **register()**. This method will create a new user.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/427632e587203-register-a-new-user for more details.
* Note: Only requestId in the headers is required.
* @example
* ```js
* const headers = {
* requestId: 'your_received_requestId',
* captcha: 'captcha',
* acceptlanguage: 'acceptlanguage',
* bot_captcha_response: 'bot_captcha_response'
* };
*
* cidaas.register({
* email: 'xxx123@xxx.com',
* given_name: 'xxxxx',
* family_name: 'yyyyy',
* password: '123456',
* password_echo: '123456',
* provider: 'your provider', // FACEBOOK, GOOGLE, SELF
* acceptlanguage: 'your locale' // optional example: de-de, en-US
* }, headers).then(function (response) {
* // the response will give you client registration details.
* }).catch(function(ex) {
* // your failure code here
* });
*```
*/
export function register(options: UserEntity, headers: {
requestId: string;
captcha?: string;
acceptlanguage?: string;
bot_captcha_response?: string;
trackId?: string;
}) {
let _serviceURL = window.webAuthSettings.authority + "/users-srv/register";
if (options.invite_id) {
_serviceURL = _serviceURL + "?invite_id=" + options.invite_id;
}
return Helper.createHttpPromise(options, _serviceURL, false, "POST", undefined, headers);
};
/**
* to get information about invitation details, call **getInviteUserDetails()**. This API allows to retrieve invitation details and prefill the registration form.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/0b5efa5a2db5d-prefill-the-user-invitation for more details.
* Minimum cidaas version to use latest api is v3.100
* @example
* ```js
* const options = {
* invite_id: 'id of user invitation'
* callLatestAPI: 'true' // call latest api if parameter is given. By default, the older api will be called
* }
* cidaas.getInviteUserDetails(options)
* .then(function () {
* // the response will give you information about the invitation.
* }).catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function getInviteUserDetails(options: { invite_id: string, callLatestAPI?: boolean }) {
let _serviceURL: string = "";
if(options.callLatestAPI){
_serviceURL = window.webAuthSettings.authority + "/useractions-srv/invitations/" + options.invite_id;
}else{
_serviceURL = window.webAuthSettings.authority + "/users-srv/invite/info/" + options.invite_id;
}
return Helper.createHttpPromise(undefined, _serviceURL, false, "GET");
};
/**
* Once registration successful, verify the account based on the flow. To get the details, call **getCommunicationStatus()**.
* @example
* ```js
* cidaas.getCommunicationStatus({
* sub: 'your sub', // which you will get on the registration response
* }).then(function (response) {
* // the response will give you account details once its verified.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function getCommunicationStatus(options: { sub: string }, headers?: {requestId: string }) {
let _serviceURL = window.webAuthSettings.authority + "/users-srv/user/communication/status/" + options.sub;
return Helper.createHttpPromise(undefined, _serviceURL, false, "GET", undefined, headers);
};
/**
* To initiate the password resetting, call **initiateResetPassword()**. This will send verification code to your email or mobile based on the resetMedium you mentioned.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/6b29bac6002f4-initiate-password-reset for more details.
* @example
* ```js
* cidaas.initiateResetPassword({
* email: 'xxxxxx@xxx.com',
* processingType: 'CODE',
* requestId: 'your requestId',
* resetMedium: 'email'
* }).then(function (response) {
* // the response will give you password reset details.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function initiateResetPassword(options: ResetPasswordEntity) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/resetpassword/initiate";
return Helper.createHttpPromise(options, _serviceURL, false, "POST");
};
/**
* To handle the reset password by entering the verification code you received, call **handleResetPassword()**. This will check if your verification code was valid or not, and allows you to proceed to the next step.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/3t8ztokeb7cfz-handle-reset-password for more details.
* @example
* ```js
* const handleResponseAsJson = 'true if the response need to be handled the old way (as json). In the current handling, the response information will be given as query parameter in redirect url.';
* cidaas.handleResetPassword({
* code: 'your code in email or sms or ivr',
* resetRequestId: 'your resetRequestId' // which you will get on initiate reset password response
* }, handleResponseAsJson).then(function (response) {
* // the response will give you valid verification code.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function handleResetPassword(options: ValidateResetPasswordEntity, handleResponseAsJson?: boolean) {
try {
const url = window.webAuthSettings.authority + "/users-srv/resetpassword/validatecode";
if (!handleResponseAsJson) {
// current handling will redirect and give query parameters
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} else {
// older cidaas service handling return json object
return Helper.createHttpPromise(options, url, false, "POST");
}
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To finish reseting the password, call **resetPassword()**. This will allow you to change your password.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/qa9ny0gkzlf6y-accept-reset-password for more details.
* @example
* ```js
* const handleResponseAsJson = 'true if the response need to be handled the old way (as json). In the current handling, user will be redirected to success page after successful reset password.';
* cidaas.resetPassword({
* password: '123456',
* confirmPassword: '123456',
* exchangeId: 'your exchangeId', // which you will get on handle reset password response
* resetRequestId: 'your resetRequestId' // which you will get on handle reset password response
* }).then(function (response) {
* // the response will give you reset password details.
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function resetPassword(options: AcceptResetPasswordEntity, handleResponseAsJson?: boolean) {
const url = window.webAuthSettings.authority + "/users-srv/resetpassword/accept";
try {
if (!handleResponseAsJson) {
// current handling will redirect and give query parameters
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} else {
// older cidaas service handling return json object
return Helper.createHttpPromise(options, url, false, "POST");
}
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To get the list of existing users in deduplication, call **getDeduplicationDetails()**.
* @example
* ```js
* this.cidaas.getDeduplicationDetails({
* track_id: 'your track id'
* }).then((response) => {
* // the response will give you deduplication details of users.
* }).catch((err) => {
* // your failure code here
* });
* ```
*/
export function getDeduplicationDetails(options: { trackId: string }) {
const _serviceURL = window.webAuthSettings.authority + "/users-srv/deduplication/info/" + options.trackId;
return Helper.createHttpPromise(options, _serviceURL, false, "GET");
};
/**
* To use the existing users in deduplication, you need to call **deduplicationLogin()**.
* @example
* ```js
* this.cidaas.deduplicationLogin({
* sub: 'your sub',
* requestId: 'request id from deduplication initialisation after register',
* trackId: 'track id from deduplication initialisation after register'
* })
* ```
*/
export function deduplicationLogin(options: { trackId: string, requestId: string, sub: string }) {
try {
const url = window.webAuthSettings.authority + "/users-srv/deduplication/login/redirection?trackId=" + options.trackId + "&requestId=" + options.requestId + "&sub=" + options.sub;
const form = Helper.createForm(url, {});
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To register new user in deduplication, call **registerDeduplication()**.
* @example
* ```js
* this.cidaas.registerDeduplication({
* track_id: 'track id from deduplication initialisation after register',
* }).then((response) => {
* // the response will give you new registered deduplication user.
* }).catch((err) => {
* // your failure code here
* });
* ```
*/
export function registerDeduplication(options: { trackId: string }) {
const _serviceURL = window.webAuthSettings.authority + "/users-srv/deduplication/register/" + options.trackId;
return Helper.createHttpPromise(undefined, _serviceURL, undefined, "POST");
};
/**
* To change the password, call **changePassword()**. This will allow you to change your password.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/8221883241464-change-password for more details.
* @example
* ```js
* cidaas.changePassword({
* old_password: 'your old password',
* new_password: 'your new password',
* confirm_password: 'your new password',
* sub: 'your sub',
* }, 'your access token')
* .then(function () {
* // your success code
* }).catch(function (ex) {
* // your failure code
* });
* ```
*/
export function changePassword(options: ChangePasswordEntity, access_token: string) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/changepassword";
return Helper.createHttpPromise(options, _serviceURL, false, "POST", access_token);
};
/**
* To update the user profile information, call **updateProfile()**.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/i3uqnxcpxr19r-update-user-profile for more details.
* @example
* ```js
* cidaas.updateProfile({
* family_name: 'Doe',
* given_name: 'John',
* provider: 'self',
* acceptlanguage: 'your locale' // optional example: de-de, en-US
* }, 'your access token', 'your sub').then(function () {
* // the response will give you updated user profile info.
* }).catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function updateProfile(options: UserEntity, access_token: string, sub: string) {
const _serviceURL = window.webAuthSettings.authority + "/users-srv/user/profile/" + sub;
return Helper.createHttpPromise(options, _serviceURL, false, "PUT", access_token);
};
/**
* To initiate account linking, call **initiateLinkAccount()**.
* @example
* ```js
* const options = {
* master_sub: 'sub of the user who initiates the user link',
* user_name_to_link: 'username of the user which should get linked',
* user_name_type: 'type of user name to link. E.g. email'
* }
* const access_token = 'your access token'
* this.cidaas.initiateLinkAccount(options, access_token).then((response) => {
* // your success code
* }).catch((err) => {
* // your failure code here
* });
* ```
*/
export function initiateLinkAccount(options: IUserLinkEntity, access_token: string) {
options.user_name_type = 'email';
var _serviceURL = window.webAuthSettings.authority + "/users-srv/user/link/initiate";
return Helper.createHttpPromise(options, _serviceURL, false, "POST", access_token);
};
/**
* To complete account linking, call **completeLinkAccount()**.
* @example
* ```js
* const options = {
* code: 'code which is sent to account to be linked',
* link_request_id: 'comes from initiateLinkAccount'
* }
* const access_token = 'your access token'
* this.cidaas.completeLinkAccount(options, access_token).then((response) => {
* // your success code
* }).catch((err) => {
* // your failure code here
* });
* ```
*/
export function completeLinkAccount(options: { code?: string; link_request_id?: string; }, access_token: string) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/user/link/complete";
return Helper.createHttpPromise(options, _serviceURL, false, "POST", access_token);
};
/**
* To get all the linked accounts, call **getLinkedUsers()**.
* @example
* ```js
* const acccess_token= 'your access token';
* const sub = 'your sub';
*
* cidaas.getLinkedUsers(access_token, sub)
* .then(function (response) {
* // type your code here
* })
* .catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function getLinkedUsers(access_token: string, sub: string) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/userinfo/social/" + sub;
return Helper.createHttpPromise(undefined, _serviceURL, false, "GET", access_token);
};
/**
* To unlink an account for a user, call **unlinkAccount()**.
* @example
* ```js
* const acccess_token= "your access token";
* const identityId = "comes from getLinkedUsers";
*
* cidaas.unlinkAccount(access_token, identityId)
* .then(function (response) {
* // type your code here
* })
* .catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function unlinkAccount(access_token: string, identityId: string) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/user/unlink/" + identityId;
return Helper.createHttpPromise(undefined, _serviceURL, false, "POST", access_token);
};
/**
* To delete the user account directly in the application, call **deleteUserAccount()**.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/x133xdifl1sx9-schedule-user-deletion for more details.
* @example
* ```js
* options = {
* access_token: "your access token",
* sub: "your sub"
* }
*
* cidaas.deleteUserAccount(options).then(function (response) {
* // your success code
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function deleteUserAccount(options: { access_token: string, sub: string }) {
var _serviceURL = window.webAuthSettings.authority + "/users-srv/user/unregister/scheduler/schedule/" + options.sub;
return Helper.createHttpPromise(options, _serviceURL, undefined, "POST", options.access_token);
};
/**
* To check if user exists, call **userCheckExists()**.
* @example
* options = {
* requestId: "your request id",
* email: "your email"
* }
*
* cidaas.userCheckExists(options).then(function (response) {
* // your success code
* }).catch(function(ex) {
* // your failure code here
* });
* ```
*/
export function userCheckExists(options: FindUserEntity) {
let queryParameter = ''
if (options.webfinger || options.rememberMe) {
queryParameter += '?';
if (options.webfinger) {
queryParameter += 'webfinger=' + options.webfinger;
if (options.rememberMe) {
queryParameter += '&rememberMe=' + options.rememberMe;
}
} else if (options.rememberMe) {
queryParameter += 'rememberMe=' + options.rememberMe;
}
}
var _serviceURL = window.webAuthSettings.authority + "/useractions-srv/userexistence/" + options.requestId + queryParameter;
return Helper.createHttpPromise(options, _serviceURL, undefined, "POST");
};
}