@cloudbase/oauth 0.1.1-alpha → 1.0.0-alpha.3

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.
Files changed (97) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +26 -0
  3. package/Dockerfile +15 -0
  4. package/README.md +130 -100
  5. package/_exmaple/assets/scripts/function/function.ts +99 -0
  6. package/_exmaple/assets/scripts/index.ts +101 -0
  7. package/_exmaple/assets/scripts/request.ts +11 -0
  8. package/_exmaple/index.html +15 -0
  9. package/_exmaple/package.json +33 -0
  10. package/_exmaple/tsconfig.json +71 -0
  11. package/_exmaple/typings.d.ts +0 -0
  12. package/_exmaple/webpack.config.js +42 -0
  13. package/dist/auth/apis.d.ts +55 -0
  14. package/dist/auth/apis.js +570 -0
  15. package/{auth → dist/auth}/consts.d.ts +7 -2
  16. package/dist/auth/consts.js +61 -0
  17. package/dist/auth/models.d.ts +355 -0
  18. package/dist/auth/models.js +3 -0
  19. package/dist/captcha/captcha.d.ts +35 -0
  20. package/dist/captcha/captcha.js +267 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.js +63 -0
  23. package/{oauthclient → dist/oauth2client}/consts.d.ts +22 -1
  24. package/dist/oauth2client/consts.js +73 -0
  25. package/dist/oauth2client/interface.d.ts +15 -0
  26. package/dist/oauth2client/interface.js +10 -0
  27. package/{oauthclient → dist/oauth2client}/models.d.ts +13 -5
  28. package/dist/oauth2client/models.js +3 -0
  29. package/dist/oauth2client/oauth2client.d.ts +71 -0
  30. package/dist/oauth2client/oauth2client.js +634 -0
  31. package/dist/utils/function/single-promise.d.ts +5 -0
  32. package/dist/utils/function/single-promise.js +89 -0
  33. package/dist/utils/uuid.d.ts +1 -0
  34. package/dist/utils/uuid.js +12 -0
  35. package/package.json +28 -29
  36. package/publish.sh +2 -0
  37. package/src/auth/apis.ts +644 -0
  38. package/src/auth/consts.ts +57 -0
  39. package/src/auth/models.ts +445 -0
  40. package/src/captcha/captcha.ts +222 -0
  41. package/src/index.ts +49 -0
  42. package/src/oauth2client/consts.ts +69 -0
  43. package/src/oauth2client/interface.ts +57 -0
  44. package/src/oauth2client/models.ts +61 -0
  45. package/src/oauth2client/oauth2client.ts +615 -0
  46. package/src/utils/function/single-promise.ts +40 -0
  47. package/src/utils/uuid.ts +11 -0
  48. package/tsconfig.json +44 -0
  49. package/wiki/README.md +75 -0
  50. package/app/index.d.ts +0 -28
  51. package/app/index.js +0 -54
  52. package/app/index.js.map +0 -1
  53. package/app/internal.d.ts +0 -12
  54. package/app/internal.js +0 -25
  55. package/app/internal.js.map +0 -1
  56. package/app/openuri.d.ts +0 -20
  57. package/app/openuri.js +0 -104
  58. package/app/openuri.js.map +0 -1
  59. package/app/request.d.ts +0 -18
  60. package/app/request.js +0 -44
  61. package/app/request.js.map +0 -1
  62. package/app/storage.d.ts +0 -41
  63. package/app/storage.js +0 -35
  64. package/app/storage.js.map +0 -1
  65. package/auth/consts.js +0 -57
  66. package/auth/consts.js.map +0 -1
  67. package/auth/index.d.ts +0 -193
  68. package/auth/index.js +0 -403
  69. package/auth/index.js.map +0 -1
  70. package/auth/models.d.ts +0 -158
  71. package/auth/models.js +0 -3
  72. package/auth/models.js.map +0 -1
  73. package/captcha/index.d.ts +0 -45
  74. package/captcha/index.js +0 -133
  75. package/captcha/index.js.map +0 -1
  76. package/index.d.ts +0 -8
  77. package/index.js +0 -17
  78. package/index.js.map +0 -1
  79. package/oauthclient/consts.js +0 -53
  80. package/oauthclient/consts.js.map +0 -1
  81. package/oauthclient/index.d.ts +0 -18
  82. package/oauthclient/index.js +0 -21
  83. package/oauthclient/index.js.map +0 -1
  84. package/oauthclient/interface.d.ts +0 -25
  85. package/oauthclient/interface.js +0 -10
  86. package/oauthclient/interface.js.map +0 -1
  87. package/oauthclient/models.js +0 -3
  88. package/oauthclient/models.js.map +0 -1
  89. package/oauthclient/oauthclient.d.ts +0 -142
  90. package/oauthclient/oauthclient.js +0 -399
  91. package/oauthclient/oauthclient.js.map +0 -1
  92. package/utils/single-promise.d.ts +0 -18
  93. package/utils/single-promise.js +0 -48
  94. package/utils/single-promise.js.map +0 -1
  95. package/utils/uuid.d.ts +0 -5
  96. package/utils/uuid.js +0 -16
  97. package/utils/uuid.js.map +0 -1
@@ -0,0 +1,644 @@
1
+ 'use strict';
2
+
3
+ import { ApiUrls } from './consts';
4
+ import {
5
+ GetVerificationRequest,
6
+ GetVerificationResponse,
7
+ UserProfile,
8
+ UserInfo,
9
+ SignInRequest,
10
+ SignUpRequest,
11
+ VerifyRequest,
12
+ VerifyResponse,
13
+ GenProviderRedirectUriRequest,
14
+ GenProviderRedirectUriResponse,
15
+ GrantProviderTokenRequest,
16
+ GrantProviderTokenResponse,
17
+ PatchProviderTokenRequest,
18
+ PatchProviderTokenResponse,
19
+ SignInWithProviderRequest,
20
+ BindWithProviderRequest,
21
+ TransByProviderRequest,
22
+ GrantTokenRequest,
23
+ UserProfileProvider,
24
+ UnbindProviderRequest,
25
+ CheckPasswordrRequest,
26
+ BindPhoneRequest,
27
+ BindEmailRequest,
28
+ SetPasswordRequest,
29
+ ChangeBindedProviderRequest,
30
+ ChangeBindedProviderResponse,
31
+ UpdatePasswordRequest,
32
+ SudoResponse,
33
+ SudoRequest,
34
+ GetCustomSignTicketFn,
35
+ QueryUserProfileRequest,
36
+ QueryUserProfileResponse,
37
+ ResetPasswordRequest,
38
+ DeviceAuthorizeRequest,
39
+ DeviceAuthorizeResponse,
40
+ CheckUsernameRequest,
41
+ CheckIfUserExistRequest,
42
+ CheckIfUserExistResponse,
43
+ WithSudoRequest
44
+ } from './models';
45
+ import { SimpleStorage, RequestFunction } from '../oauth2client/interface';
46
+ import { OAuth2Client, defaultStorage } from '../oauth2client/oauth2client';
47
+ import { Credentials } from '../oauth2client/models';
48
+ import { Captcha } from '../captcha/captcha';
49
+
50
+
51
+ export interface AuthOptions {
52
+ apiOrigin: string;
53
+ clientId: string;
54
+ credentialsClient?: OAuth2Client;
55
+ request?: RequestFunction;
56
+ storage?: SimpleStorage;
57
+ }
58
+
59
+ /**
60
+ * Auth
61
+ */
62
+ export class Auth {
63
+ private _config: AuthOptions;
64
+ private _getCustomSignTicketFn?: GetCustomSignTicketFn;
65
+
66
+
67
+ /**
68
+ * constructor
69
+ * @param {AuthOptions} opts
70
+ */
71
+ constructor(opts: AuthOptions) {
72
+ let request = opts.request;
73
+ let oAuth2Client = opts.credentialsClient;
74
+ if (!oAuth2Client) {
75
+ const initOptions = {
76
+ apiOrigin: opts.apiOrigin,
77
+ clientId: opts.clientId,
78
+ storage: opts.storage,
79
+ };
80
+ oAuth2Client = new OAuth2Client(initOptions);
81
+ }
82
+ if (!request) {
83
+ const baseRequest = oAuth2Client.request.bind(oAuth2Client);
84
+ const captcha = new Captcha({
85
+ clientId: opts.clientId,
86
+ request: baseRequest,
87
+ storage: opts.storage,
88
+ })
89
+ request = captcha.request.bind(captcha)
90
+ }
91
+ this._config = {
92
+ apiOrigin: opts.apiOrigin,
93
+ clientId: opts.clientId,
94
+ request: request,
95
+ credentialsClient: oAuth2Client,
96
+ storage: opts.storage || defaultStorage,
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Sign in.
102
+ * @param {SignInRequest} params A SignInRequest Object.
103
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
104
+ */
105
+ public async signIn(params: SignInRequest): Promise<Credentials> {
106
+ const credentials: Credentials = await this._config.request<Credentials>(
107
+ ApiUrls.AUTH_SIGN_IN_URL,
108
+ {
109
+ method: 'POST',
110
+ body: params
111
+ },
112
+ );
113
+ await this._config.credentialsClient.setCredentials(credentials);
114
+ return Promise.resolve(credentials);
115
+ }
116
+
117
+ /**
118
+ * Sign in Anonymously
119
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
120
+ */
121
+ public async signInAnonymously(): Promise<Credentials> {
122
+ const credentials: Credentials = await this._config.request<Credentials>(
123
+ ApiUrls.AUTH_SIGN_IN_ANONYMOUSLY_URL,
124
+ {
125
+ method: 'POST',
126
+ body: {}
127
+ },
128
+ );
129
+ await this._config.credentialsClient.setCredentials(credentials);
130
+ return Promise.resolve(credentials);
131
+ }
132
+
133
+ /**
134
+ * Sign up.
135
+ * @param {SignUpRequest} params A SignUpRequest Object.
136
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
137
+ */
138
+ public async signUp(params: SignUpRequest): Promise<Credentials> {
139
+ const data: Credentials = await this._config.request<Credentials>(
140
+ ApiUrls.AUTH_SIGN_UP_URL,
141
+ {
142
+ method: 'POST',
143
+ body: params,
144
+ },
145
+ );
146
+ await this._config.credentialsClient.setCredentials(data);
147
+ return Promise.resolve(data);
148
+ }
149
+
150
+ /**
151
+ * Sign out.
152
+ * @return {Object} A Promise<void> object.
153
+ */
154
+ public async signOut(): Promise<any> {
155
+ const accessToken: string = await this._config.credentialsClient.getAccessToken();
156
+ const data = await this._config.request(ApiUrls.AUTH_REVOKE_URL, {
157
+ method: 'POST',
158
+ body: {
159
+ token: accessToken,
160
+ },
161
+ });
162
+ await this._config.credentialsClient.setCredentials();
163
+ return Promise.resolve(data);
164
+ }
165
+
166
+ /**
167
+ * Get the verification.
168
+ * @param {GetVerificationRequest} params A GetVerificationRequest Object.
169
+ * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
170
+ */
171
+ public async getVerification(
172
+ params: GetVerificationRequest,
173
+ ): Promise<GetVerificationResponse> {
174
+ let withCredentials = false;
175
+ // 发送短信时,如果时给当前用户发,则需要带上鉴权信息
176
+ if (params.target == 'CUR_USER') {
177
+ withCredentials = true
178
+ } else {
179
+ const hasLogin = await this.hasLoginState()
180
+ if (hasLogin) {
181
+ withCredentials = true
182
+ }
183
+ }
184
+ return this._config.request<GetVerificationResponse>(
185
+ ApiUrls.VERIFICATION_URL,
186
+ {
187
+ method: 'POST',
188
+ body: params,
189
+ withCaptcha: true,
190
+ withCredentials: withCredentials,
191
+ },
192
+ );
193
+ }
194
+
195
+ /**
196
+ * Verify the code
197
+ * @param {VerifyRequest} params A VerifyRequest Object.
198
+ * @return {Promise<VerifyResponse>} A Promise<VerifyResponse> object.
199
+ */
200
+ public async verify(params: VerifyRequest): Promise<VerifyResponse> {
201
+ return this._config.request<VerifyResponse>(ApiUrls.VERIFY_URL, {
202
+ method: 'POST',
203
+ body: params,
204
+ });
205
+ }
206
+
207
+ /**
208
+ * Gen provider redirect uri.
209
+ * @param {GenProviderRedirectUriRequest} params A GenProviderRedirectUriRequest object.
210
+ * @return {Promise<GenProviderRedirectUriResponse>} A Promise<GenProviderRedirectUriResponse> object.
211
+ */
212
+ public async genProviderRedirectUri(
213
+ params: GenProviderRedirectUriRequest,
214
+ ): Promise<GenProviderRedirectUriResponse> {
215
+ let url = `${ApiUrls.PROVIDER_URI_URL}?client_id=${this._config.clientId
216
+ }&provider_id=${params.provider_id}&redirect_uri=${encodeURIComponent(
217
+ params.provider_redirect_uri,
218
+ )}&state=${params.state}`;
219
+ const other_params = params.other_params;
220
+ if (other_params) {
221
+ if (
222
+ typeof other_params.sign_out_uri === 'string' &&
223
+ other_params.sign_out_uri.length > 0
224
+ ) {
225
+ url += `&other_params[sign_out_uri]=${other_params.sign_out_uri}`;
226
+ }
227
+ }
228
+ return this._config.request<GenProviderRedirectUriResponse>(url, {
229
+ method: 'GET',
230
+ });
231
+ }
232
+
233
+ /**
234
+ * Grant provider token.
235
+ * @param {GrantProviderTokenRequest} params A GrantProviderTokenRequest object.
236
+ * @return {Promise<GrantProviderTokenResponse>} A Promise<GrantProviderTokenResponse> object.
237
+ */
238
+ public async grantProviderToken(
239
+ params: GrantProviderTokenRequest,
240
+ ): Promise<GrantProviderTokenResponse> {
241
+ return this._config.request<GrantProviderTokenResponse>(
242
+ ApiUrls.PROVIDER_TOKEN_URL,
243
+ {
244
+ method: 'POST',
245
+ body: params,
246
+ },
247
+ );
248
+ }
249
+
250
+ /**
251
+ * Grant provider token.
252
+ * @param {PatchProviderTokenRequest} params A PatchProviderTokenRequest object.
253
+ * @return {Promise<PatchProviderTokenResponse>} A Promise<PatchProviderTokenResponse> object.
254
+ */
255
+ public async patchProviderToken(
256
+ params: PatchProviderTokenRequest,
257
+ ): Promise<PatchProviderTokenResponse> {
258
+ return this._config.request<PatchProviderTokenResponse>(
259
+ ApiUrls.PROVIDER_TOKEN_URL,
260
+ {
261
+ method: 'PATCH',
262
+ body: params,
263
+ },
264
+ );
265
+ }
266
+
267
+ /**
268
+ * Signin with provider request.
269
+ * @param {SignInWithProviderRequest} params A SignInWithProviderRequest object.
270
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
271
+ */
272
+ public async signInWithProvider(
273
+ params: SignInWithProviderRequest,
274
+ ): Promise<Credentials> {
275
+ const credentials: Credentials = await this._config.request<Credentials>(
276
+ ApiUrls.AUTH_SIGN_IN_WITH_PROVIDER_URL,
277
+ {
278
+ method: 'POST',
279
+ body: params,
280
+ },
281
+ );
282
+ await this._config.credentialsClient.setCredentials(credentials);
283
+ return Promise.resolve(credentials);
284
+ }
285
+
286
+ /**
287
+ * Bind with provider
288
+ * @param {BindWithProviderRequest} params A BindWithProviderRequest object.
289
+ * @return {Promise<void>} A Promise<any> object.
290
+ */
291
+ public async bindWithProvider(
292
+ params: BindWithProviderRequest,
293
+ ): Promise<void> {
294
+ return this._config.request<any>(ApiUrls.PROVIDER_BIND_URL, {
295
+ method: 'POST',
296
+ body: params,
297
+ withCredentials: true,
298
+ });
299
+ }
300
+
301
+ /**
302
+ * Get the user profile.
303
+ * @return {Promise<UserProfile>} A Promise<UserProfile> object.
304
+ */
305
+ public async getUserProfile(): Promise<UserProfile> {
306
+ return this.getUserInfo();
307
+ }
308
+
309
+ /**
310
+ * Get the user info.
311
+ * @return {Promise<UserInfo>} A Promise<UserProfile> object.
312
+ */
313
+ public async getUserInfo(): Promise<UserInfo> {
314
+ const userInfo = await this._config.request<UserInfo>(ApiUrls.USER_ME_URL, {
315
+ method: 'GET',
316
+ withCredentials: true,
317
+ });
318
+ if (userInfo.picture) {
319
+ userInfo.avatarUrl = userInfo.picture;
320
+ }
321
+
322
+ if (userInfo.sub) {
323
+ userInfo.uid = userInfo.sub
324
+ }
325
+
326
+ return userInfo;
327
+ }
328
+
329
+ /**
330
+ * Delete me
331
+ * @param params
332
+ */
333
+ public async deleteMe(params: WithSudoRequest): Promise<UserProfile> {
334
+ const url = `${ApiUrls.USER_ME_URL}?${Auth.parseParamsToSearch(params)}`;
335
+ return this._config.request<UserProfile>(url, {
336
+ method: 'DELETE',
337
+ withCredentials: true,
338
+ });
339
+ }
340
+
341
+ /**
342
+ * hasLoginState check if has login state
343
+ * @return {Promise<boolean>} A Promise<boolean> object.
344
+ */
345
+ public async hasLoginState(): Promise<boolean> {
346
+ try {
347
+ await this._config.credentialsClient.getAccessToken()
348
+ return true
349
+ } catch (error) {
350
+ return false
351
+ }
352
+ }
353
+
354
+ public hasLoginStateSync(): Credentials | null {
355
+ const credentials = this._config.credentialsClient.getCredentialsSync()
356
+ return credentials
357
+ }
358
+
359
+ public async getLoginState(): Promise<Credentials | null> {
360
+ return this._config.credentialsClient.getCredentialsAsync()
361
+ }
362
+
363
+ /**
364
+ * Trans by provider.
365
+ * @param {TransByProviderRequest} params A TransByProviderRequest object.
366
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
367
+ */
368
+ public async transByProvider(
369
+ params: TransByProviderRequest,
370
+ ): Promise<Credentials> {
371
+ return this._config.request<Credentials>(
372
+ ApiUrls.USER_TRANS_BY_PROVIDER_URL,
373
+ {
374
+ method: 'PATCH',
375
+ body: params,
376
+ withCredentials: true,
377
+ },
378
+ );
379
+ }
380
+
381
+ /**
382
+ * Grant token.
383
+ * @param {GrantTokenRequest} params A GrantTokenRequest object.
384
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
385
+ */
386
+ public async grantToken(params: GrantTokenRequest): Promise<Credentials> {
387
+ return this._config.request<Credentials>(ApiUrls.AUTH_TOKEN_URL, {
388
+ method: 'POST',
389
+ body: params,
390
+ });
391
+ }
392
+
393
+ /**
394
+ * Get the provide list.
395
+ * @return {Promise<UserProfileProvider>} A Promise<UserProfileProvider> object.
396
+ */
397
+ public async getProviders(): Promise<UserProfileProvider> {
398
+ return this._config.request<UserProfileProvider>(ApiUrls.PROVIDER_LIST, {
399
+ method: 'GET',
400
+ withCredentials: true,
401
+ });
402
+ }
403
+
404
+ /**
405
+ * unbind provider.
406
+ * @param {UnbindProviderRequest} params
407
+ * @return {Promise<any>}
408
+ */
409
+ public async unbindProvider(params: UnbindProviderRequest): Promise<void> {
410
+ return this._config.request<any>(
411
+ `${ApiUrls.PROVIDER_UNBIND_URL}/${params.provider_id}`,
412
+ {
413
+ method: 'DELETE',
414
+ withCredentials: true,
415
+ },
416
+ );
417
+ }
418
+
419
+ /**
420
+ * check Password.
421
+ * @param {CheckPasswordrRequest} params
422
+ * @return {Promise<any>}
423
+ */
424
+ public async checkPassword(params: CheckPasswordrRequest): Promise<void> {
425
+ return this._config.request<any>(`${ApiUrls.CHECK_PWD_URL}`, {
426
+ method: 'POST',
427
+ withCredentials: true,
428
+ body: params,
429
+ });
430
+ }
431
+
432
+ /**
433
+ * check Password.
434
+ * @param {CheckPasswordrRequest} params
435
+ * @return {Promise<any>}
436
+ */
437
+ public async bindPhone(params: BindPhoneRequest): Promise<void> {
438
+ return this._config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
439
+ method: 'PATCH',
440
+ withCredentials: true,
441
+ body: params,
442
+ });
443
+ }
444
+
445
+ /**
446
+ * check Password.
447
+ * @param {CheckPasswordrRequest} params
448
+ * @return {Promise<any>}
449
+ */
450
+ public async bindEmail(params: BindEmailRequest): Promise<void> {
451
+ return this._config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
452
+ method: 'PATCH',
453
+ withCredentials: true,
454
+ body: params,
455
+ });
456
+ }
457
+
458
+ /**
459
+ * Set Password.
460
+ * @param {SetPasswordrRequest} params
461
+ * @return {Promise<any>}
462
+ */
463
+ public async setPassword(params: SetPasswordRequest): Promise<void> {
464
+ return this._config.request<any>(`${ApiUrls.AUTH_SET_PASSWORD}`, {
465
+ method: 'PATCH',
466
+ withCredentials: true,
467
+ body: params,
468
+ });
469
+ }
470
+
471
+ /**
472
+ * updatePasswordByOld 使用旧密码修改密码,如果已经绑定手机号,请先:sudo,再修改密码
473
+ * @param {SetPasswordrRequest} params
474
+ * @return {Promise<any>}
475
+ */
476
+ public async updatePasswordByOld(params: UpdatePasswordRequest): Promise<void> {
477
+ const sudoToken = await this.sudo({ password: params.old_password })
478
+ return this.setPassword({
479
+ sudo_token: sudoToken.sudo_token,
480
+ new_password: params.new_password,
481
+ })
482
+ }
483
+
484
+
485
+ /**
486
+ * sudo
487
+ * @param {sudo} params
488
+ * @return {Promise<any>}
489
+ */
490
+ public async sudo(params: SudoRequest): Promise<SudoResponse> {
491
+ return this._config.request<SudoResponse>(`${ApiUrls.SUDO_URL}`, {
492
+ method: 'POST',
493
+ withCredentials: true,
494
+ body: params,
495
+ });
496
+ }
497
+
498
+ /**
499
+ * Get the current user verification.
500
+ * @param {GetVerificationRequest} params A GetVerificationRequest Object.
501
+ * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
502
+ */
503
+ public async getCurUserVerification(
504
+ params: GetVerificationRequest,
505
+ ): Promise<GetVerificationResponse> {
506
+ params.target = 'CUR_USER';
507
+ return this._config.request<GetVerificationResponse>(
508
+ ApiUrls.VERIFICATION_URL,
509
+ {
510
+ method: 'POST',
511
+ body: params,
512
+ withCredentials: true,
513
+ withCaptcha: true
514
+ },
515
+ );
516
+ }
517
+
518
+ /**
519
+ * change binded provider.
520
+ * @param {GetVerificationRequest} params A GetVerificationRequest Object.
521
+ * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
522
+ */
523
+ public async changeBindedProvider(
524
+ params: ChangeBindedProviderRequest,
525
+ ): Promise<ChangeBindedProviderResponse> {
526
+ return this._config.request<ChangeBindedProviderResponse>(
527
+ `${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`,
528
+ {
529
+ method: 'POST',
530
+ body: {
531
+ provider_trans_token: params.trans_token,
532
+ },
533
+ withCredentials: true,
534
+ },
535
+ );
536
+ }
537
+
538
+ /**
539
+ * Patch the user profile.
540
+ * @param {UserProfile} params A UserProfile Object.
541
+ * @return {Promise<UserProfile>} A Promise<UserProfile> object.
542
+ */
543
+ public async setUserProfile(params: UserProfile): Promise<UserProfile> {
544
+ return this._config.request<UserProfile>(ApiUrls.USER_PRIFILE_URL, {
545
+ method: 'PATCH',
546
+ body: params,
547
+ withCredentials: true,
548
+ });
549
+ }
550
+
551
+ /**
552
+ * Patch the user profile.
553
+ * @param {QueryUserProfileReq} appended_params A QueryUserProfileReq Object.
554
+ * @return {Promise<UserProfile>} A Promise<UserProfile> object.
555
+ */
556
+ public async queryUserProfile(
557
+ params: QueryUserProfileRequest,
558
+ ): Promise<QueryUserProfileResponse> {
559
+ // let url = new URL(ApiUrls.USER_QUERY_URL);
560
+ const searchParams = new URLSearchParams(params as any);
561
+ // url.search = searchParams.toString();
562
+ return this._config.request<QueryUserProfileResponse>(`${ApiUrls.USER_QUERY_URL}?${searchParams.toString()}`, {
563
+ method: 'GET',
564
+ withCredentials: true,
565
+ });
566
+ }
567
+
568
+ /**
569
+ * setCustomSignFunc set the get ticket function
570
+ * @param getTickFn
571
+ */
572
+ public setCustomSignFunc(getTickFn: GetCustomSignTicketFn) {
573
+ this._getCustomSignTicketFn = getTickFn
574
+ }
575
+
576
+ /**
577
+ * SignInWithCustomTicket custom signIn
578
+ * @constructor
579
+ */
580
+ public async signInWithCustomTicket(): Promise<Credentials> {
581
+ const customTicket = await this._getCustomSignTicketFn()
582
+ return this.signInWithProvider({
583
+ provider_id: 'custom',
584
+ provider_token: customTicket
585
+ })
586
+ }
587
+
588
+ /**
589
+ * Reset password
590
+ * @param {ResetPasswordRequest} params
591
+ * @returns {Promise<void>}
592
+ * @memberof Auth
593
+ */
594
+ public async resetPassword(params: ResetPasswordRequest): Promise<void> {
595
+ return this._config.request(ApiUrls.AUTH_RESET_PASSWORD, {
596
+ method: 'PATCH',
597
+ body: params,
598
+ // withCredentials: true
599
+ })
600
+ }
601
+
602
+ /**
603
+ * device authorization
604
+ * @param {DeviceAuthorizeRequest} params
605
+ * @returns {Promise<DeviceAuthorizeResponse>}
606
+ * @memberof Auth
607
+ */
608
+ public async deviceAuthorize(params: DeviceAuthorizeRequest): Promise<DeviceAuthorizeResponse> {
609
+ return this._config.request(ApiUrls.AUTH_GET_DEVICE_CODE, {
610
+ method: 'POST',
611
+ body: params,
612
+ withCredentials: true
613
+ })
614
+ }
615
+
616
+ public async checkUsername(params: CheckUsernameRequest): Promise<void> {
617
+ return this._config.request(ApiUrls.CHECK_USERNAME, {
618
+ method: 'GET',
619
+ body: params,
620
+ withCredentials: true
621
+ })
622
+ }
623
+
624
+ public async checkIfUserExist(params: CheckIfUserExistRequest): Promise<CheckIfUserExistResponse> {
625
+ return this._config.request<CheckIfUserExistResponse>(ApiUrls.CHECK_IF_USER_EXIST, {
626
+ method: 'GET',
627
+ body: params,
628
+ });
629
+ }
630
+
631
+ public async loginScope(): Promise<string> {
632
+ return this._config.credentialsClient.getScope()
633
+ }
634
+
635
+ private static parseParamsToSearch(params: any): string {
636
+ for (let key in params) {
637
+ if (!params[key]) {
638
+ delete params[key]
639
+ }
640
+ }
641
+ const searchParams = new URLSearchParams(params as any);
642
+ return searchParams.toString();
643
+ }
644
+ }
@@ -0,0 +1,57 @@
1
+ export enum ApiUrls {
2
+ AUTH_SIGN_IN_URL = '/auth/v1/signin',
3
+ AUTH_SIGN_IN_ANONYMOUSLY_URL = '/auth/v1/signin/anonymously',
4
+ AUTH_SIGN_IN_WITH_PROVIDER_URL = '/auth/v1/signin/with/provider',
5
+ AUTH_SIGN_UP_URL = '/auth/v1/signup',
6
+ AUTH_TOKEN_URL = '/auth/v1/token',
7
+ AUTH_REVOKE_URL = '/auth/v1/revoke',
8
+ PROVIDER_BIND_URL = '/auth/v1/user/provider/bind',
9
+ PROVIDER_TOKEN_URL = '/auth/v1/provider/token',
10
+ PROVIDER_URI_URL = '/auth/v1/provider/uri',
11
+ USER_ME_URL = '/auth/v1/user/me',
12
+ USER_QUERY_URL = '/auth/v1/user/query',
13
+ USER_PRIFILE_URL = '/auth/v1/user/profile',
14
+ USER_TRANS_BY_PROVIDER_URL = '/auth/v1/user/trans/by/provider',
15
+ VERIFICATION_URL = '/auth/v1/verification',
16
+ VERIFY_URL = '/auth/v1/verification/verify',
17
+ PROVIDER_LIST = '/auth/v1/user/provider',
18
+ PROVIDER_UNBIND_URL = '/auth/v1/user/provider',
19
+ CHECK_PWD_URL = '/auth/v1/user/sudo',
20
+ SUDO_URL = '/auth/v1/user/sudo',
21
+ BIND_CONTACT_URL = '/auth/v1/user/contact',
22
+ AUTH_SET_PASSWORD = '/auth/v1/user/password',
23
+ AUTH_RESET_PASSWORD = '/auth/v1/reset',
24
+ AUTH_GET_DEVICE_CODE = '/auth/v1/device/code',
25
+ CHECK_USERNAME = '/auth/v1/checkUsername',
26
+ CHECK_IF_USER_EXIST = '/auth/v1/checkIfUserExist'
27
+ }
28
+
29
+ export enum VerificationUsages {
30
+ REGISTER = 'REGISTER',
31
+ SIGN_IN = 'SIGN_IN',
32
+ PASSWORD_RESET = 'PASSWORD_RESET',
33
+ EMAIL_ADDRESS_CHANGE = 'EMAIL_ADDRESS_CHANGE',
34
+ PHONE_NUMBER_CHANGE = 'PHONE_NUMBER_CHANGE',
35
+ }
36
+
37
+ export enum ErrorType {
38
+ INVALID_ARGUMENT = 'invalid_argument',
39
+ DEADLINE_EXCEEDED = 'deadline_exceeded',
40
+ NOT_FOUND = 'not_found',
41
+ ALREADY_EXISTS = 'already_exists',
42
+ PERMISSION_DENIED = 'permission_denied',
43
+ ABORTED = 'aborted',
44
+ OUT_OF_RANGE = 'out_of_range',
45
+ UNIMPLEMENTED = 'unimplemented',
46
+ INTERNAL = 'internal',
47
+ UNAVAILABLE = 'unavailable',
48
+ DATA_LOSS = 'data_loss',
49
+ // CommonError
50
+ CAPTCHA_REQUIRED = 'captcha_required',
51
+ CAPTCHA_INVALID = 'captcha_invalid',
52
+ INVALID_PASSWORD = 'invalid_password',
53
+ PASSWORD_NOT_SET = 'password_not_set',
54
+ INVALID_STATUS = 'invalid_status',
55
+ USER_PENDING = 'user_pending',
56
+ USER_BLOCKED = 'user_blocked',
57
+ }