@cloudbase/oauth 1.2.0-beta → 2.2.0

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 (125) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +26 -0
  3. package/CHANGELOG.md +30 -0
  4. package/Dockerfile +15 -0
  5. package/README.md +183 -0
  6. package/_exmaple/assets/scripts/function/function.ts +99 -0
  7. package/_exmaple/assets/scripts/index.ts +101 -0
  8. package/_exmaple/assets/scripts/request.ts +11 -0
  9. package/_exmaple/index.html +15 -0
  10. package/_exmaple/package.json +33 -0
  11. package/_exmaple/tsconfig.json +71 -0
  12. package/_exmaple/typings.d.ts +0 -0
  13. package/_exmaple/webpack.config.js +42 -0
  14. package/dist/cjs/auth/apis.d.ts +55 -0
  15. package/dist/cjs/auth/apis.js +568 -0
  16. package/{auth → dist/cjs/auth}/consts.d.ts +9 -21
  17. package/dist/cjs/auth/consts.js +61 -0
  18. package/dist/cjs/auth/models.d.ts +354 -0
  19. package/dist/cjs/auth/models.js +3 -0
  20. package/dist/cjs/captcha/captcha.d.ts +35 -0
  21. package/dist/cjs/captcha/captcha.js +267 -0
  22. package/dist/cjs/index.d.ts +14 -0
  23. package/dist/cjs/index.js +63 -0
  24. package/{oauthclient → dist/cjs/oauth2client}/consts.d.ts +22 -1
  25. package/dist/cjs/oauth2client/consts.js +73 -0
  26. package/dist/cjs/oauth2client/interface.d.ts +15 -0
  27. package/dist/cjs/oauth2client/interface.js +10 -0
  28. package/dist/cjs/oauth2client/models.d.ts +47 -0
  29. package/dist/cjs/oauth2client/models.js +3 -0
  30. package/dist/cjs/oauth2client/oauth2client.d.ts +71 -0
  31. package/dist/cjs/oauth2client/oauth2client.js +640 -0
  32. package/dist/cjs/utils/function/single-promise.d.ts +5 -0
  33. package/dist/cjs/utils/function/single-promise.js +89 -0
  34. package/dist/cjs/utils/uuid.d.ts +1 -0
  35. package/dist/cjs/utils/uuid.js +12 -0
  36. package/dist/esm/auth/apis.d.ts +55 -0
  37. package/dist/esm/auth/apis.js +566 -0
  38. package/dist/esm/auth/consts.d.ts +54 -0
  39. package/dist/esm/auth/consts.js +58 -0
  40. package/dist/esm/auth/models.d.ts +354 -0
  41. package/dist/esm/auth/models.js +1 -0
  42. package/dist/esm/captcha/captcha.d.ts +35 -0
  43. package/dist/esm/captcha/captcha.js +264 -0
  44. package/dist/esm/index.d.ts +14 -0
  45. package/dist/esm/index.js +32 -0
  46. package/dist/esm/oauth2client/consts.d.ts +67 -0
  47. package/dist/esm/oauth2client/consts.js +70 -0
  48. package/dist/esm/oauth2client/interface.d.ts +15 -0
  49. package/dist/esm/oauth2client/interface.js +7 -0
  50. package/dist/esm/oauth2client/models.d.ts +47 -0
  51. package/dist/esm/oauth2client/models.js +1 -0
  52. package/dist/esm/oauth2client/oauth2client.d.ts +71 -0
  53. package/dist/esm/oauth2client/oauth2client.js +636 -0
  54. package/dist/esm/utils/function/single-promise.d.ts +5 -0
  55. package/dist/esm/utils/function/single-promise.js +86 -0
  56. package/dist/esm/utils/uuid.d.ts +1 -0
  57. package/dist/esm/utils/uuid.js +8 -0
  58. package/package.json +27 -14
  59. package/publish.sh +2 -0
  60. package/src/auth/apis.ts +642 -0
  61. package/src/auth/consts.ts +57 -0
  62. package/src/auth/models.ts +444 -0
  63. package/src/captcha/captcha.ts +222 -0
  64. package/src/index.ts +49 -0
  65. package/src/oauth2client/consts.ts +69 -0
  66. package/src/oauth2client/interface.ts +57 -0
  67. package/src/oauth2client/models.ts +61 -0
  68. package/src/oauth2client/oauth2client.ts +620 -0
  69. package/src/utils/function/single-promise.ts +40 -0
  70. package/src/utils/uuid.ts +11 -0
  71. package/tsconfig.esm.json +44 -0
  72. package/tsconfig.json +44 -0
  73. package/wiki/README.md +75 -0
  74. package/app/index.d.ts +0 -29
  75. package/app/index.js +0 -50
  76. package/app/index.js.map +0 -1
  77. package/app/internal.d.ts +0 -12
  78. package/app/internal.js +0 -25
  79. package/app/internal.js.map +0 -1
  80. package/app/openuri.d.ts +0 -20
  81. package/app/openuri.js +0 -106
  82. package/app/openuri.js.map +0 -1
  83. package/app/request.d.ts +0 -19
  84. package/app/request.js +0 -52
  85. package/app/request.js.map +0 -1
  86. package/app/storage.d.ts +0 -41
  87. package/app/storage.js +0 -35
  88. package/app/storage.js.map +0 -1
  89. package/auth/consts.js +0 -79
  90. package/auth/consts.js.map +0 -1
  91. package/auth/index.d.ts +0 -242
  92. package/auth/index.js +0 -550
  93. package/auth/index.js.map +0 -1
  94. package/auth/models.d.ts +0 -284
  95. package/auth/models.js +0 -4
  96. package/auth/models.js.map +0 -1
  97. package/captcha/index.d.ts +0 -45
  98. package/captcha/index.js +0 -135
  99. package/captcha/index.js.map +0 -1
  100. package/function/index.d.ts +0 -38
  101. package/function/index.js +0 -80
  102. package/function/index.js.map +0 -1
  103. package/index.d.ts +0 -8
  104. package/index.js +0 -17
  105. package/index.js.map +0 -1
  106. package/oauthclient/consts.js +0 -53
  107. package/oauthclient/consts.js.map +0 -1
  108. package/oauthclient/index.d.ts +0 -18
  109. package/oauthclient/index.js +0 -21
  110. package/oauthclient/index.js.map +0 -1
  111. package/oauthclient/interface.d.ts +0 -29
  112. package/oauthclient/interface.js +0 -10
  113. package/oauthclient/interface.js.map +0 -1
  114. package/oauthclient/models.d.ts +0 -40
  115. package/oauthclient/models.js +0 -3
  116. package/oauthclient/models.js.map +0 -1
  117. package/oauthclient/oauthclient.d.ts +0 -146
  118. package/oauthclient/oauthclient.js +0 -414
  119. package/oauthclient/oauthclient.js.map +0 -1
  120. package/utils/promise.d.ts +0 -18
  121. package/utils/promise.js +0 -48
  122. package/utils/promise.js.map +0 -1
  123. package/utils/uuid.d.ts +0 -5
  124. package/utils/uuid.js +0 -16
  125. package/utils/uuid.js.map +0 -1
@@ -0,0 +1,444 @@
1
+ interface BaseRequest {
2
+ client_id?: string;
3
+ }
4
+
5
+ export type GetCustomSignTicketFn = () => Promise<string>;
6
+
7
+ export interface SignInRequest extends BaseRequest {
8
+ username?: string;
9
+ password?: string;
10
+ verification_token?: string;
11
+ }
12
+
13
+ export interface SignInWithProviderRequest extends BaseRequest {
14
+ provider_token: string;
15
+ }
16
+
17
+ export interface SignUpRequest extends BaseRequest {
18
+ phone_number?: string;
19
+ email?: string;
20
+
21
+ verification_code?: string;
22
+ verification_token?: string;
23
+ provider_token?: string;
24
+
25
+ password?: string;
26
+ name?: string;
27
+ gender?: string;
28
+ picture?: string;
29
+ locale?: string;
30
+ }
31
+
32
+ export interface GetVerificationRequest extends BaseRequest {
33
+ phone_number?: string;
34
+ email?: string;
35
+ target?: string | 'ANY';
36
+ usage?: string;
37
+ }
38
+
39
+ export interface GetVerificationResponse {
40
+ verification_id?: string;
41
+ is_user?: boolean | false;
42
+ }
43
+
44
+ export interface VerifyResponse {
45
+ verification_token?: string;
46
+ }
47
+
48
+ export interface VerifyRequest extends BaseRequest {
49
+ verification_code: string;
50
+ verification_id?: string;
51
+ }
52
+
53
+ export interface ProviderBindRequest {
54
+ provider_token: string;
55
+ }
56
+
57
+ export interface GrantProviderTokenRequest extends BaseRequest {
58
+ provider_id: string;
59
+ provider_redirect_uri?: string;
60
+ provider_code?: string;
61
+ provider_access_token?: string;
62
+ provider_id_token?: string;
63
+ }
64
+
65
+ export interface GrantProviderTokenResponse {
66
+ provider_token: string;
67
+ expires_in: number;
68
+ }
69
+
70
+ export interface PatchProviderTokenRequest extends BaseRequest {
71
+ provider_token: string;
72
+ provider_params: {
73
+ encryptedData: string;
74
+ iv: string;
75
+ };
76
+ }
77
+
78
+ export interface PatchProviderTokenResponse {
79
+ provider_token: string;
80
+ expires_in: number;
81
+ provider_profile: ProviderProfile;
82
+ }
83
+
84
+ // export interface GenProviderRedirectUriRequest {
85
+ // provider_id: string;
86
+ // provider_redirect_uri: string;
87
+ // state: string;
88
+ // other_params?: {
89
+ // sign_out_uri?: string;
90
+ // };
91
+ // }
92
+
93
+ export interface GenProviderRedirectUriResponse {
94
+ uri: string;
95
+ signout_uri?: string;
96
+ }
97
+
98
+ export interface BindWithProviderRequest extends BaseRequest {
99
+ provider_token: string;
100
+ }
101
+
102
+ export interface BindWithProviderRequest {
103
+ provider_token: string;
104
+ }
105
+
106
+ export interface UserProfileProvider {
107
+ id?: string;
108
+ provider_user_id?: string;
109
+ name?: string;
110
+ }
111
+
112
+ export interface UserProfile {
113
+ name?: string;
114
+ picture?: string;
115
+ username?: string;
116
+ email?: string;
117
+ email_verified?: boolean;
118
+ phone_number?: string;
119
+ providers?: [UserProfileProvider];
120
+ gender?: string;
121
+ birthdate?: string;
122
+ zoneinfo?: string;
123
+ locale?: string;
124
+ created_from?: string;
125
+ sub?: string
126
+ uid?: string
127
+ address?: {
128
+ formatted?: string,
129
+ street_address?: string,
130
+ locality?: string,
131
+ region?: string,
132
+ postal_code?: string,
133
+ country?: string
134
+ }
135
+ nickName?: string // TODO:
136
+ province?: string // TODO:
137
+ country?: string // TODO:
138
+ city?: string // TODO:
139
+ }
140
+
141
+ export type UserInfo = UserProfile;
142
+
143
+ export interface ProviderProfile {
144
+ provider_id: string;
145
+ phone_number?: string;
146
+ }
147
+
148
+ export interface TransByProviderRequest {
149
+ provider_token: string;
150
+ }
151
+
152
+ export interface GrantTokenRequest extends BaseRequest {
153
+ client_secret?: string;
154
+ code?: string;
155
+ grant_type?: string;
156
+ redirect_uri?: string;
157
+ nonce?: string;
158
+ refresh_token?: string;
159
+ scope?: string;
160
+ }
161
+
162
+ export interface UnbindProviderRequest extends BaseRequest {
163
+ provider_id: string;
164
+ }
165
+
166
+ export interface CheckPasswordrRequest extends BaseRequest {
167
+ password: string;
168
+ }
169
+
170
+ export interface BindPhoneRequest extends BaseRequest {
171
+ phone_number: string;
172
+ sudo_token: string;
173
+ verification_token: string;
174
+ }
175
+
176
+ export interface SetPasswordRequest extends BaseRequest {
177
+ new_password: string;
178
+ sudo_token: string;
179
+ }
180
+
181
+ export interface ChangeBindedProviderRequest extends BaseRequest {
182
+ trans_token: string;
183
+ provider_id: string;
184
+ }
185
+
186
+ export type ChangeBindedProviderResponse = BaseRequest
187
+
188
+ export interface QueryUserProfileReq extends BaseRequest {
189
+ appended_params: string;
190
+ }
191
+
192
+ export interface SignInWithProviderRequest {
193
+ provider_token: string;
194
+ provider_id?: string;
195
+ }
196
+
197
+ export interface SignUpRequest {
198
+ phone_number?: string;
199
+ email?: string;
200
+
201
+ verification_code?: string;
202
+ verification_token?: string;
203
+ provider_token?: string;
204
+
205
+ password?: string;
206
+ name?: string;
207
+ gender?: string;
208
+ picture?: string;
209
+ locale?: string;
210
+ }
211
+
212
+ export interface GetVerificationRequest {
213
+ phone_number?: string;
214
+ email?: string;
215
+ // 可选 ANY,USER,NOT_USER, CUR_USER;
216
+ target?: string | 'ANY';
217
+ usage?: string;
218
+ }
219
+
220
+ export interface GetVerificationResponse {
221
+ verification_id?: string;
222
+ is_user?: boolean | false;
223
+ }
224
+
225
+ export interface VerifyResponse {
226
+ verification_token?: string;
227
+ }
228
+
229
+ export interface VerifyRequest {
230
+ verification_code: string;
231
+ verification_id?: string;
232
+ }
233
+
234
+ export interface ProviderBindRequest {
235
+ provider_token: string;
236
+ }
237
+
238
+ export interface GrantProviderTokenRequest {
239
+ provider_id: string;
240
+ provider_redirect_uri?: string;
241
+ provider_code?: string;
242
+ provider_access_token?: string;
243
+ provider_id_token?: string;
244
+ }
245
+
246
+ export interface GrantProviderTokenResponse {
247
+ provider_token: string;
248
+ expires_in: number;
249
+ }
250
+
251
+ export interface PatchProviderTokenRequest {
252
+ provider_token: string;
253
+ provider_params: {
254
+ encryptedData: string;
255
+ iv: string;
256
+ };
257
+ }
258
+
259
+ export interface PatchProviderTokenResponse {
260
+ provider_token: string;
261
+ expires_in: number;
262
+ provider_profile: ProviderProfile;
263
+ }
264
+
265
+ export interface GenProviderRedirectUriRequest {
266
+ provider_id: string;
267
+ provider_redirect_uri: string;
268
+ state: string;
269
+ other_params?: {
270
+ [key: string]: string
271
+ };
272
+ }
273
+
274
+ export interface GenProviderRedirectUriResponse {
275
+ uri: string;
276
+ signout_uri?: string;
277
+ }
278
+
279
+ export interface BindWithProviderRequest {
280
+ provider_token: string;
281
+ }
282
+
283
+ export interface BindWithProviderRequest {
284
+ provider_token: string;
285
+ }
286
+
287
+ export interface UserProfileProvider {
288
+ id?: string;
289
+ provider_user_id?: string;
290
+ name?: string;
291
+ }
292
+
293
+ export interface UserProfile {
294
+ name?: string;
295
+ picture?: string;
296
+ username?: string;
297
+ email?: string;
298
+ email_verified?: boolean;
299
+ phone_number?: string;
300
+ providers?: [UserProfileProvider];
301
+ gender?: string;
302
+ birthdate?: string;
303
+ zoneinfo?: string;
304
+ locale?: string;
305
+ created_from?: string;
306
+ }
307
+
308
+ export interface ProviderProfile {
309
+ provider_id: string;
310
+ phone_number?: string;
311
+ }
312
+
313
+ export interface TransByProviderRequest {
314
+ provider_token: string;
315
+ }
316
+
317
+ export interface GrantTokenRequest {
318
+ client_secret?: string;
319
+ code?: string;
320
+ grant_type?: string;
321
+ redirect_uri?: string;
322
+ nonce?: string;
323
+ refresh_token?: string;
324
+ scope?: string;
325
+ }
326
+
327
+ export interface UnbindProviderRequest {
328
+ provider_id: string;
329
+ }
330
+
331
+ export interface CheckPasswordrRequest {
332
+ password: string;
333
+ }
334
+
335
+ export interface BindPhoneRequest {
336
+ phone_number: string;
337
+ sudo_token: string;
338
+ verification_token: string;
339
+ conflict_resolution: string
340
+ // 1. DEFAULT 0, 默认提示用户手机号已被绑定
341
+ // 2. DELETE_ACCOUNT_TRANSFER 1, 标记原账号已被注销,并将手机换绑给自己
342
+ // 3. TRANSFER 2, 仅换绑手机号,不注销原有账号(换绑后原账号无法登录时,则自动注销原账号)
343
+ }
344
+
345
+ export interface BindEmailRequest {
346
+ email: string;
347
+ sudo_token: string;
348
+ verification_token: string;
349
+ }
350
+
351
+ export interface SetPasswordRequest {
352
+ new_password: string;
353
+ sudo_token: string;
354
+ }
355
+
356
+
357
+ export interface SetPasswordRequest {
358
+ new_password: string;
359
+ sudo_token: string;
360
+ }
361
+
362
+ export interface UpdatePasswordRequest {
363
+ old_password: string;
364
+ new_password: string;
365
+ }
366
+
367
+ // password 和 verification_token 而选一,如果绑定了手机号,则必须使用verification_token 进行sudo
368
+ export interface SudoRequest {
369
+ password?: string;
370
+ verification_token?: string
371
+ }
372
+
373
+ export interface SudoResponse {
374
+ sudo_token?: string
375
+ }
376
+
377
+ export interface WithSudoRequest {
378
+ sudo_token: string
379
+ }
380
+
381
+
382
+ export interface ChangeBoundProviderRequest {
383
+ trans_token: string;
384
+ provider_id: string;
385
+ }
386
+
387
+ export interface ChangeBoundProviderResponse {
388
+ client_id: string;
389
+ }
390
+
391
+ export interface QueryUserProfileRequest {
392
+ id?: [string];
393
+ username?: string;
394
+ email?: string;
395
+ phone_number?: string;
396
+ }
397
+
398
+ export interface QueryUserProfileResponse {
399
+ total: string;
400
+ data: SimpleUserProfile[]
401
+ }
402
+
403
+ export interface ResetPasswordRequest extends BaseRequest {
404
+ email: string
405
+ phone_number: string
406
+ new_password: string
407
+ verification_token: string
408
+ }
409
+
410
+ export interface DeviceAuthorizeRequest extends BaseRequest {
411
+ scope?: string
412
+ }
413
+
414
+ export interface DeviceAuthorizeResponse {
415
+ device_code: string
416
+ user_code: string
417
+ expires_in: number
418
+ interval: number
419
+ verification_url: string
420
+ verification_uri_complete: string
421
+ }
422
+
423
+ // 简化版用户信息
424
+ export interface SimpleUserProfile {
425
+ sub: string;
426
+ name: string;
427
+ picture?: string;
428
+ gender?: string;
429
+ locale?: string;
430
+ email?: string;
431
+ phone_number?: string;
432
+ }
433
+
434
+ export interface CheckUsernameRequest {
435
+ username: string
436
+ }
437
+
438
+ export interface CheckIfUserExistRequest {
439
+ username: string;
440
+ }
441
+
442
+ export interface CheckIfUserExistResponse {
443
+ exist: boolean;
444
+ }
@@ -0,0 +1,222 @@
1
+ import { SimpleStorage, RequestFunction } from '../oauth2client/interface';
2
+ import { AuthClientRequestOptions } from "../oauth2client/models";
3
+ import { defaultStorage } from "../oauth2client/oauth2client";
4
+
5
+ export interface CaptchaOptions {
6
+ clientId: string
7
+ request: RequestFunction;
8
+ storage: SimpleStorage;
9
+ // 打开网页并通过URL回调获取 CaptchaToken,针对不通的平台,该函数可以自定义实现, 默认集成浏览器端认证
10
+ openURIWithCallback?: OpenURIWithCallbackFuction;
11
+ }
12
+
13
+ type OpenURIWithCallbackFuction = (url: string) => Promise<CaptchaToken>;
14
+
15
+ export interface CaptchaToken {
16
+ captcha_token: string
17
+ expires_in: number
18
+ expires_at?: Date | null;
19
+ }
20
+
21
+ export interface CaptchaRequestOptions extends AuthClientRequestOptions {
22
+ withCaptcha?: boolean;
23
+ }
24
+
25
+ export interface GetCaptchaResponse {
26
+ captcha_token?: string
27
+ expires_in?: number
28
+ url?: string
29
+ }
30
+
31
+ const GET_CAPTCHA_URL = '/auth/v1/captcha/init'
32
+
33
+ export class Captcha {
34
+ private _config: CaptchaOptions;
35
+ private _tokenSectionName: string;
36
+
37
+ /**
38
+ * constructor
39
+ * @param {CaptchaOptions} opts
40
+ */
41
+ constructor(opts: CaptchaOptions) {
42
+ if (!opts.openURIWithCallback) {
43
+ opts.openURIWithCallback = this._getDefaultOpenURIWithCallback()
44
+ }
45
+ if (!opts.storage) {
46
+ opts.storage = defaultStorage
47
+ }
48
+ this._config = opts
49
+ this._tokenSectionName = 'captcha_' + opts.clientId
50
+ }
51
+
52
+ /**
53
+ * request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
54
+ * @param {string} url
55
+ * @param {AuthClientRequestOptions} options
56
+ */
57
+ public async request<T>(
58
+ url: string,
59
+ options?: CaptchaRequestOptions,
60
+ ): Promise<T> {
61
+ if (!options) {
62
+ options = {};
63
+ }
64
+ if (!options.method) {
65
+ options.method = 'GET'
66
+ }
67
+ const state = options.method + ":" + url
68
+ let reqURL = url;
69
+ if (options.withCaptcha) {
70
+ reqURL = await this._appendCaptchaTokenToURL(url, state, false);
71
+ }
72
+
73
+ let resp: T;
74
+ try {
75
+ resp = await this._config.request<T>(reqURL, options)
76
+ } catch (err) {
77
+ if (err.error === 'captcha_required' || err.error === 'captcha_invalid') {
78
+ url = await this._appendCaptchaTokenToURL(url, state, err.error === 'captcha_invalid')
79
+ return this._config.request<T>(url, options)
80
+ } else {
81
+ return Promise.reject(err)
82
+ }
83
+ }
84
+ return resp
85
+ }
86
+
87
+ private _getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
88
+ if (window.location.search.indexOf('__captcha') > 0) {
89
+ document.body.style.display = 'none';
90
+ }
91
+ if (document.getElementById('captcha_panel_wrap') === null) {
92
+ var elementDiv = document.createElement('div');
93
+ elementDiv.style.cssText =
94
+ 'background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;';
95
+ elementDiv.setAttribute('id', 'captcha_panel_wrap');
96
+ setTimeout(() => {
97
+ document.body.appendChild(elementDiv);
98
+ }, 0)
99
+ }
100
+ return this._defaultOpenURIWithCallback
101
+ }
102
+
103
+ /**
104
+ * 默认通过浏览器打开网页并获取回调
105
+ */
106
+ private async _defaultOpenURIWithCallback(url: string): Promise<CaptchaToken> {
107
+ const target = document.getElementById('captcha_panel_wrap'),
108
+ iframe = document.createElement('iframe')
109
+ target.innerHTML = '';
110
+ iframe.setAttribute('src', url)
111
+ iframe.setAttribute('id', 'review-panel-iframe')
112
+ iframe.style.cssText = 'min-width:355px;display:block;height:355px;margin:0 auto;background-color: rgb(255, 255, 255);border: none;';
113
+ target.appendChild(iframe);
114
+ target.style.display = 'block';
115
+ return new Promise<CaptchaToken>((resolve, reject) => {
116
+ iframe.onload = function () {
117
+ try {
118
+ var windowLocation = window.location;
119
+ var iframeLocation = iframe.contentWindow.location;
120
+ if (
121
+ iframeLocation.host +
122
+ iframeLocation.pathname ===
123
+ windowLocation.host +
124
+ windowLocation.pathname
125
+ ) {
126
+ target.style.display = 'none';
127
+ const iframeUrlParams = new URLSearchParams(iframeLocation.search);
128
+ const captchToken = iframeUrlParams.get('captcha_token');
129
+ if (captchToken) {
130
+ return resolve({
131
+ captcha_token: captchToken,
132
+ expires_in: Number(iframeUrlParams.get('expires_in'))
133
+ })
134
+ }
135
+ return reject({
136
+ error: iframeUrlParams.get('error'),
137
+ error_description: iframeUrlParams.get('error_description')
138
+ })
139
+ } else {
140
+ target.style.display = 'block';
141
+ }
142
+ } catch (error) {
143
+ target.style.display = 'block';
144
+ }
145
+ };
146
+ })
147
+ }
148
+ /**
149
+ * _getCaptchaToken 获取captchaToken
150
+ */
151
+ private async _getCaptchaToken(forceNewToken: boolean, state: string): Promise<string> {
152
+ if (!forceNewToken) {
153
+ // 如果本地存在,则直接返回
154
+ const captchaToken = await this._findCaptchaToken()
155
+ if (captchaToken) {
156
+ return captchaToken
157
+ }
158
+ }
159
+ const redirectURL = window.location.origin + window.location.pathname + "?__captcha=on"
160
+ const captchaTokenResp = await this._config.request<GetCaptchaResponse>(GET_CAPTCHA_URL, {
161
+ method: 'POST',
162
+ body: {
163
+ client_id: this._config.clientId,
164
+ redirect_uri: redirectURL,
165
+ state: state
166
+ },
167
+ withCredentials: false,
168
+ })
169
+ if (captchaTokenResp.captcha_token) {
170
+ const captchaToken = {
171
+ captcha_token: captchaTokenResp.captcha_token,
172
+ expires_in: captchaTokenResp.expires_in,
173
+ }
174
+ this._saveCaptchaToken(captchaToken)
175
+ return captchaTokenResp.captcha_token
176
+ }
177
+ const captchaToken = await this._config.openURIWithCallback(captchaTokenResp.url)
178
+ this._saveCaptchaToken(captchaToken)
179
+ return captchaToken.captcha_token
180
+ }
181
+
182
+ private async _appendCaptchaTokenToURL(url: string, state: string, forceNewToken: boolean): Promise<string> {
183
+ const captchaToken = await this._getCaptchaToken(forceNewToken, state);
184
+ if (url.indexOf("?") > 0) {
185
+ url += "&captcha_token=" + captchaToken
186
+ } else {
187
+ url += "?captcha_token=" + captchaToken
188
+ }
189
+ return url
190
+ }
191
+
192
+ private async _saveCaptchaToken(token: CaptchaToken) {
193
+ token.expires_at = new Date(
194
+ Date.now() + (token.expires_in - 10) * 1000,
195
+ );
196
+ const tokenStr: string = JSON.stringify(token);
197
+ await this._config.storage.setItem(this._tokenSectionName, tokenStr);
198
+ }
199
+
200
+ private async _findCaptchaToken(): Promise<string> {
201
+ const tokenStr: string = await this._config.storage.getItem(
202
+ this._tokenSectionName,
203
+ );
204
+ if (tokenStr !== undefined && tokenStr !== null) {
205
+ try {
206
+ const captchaToken = JSON.parse(tokenStr);
207
+ if (captchaToken?.expires_at) {
208
+ captchaToken.expires_at = new Date(captchaToken.expires_at);
209
+ }
210
+ const isExpired = captchaToken.expires_at < new Date();
211
+ if (isExpired) {
212
+ return null
213
+ }
214
+ return captchaToken.captcha_token
215
+ } catch (error) {
216
+ await this._config.storage.removeItem(this._tokenSectionName);
217
+ return null
218
+ }
219
+ }
220
+ return null
221
+ }
222
+ }
package/src/index.ts ADDED
@@ -0,0 +1,49 @@
1
+ export { Syntax, ErrorType } from './oauth2client/consts';
2
+
3
+ import { OAuth2Client } from './oauth2client/oauth2client'
4
+
5
+ export {
6
+ defaultStorage,
7
+ defaultRequest,
8
+ ToResponseErrorOptions,
9
+ toResponseError,
10
+ generateRequestId,
11
+ OAuth2Client,
12
+ } from './oauth2client/oauth2client';
13
+
14
+ export { AuthClient, SimpleStorage } from './oauth2client/interface';
15
+
16
+ // import { Credentials } from './oauth2client/models'
17
+ export {
18
+ Credentials,
19
+ ResponseError,
20
+ OAuth2ClientOptions,
21
+ AuthClientRequestOptions,
22
+ } from './oauth2client/models';
23
+
24
+ import { AuthOptions, Auth } from './auth/apis'
25
+
26
+ export { AuthOptions, Auth } from './auth/apis';
27
+
28
+ import * as authModels from './auth/models';
29
+ export { authModels };
30
+
31
+
32
+ export class CloudbaseOAuth {
33
+ public oauth2client: OAuth2Client
34
+ public authApi: Auth
35
+
36
+ constructor(authOptions: AuthOptions) {
37
+ const { apiOrigin, clientId } = authOptions
38
+ this.oauth2client = new OAuth2Client({
39
+ apiOrigin,
40
+ clientId
41
+ })
42
+
43
+ this.authApi = new Auth({
44
+ credentialsClient: this.oauth2client,
45
+ ...authOptions
46
+ })
47
+ }
48
+ }
49
+