@cloudbase/oauth 0.1.1-alpha → 1.0.0-alpha.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 (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 +51 -0
  14. package/dist/auth/apis.js +519 -0
  15. package/{auth → dist/auth}/consts.d.ts +6 -2
  16. package/dist/auth/consts.js +60 -0
  17. package/dist/auth/models.d.ts +354 -0
  18. package/dist/auth/models.js +3 -0
  19. package/dist/captcha/captcha.d.ts +35 -0
  20. package/dist/captcha/captcha.js +262 -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 +70 -0
  30. package/dist/oauth2client/oauth2client.js +618 -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 +597 -0
  38. package/src/auth/consts.ts +56 -0
  39. package/src/auth/models.ts +432 -0
  40. package/src/captcha/captcha.ts +217 -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 +606 -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,432 @@
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
+ avatarUrl?: string;
116
+ username?: string;
117
+ email?: string;
118
+ email_verified?: boolean;
119
+ phone_number?: string;
120
+ providers?: [UserProfileProvider];
121
+ gender?: string;
122
+ birthdate?: string;
123
+ zoneinfo?: string;
124
+ locale?: string;
125
+ created_from?: string;
126
+ sub?: string
127
+ uid?: string
128
+ address?: {
129
+ formatted?: string,
130
+ street_address?: string,
131
+ locality?: string,
132
+ region?: string,
133
+ postal_code?: string,
134
+ country?: string
135
+ }
136
+ nickName?: string // TODO:
137
+ province?: string // TODO:
138
+ country?: string // TODO:
139
+ city?: string // TODO:
140
+ }
141
+
142
+ export type UserInfo = UserProfile;
143
+
144
+ export interface ProviderProfile {
145
+ provider_id: string;
146
+ phone_number?: string;
147
+ }
148
+
149
+ export interface TransByProviderRequest {
150
+ provider_token: string;
151
+ }
152
+
153
+ export interface GrantTokenRequest extends BaseRequest {
154
+ client_secret?: string;
155
+ code?: string;
156
+ grant_type?: string;
157
+ redirect_uri?: string;
158
+ nonce?: string;
159
+ refresh_token?: string;
160
+ scope?: string;
161
+ }
162
+
163
+ export interface UnbindProviderRequest extends BaseRequest {
164
+ provider_id: string;
165
+ }
166
+
167
+ export interface CheckPasswordrRequest extends BaseRequest {
168
+ password: string;
169
+ }
170
+
171
+ export interface BindPhoneRequest extends BaseRequest {
172
+ phone_number: string;
173
+ sudo_token: string;
174
+ verification_token: string;
175
+ }
176
+
177
+ export interface SetPasswordRequest extends BaseRequest {
178
+ new_password: string;
179
+ sudo_token: string;
180
+ }
181
+
182
+ export interface ChangeBindedProviderRequest extends BaseRequest {
183
+ trans_token: string;
184
+ provider_id: string;
185
+ }
186
+
187
+ export type ChangeBindedProviderResponse = BaseRequest
188
+
189
+ export interface QueryUserProfileReq extends BaseRequest {
190
+ appended_params: string;
191
+ }
192
+
193
+ export interface SignInWithProviderRequest {
194
+ provider_token: string;
195
+ provider_id?: string;
196
+ }
197
+
198
+ export interface SignUpRequest {
199
+ phone_number?: string;
200
+ email?: string;
201
+
202
+ verification_code?: string;
203
+ verification_token?: string;
204
+ provider_token?: string;
205
+
206
+ password?: string;
207
+ name?: string;
208
+ gender?: string;
209
+ picture?: string;
210
+ locale?: string;
211
+ }
212
+
213
+ export interface GetVerificationRequest {
214
+ phone_number?: string;
215
+ email?: string;
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
+ sign_out_uri?: 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
+
378
+ export interface ChangeBoundProviderRequest {
379
+ trans_token: string;
380
+ provider_id: string;
381
+ }
382
+
383
+ export interface ChangeBoundProviderResponse {
384
+ client_id: string;
385
+ }
386
+
387
+ export interface QueryUserProfileRequest {
388
+ id?: [string];
389
+ username?: string;
390
+ email?: string;
391
+ phone_number?: string;
392
+ }
393
+
394
+ export interface QueryUserProfileResponse {
395
+ total: string;
396
+ data: SimpleUserProfile[]
397
+ }
398
+
399
+ export interface ResetPasswordRequest extends BaseRequest {
400
+ email: string
401
+ phone_number: string
402
+ new_password: string
403
+ verification_token: string
404
+ }
405
+
406
+ export interface DeviceAuthorizeRequest extends BaseRequest {
407
+ scope?: string
408
+ }
409
+
410
+ export interface DeviceAuthorizeResponse {
411
+ device_code: string
412
+ user_code: string
413
+ expires_in: number
414
+ interval: number
415
+ verification_url: string
416
+ verification_uri_complete: string
417
+ }
418
+
419
+ // 简化版用户信息
420
+ export interface SimpleUserProfile {
421
+ sub: string;
422
+ name: string;
423
+ picture?: string;
424
+ gender?: string;
425
+ locale?: string;
426
+ email?: string;
427
+ phone_number?: string;
428
+ }
429
+
430
+ export interface CheckUsernameRequest {
431
+ username: string
432
+ }
@@ -0,0 +1,217 @@
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
+ try {
73
+ return this._config.request<T>(reqURL, options)
74
+ } catch (err) {
75
+ if (err.error === 'captcha_required' || err.error === 'captcha_invalid') {
76
+ url = await this._appendCaptchaTokenToURL(url, state, err.error === 'captcha_invalid')
77
+ return this._config.request<T>(url, options)
78
+ } else {
79
+ return Promise.reject(err)
80
+ }
81
+ }
82
+ }
83
+
84
+ private _getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
85
+ if (window.location.search.indexOf('__captcha') > 0) {
86
+ document.body.style.display = 'none';
87
+ }
88
+ if (document.getElementById('captcha_panel_wrap') === null) {
89
+ var elementDiv = document.createElement('div');
90
+ elementDiv.style.cssText =
91
+ '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;';
92
+ elementDiv.setAttribute('id', 'captcha_panel_wrap');
93
+ document.body.appendChild(elementDiv);
94
+ }
95
+ return this._defaultOpenURIWithCallback
96
+ }
97
+
98
+ /**
99
+ * 默认通过浏览器打开网页并获取回调
100
+ */
101
+ private async _defaultOpenURIWithCallback(url: string): Promise<CaptchaToken> {
102
+ const target = document.getElementById('captcha_panel_wrap'),
103
+ iframe = document.createElement('iframe')
104
+ target.innerHTML = '';
105
+ iframe.setAttribute('src', url)
106
+ iframe.setAttribute('id', 'review-panel-iframe')
107
+ iframe.style.cssText = 'min-width:355px;display:block;height:355px;margin:0 auto;background-color: rgb(255, 255, 255);border: none;';
108
+ target.appendChild(iframe);
109
+ target.style.display = 'block';
110
+ return new Promise<CaptchaToken>((resolve, reject) => {
111
+ iframe.onload = function () {
112
+ try {
113
+ var windowLocation = window.location;
114
+ var iframeLocation = iframe.contentWindow.location;
115
+ if (
116
+ iframeLocation.host +
117
+ iframeLocation.pathname ===
118
+ windowLocation.host +
119
+ windowLocation.pathname
120
+ ) {
121
+ target.style.display = 'none';
122
+ const iframeUrlParams = new URLSearchParams(iframeLocation.search);
123
+ const captchToken = iframeUrlParams.get('captcha_token');
124
+ if (captchToken) {
125
+ return resolve({
126
+ captcha_token: captchToken,
127
+ expires_in: Number(iframeUrlParams.get('expires_in'))
128
+ })
129
+ }
130
+ return reject({
131
+ error: iframeUrlParams.get('error'),
132
+ error_description: iframeUrlParams.get('error_description')
133
+ })
134
+ } else {
135
+ target.style.display = 'block';
136
+ }
137
+ } catch (error) {
138
+ target.style.display = 'block';
139
+ }
140
+ };
141
+ })
142
+ }
143
+ /**
144
+ * _getCaptchaToken 获取captchaToken
145
+ */
146
+ private async _getCaptchaToken(forceNewToken: boolean, state: string): Promise<string> {
147
+ if (!forceNewToken) {
148
+ // 如果本地存在,则直接返回
149
+ const captchaToken = await this._findCaptchaToken()
150
+ if (captchaToken) {
151
+ return captchaToken
152
+ }
153
+ }
154
+ const redirectURL = window.location.origin + window.location.pathname + "?__captcha=on"
155
+ const captchaTokenResp = await this._config.request<GetCaptchaResponse>(GET_CAPTCHA_URL, {
156
+ method: 'POST',
157
+ body: {
158
+ client_id: this._config.clientId,
159
+ redirect_uri: redirectURL,
160
+ state: state
161
+ },
162
+ withCredentials: false,
163
+ })
164
+ if (captchaTokenResp.captcha_token) {
165
+ const captchaToken = {
166
+ captcha_token: captchaTokenResp.captcha_token,
167
+ expires_in: captchaTokenResp.expires_in,
168
+ }
169
+ this._saveCaptchaToken(captchaToken)
170
+ return captchaTokenResp.captcha_token
171
+ }
172
+ const captchaToken = await this._config.openURIWithCallback(captchaTokenResp.url)
173
+ this._saveCaptchaToken(captchaToken)
174
+ return captchaToken.captcha_token
175
+ }
176
+
177
+ private async _appendCaptchaTokenToURL(url: string, state: string, forceNewToken: boolean): Promise<string> {
178
+ const captchaToken = await this._getCaptchaToken(forceNewToken, state);
179
+ if (url.indexOf("?") > 0) {
180
+ url += "&captcha_token=" + captchaToken
181
+ } else {
182
+ url += "?captcha_token=" + captchaToken
183
+ }
184
+ return url
185
+ }
186
+
187
+ private async _saveCaptchaToken(token: CaptchaToken) {
188
+ token.expires_at = new Date(
189
+ Date.now() + (token.expires_in - 10) * 1000,
190
+ );
191
+ const tokenStr: string = JSON.stringify(token);
192
+ await this._config.storage.setItem(this._tokenSectionName, tokenStr);
193
+ }
194
+
195
+ private async _findCaptchaToken(): Promise<string> {
196
+ const tokenStr: string = await this._config.storage.getItem(
197
+ this._tokenSectionName,
198
+ );
199
+ if (tokenStr !== undefined && tokenStr !== null) {
200
+ try {
201
+ const captchaToken = JSON.parse(tokenStr);
202
+ if (captchaToken?.expires_at) {
203
+ captchaToken.expires_at = new Date(captchaToken.expires_at);
204
+ }
205
+ const isExpired = captchaToken.expires_at < new Date();
206
+ if (isExpired) {
207
+ return null
208
+ }
209
+ return captchaToken.captcha_token
210
+ } catch (error) {
211
+ await this._config.storage.removeItem(this._tokenSectionName);
212
+ return null
213
+ }
214
+ }
215
+ return null
216
+ }
217
+ }
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
+