@cloudbase/oauth 2.5.49-beta.0 → 2.5.49-beta.2

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 (39) hide show
  1. package/dist/cjs/auth/apis.d.ts +13 -6
  2. package/dist/cjs/auth/apis.js +94 -18
  3. package/dist/cjs/auth/consts.d.ts +5 -0
  4. package/dist/cjs/auth/consts.js +6 -1
  5. package/dist/cjs/auth/models.d.ts +60 -2
  6. package/dist/cjs/auth/models.js +1 -1
  7. package/dist/cjs/index.d.ts +2 -1
  8. package/dist/cjs/index.js +1 -1
  9. package/dist/cjs/oauth2client/models.d.ts +1 -0
  10. package/dist/cjs/oauth2client/models.js +1 -1
  11. package/dist/cjs/oauth2client/oauth2client.d.ts +5 -0
  12. package/dist/cjs/oauth2client/oauth2client.js +23 -14
  13. package/dist/cjs/utils/base64.d.ts +4 -0
  14. package/dist/cjs/utils/base64.js +99 -0
  15. package/dist/cjs/utils/encryptlong/index.js +4 -3
  16. package/dist/esm/auth/apis.d.ts +13 -6
  17. package/dist/esm/auth/apis.js +94 -18
  18. package/dist/esm/auth/consts.d.ts +5 -0
  19. package/dist/esm/auth/consts.js +6 -1
  20. package/dist/esm/auth/models.d.ts +60 -2
  21. package/dist/esm/auth/models.js +1 -1
  22. package/dist/esm/index.d.ts +2 -1
  23. package/dist/esm/index.js +1 -1
  24. package/dist/esm/oauth2client/models.d.ts +1 -0
  25. package/dist/esm/oauth2client/models.js +1 -1
  26. package/dist/esm/oauth2client/oauth2client.d.ts +5 -0
  27. package/dist/esm/oauth2client/oauth2client.js +23 -14
  28. package/dist/esm/utils/base64.d.ts +4 -0
  29. package/dist/esm/utils/base64.js +92 -0
  30. package/dist/esm/utils/encryptlong/index.js +4 -3
  31. package/package.json +2 -2
  32. package/src/auth/apis.ts +102 -30
  33. package/src/auth/consts.ts +5 -0
  34. package/src/auth/models.ts +103 -38
  35. package/src/index.ts +3 -9
  36. package/src/oauth2client/models.ts +1 -0
  37. package/src/oauth2client/oauth2client.ts +27 -16
  38. package/src/utils/base64.ts +100 -0
  39. package/src/utils/encryptlong/index.js +3 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/oauth",
3
- "version": "2.5.49-beta.0",
3
+ "version": "2.5.49-beta.2",
4
4
  "description": "cloudbase javascript sdk auth componets",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -21,5 +21,5 @@
21
21
  "encryptlong": "^3.1.4"
22
22
  },
23
23
  "license": "Apache-2.0",
24
- "gitHead": "a4b9835493467e4ab8d35ba0ab3c02dbdc576edf"
24
+ "gitHead": "0d182ece790ba8a0ef254f63f6a06ab3e878a302"
25
25
  }
package/src/auth/apis.ts CHANGED
@@ -23,11 +23,9 @@ import {
23
23
  UserProfileProvider,
24
24
  UnbindProviderRequest,
25
25
  CheckPasswordrRequest,
26
- BindPhoneRequest,
27
- BindEmailRequest,
28
26
  SetPasswordRequest,
29
- ChangeBindedProviderRequest,
30
- ChangeBindedProviderResponse,
27
+ ChangeBoundProviderRequest,
28
+ ChangeBoundProviderResponse,
31
29
  UpdatePasswordRequest,
32
30
  SudoResponse,
33
31
  SudoRequest,
@@ -49,6 +47,12 @@ import {
49
47
  GetMiniProgramQrCodeResponse,
50
48
  GetMiniProgramQrCodeStatusResponse,
51
49
  ModifyUserBasicInfoRequest,
50
+ EditContactRequest,
51
+ AuthorizeInfoRequest,
52
+ AuthorizeInfoResponse,
53
+ AuthorizeDeviceRequest,
54
+ AuthorizeRequest,
55
+ AuthorizeResponse,
52
56
  } from './models'
53
57
  import { SimpleStorage, RequestFunction } from '../oauth2client/interface'
54
58
  import { OAuth2Client, defaultStorage } from '../oauth2client/oauth2client'
@@ -60,6 +64,10 @@ import { getEncryptInfo } from '../utils/encrypt'
60
64
  export interface AuthOptions {
61
65
  apiOrigin: string
62
66
  clientId: string
67
+ /**
68
+ * basic auth
69
+ */
70
+ clientSecret?: string
63
71
  credentialsClient?: OAuth2Client
64
72
  request?: RequestFunction
65
73
  baseRequest?: RequestFunction
@@ -100,6 +108,8 @@ export class Auth {
100
108
  clientId: opts.clientId,
101
109
  storage: opts.storage,
102
110
  env: opts.env,
111
+ baseRequest: opts.baseRequest /* || opts.request */, // opts.request 废弃不用来表示 oauth rquest
112
+ anonymousSignInFunc: opts.anonymousSignInFunc,
103
113
  wxCloud: opts.wxCloud,
104
114
  }
105
115
  oAuth2Client = new OAuth2Client(initOptions)
@@ -210,6 +220,28 @@ export class Auth {
210
220
  return Promise.resolve(data)
211
221
  }
212
222
 
223
+ /**
224
+ * Revoke All Devices
225
+ * @return {Object} A Promise<void> object.
226
+ */
227
+ public async revokeAllDevices(): Promise<void> {
228
+ await this.config.request<void>(ApiUrls.AUTH_REVOKE_ALL_URL, {
229
+ method: 'DELETE',
230
+ withCredentials: true,
231
+ })
232
+ }
233
+
234
+ /**
235
+ * Revoke Device
236
+ * @return {Object} A Promise<void> object.
237
+ */
238
+ public async revokeDevice(params: { device_id: string }): Promise<void> {
239
+ await this.config.request<void>(ApiUrls.AUTHORIZED_DEVICES_DELETE_URL + params.device_id, {
240
+ method: 'DELETE',
241
+ withCredentials: true,
242
+ })
243
+ }
244
+
213
245
  /**
214
246
  * Get the verification.
215
247
  * @param {GetVerificationRequest} params A GetVerificationRequest Object.
@@ -493,25 +525,12 @@ export class Auth {
493
525
  }
494
526
 
495
527
  /**
496
- * check Password.
497
- * @param {CheckPasswordrRequest} params
498
- * @return {Promise<any>}
499
- */
500
- public async bindPhone(params: BindPhoneRequest): Promise<void> {
501
- return this.config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
502
- method: 'PATCH',
503
- withCredentials: true,
504
- body: params,
505
- })
506
- }
507
-
508
- /**
509
- * check Password.
510
- * @param {CheckPasswordrRequest} params
511
- * @return {Promise<any>}
528
+ * Edit Contact 修改 手机号 或 邮箱
529
+ * @param {EditContactRequest} params
530
+ * @return {Promise<void>}
512
531
  */
513
- public async bindEmail(params: BindEmailRequest): Promise<void> {
514
- return this.config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
532
+ public async editContact(params: EditContactRequest): Promise<void> {
533
+ return this.config.request<void>(`${ApiUrls.BIND_CONTACT_URL}`, {
515
534
  method: 'PATCH',
516
535
  withCredentials: true,
517
536
  body: params,
@@ -562,7 +581,7 @@ export class Auth {
562
581
  * @param {GetVerificationRequest} params A GetVerificationRequest Object.
563
582
  * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
564
583
  */
565
- public async getCurUserVerification(params: GetVerificationRequest): Promise<GetVerificationResponse> {
584
+ public async sendVerificationCodeToCurrentUser(params: GetVerificationRequest): Promise<GetVerificationResponse> {
566
585
  params.target = 'CUR_USER'
567
586
  return this.config.request<GetVerificationResponse>(ApiUrls.VERIFICATION_URL, {
568
587
  method: 'POST',
@@ -573,12 +592,12 @@ export class Auth {
573
592
  }
574
593
 
575
594
  /**
576
- * change binded provider.
577
- * @param {GetVerificationRequest} params A GetVerificationRequest Object.
578
- * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
595
+ * change Bound provider.
596
+ * @param {ChangeBoundProviderRequest} params A GetVerificationRequest Object.
597
+ * @return {Promise<ChangeBoundProviderResponse>} A Promise<GetVerificationResponse> object.
579
598
  */
580
- public async changeBindedProvider(params: ChangeBindedProviderRequest): Promise<ChangeBindedProviderResponse> {
581
- return this.config.request<ChangeBindedProviderResponse>(`${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`, {
599
+ public async changeBoundProvider(params: ChangeBoundProviderRequest): Promise<ChangeBoundProviderResponse> {
600
+ return this.config.request<ChangeBoundProviderResponse>(`${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`, {
582
601
  method: 'POST',
583
602
  body: {
584
603
  provider_trans_token: params.trans_token,
@@ -640,7 +659,14 @@ export class Auth {
640
659
  * @constructor
641
660
  */
642
661
  public async signInWithCustomTicket(params?: { version?: string }): Promise<Credentials> {
643
- const customTicket = await this.getCustomSignTicketFn()
662
+ const customSignTicketFn = this.getCustomSignTicketFn
663
+ if (!customSignTicketFn) {
664
+ return Promise.reject({
665
+ error: 'failed_precondition',
666
+ error_description: 'please use setCustomSignFunc to set custom sign function',
667
+ })
668
+ }
669
+ const customTicket = await customSignTicketFn()
644
670
  return this.signInWithProvider({
645
671
  ...params,
646
672
  provider_id: 'custom',
@@ -662,6 +688,31 @@ export class Auth {
662
688
  })
663
689
  }
664
690
 
691
+ /**
692
+ * Authorize oauth Authorize
693
+ * @param params
694
+ * @constructor
695
+ */
696
+ public async authorize(params: AuthorizeRequest): Promise<AuthorizeResponse> {
697
+ return this.config.request<AuthorizeResponse>(ApiUrls.AUTHORIZE_URL, {
698
+ method: 'POST',
699
+ withCredentials: true,
700
+ body: params,
701
+ })
702
+ }
703
+
704
+ /**
705
+ * authorize device
706
+ * @param params
707
+ */
708
+ public async authorizeDevice(params: AuthorizeDeviceRequest): Promise<void> {
709
+ return this.config.request<void>(ApiUrls.AUTHORIZE_DEVICE_URL, {
710
+ method: 'POST',
711
+ withCredentials: true,
712
+ body: params,
713
+ })
714
+ }
715
+
665
716
  /**
666
717
  * device authorization
667
718
  * @param {DeviceAuthorizeRequest} params
@@ -676,6 +727,27 @@ export class Auth {
676
727
  })
677
728
  }
678
729
 
730
+ /**
731
+ * OAuth get authorize info
732
+ * @param params
733
+ * @constructor
734
+ */
735
+ public async authorizeInfo(params: AuthorizeInfoRequest): Promise<AuthorizeInfoResponse> {
736
+ const url = `${ApiUrls.AUTHORIZE_INFO_URL}?${Auth.parseParamsToSearch(params)}`
737
+ let withBasicAuth = true
738
+ let withCredentials = false
739
+ const hasLogin = await this.hasLoginState()
740
+ if (hasLogin) {
741
+ withCredentials = true
742
+ withBasicAuth = false
743
+ }
744
+ return this.config.request<AuthorizeInfoResponse>(url, {
745
+ method: 'GET',
746
+ withBasicAuth,
747
+ withCredentials,
748
+ })
749
+ }
750
+
679
751
  public async checkUsername(params: CheckUsernameRequest): Promise<void> {
680
752
  return this.config.request(ApiUrls.CHECK_USERNAME, {
681
753
  method: 'GET',
@@ -804,7 +876,7 @@ export class Auth {
804
876
  * mini-program scan code status
805
877
  * @returns
806
878
  */
807
- public async checkMiniProgramCode(params: GetMiniProgramQrCodeStatusRequest,): Promise<GetMiniProgramQrCodeStatusResponse> {
879
+ public async getMiniProgramQrCodeStatus(params: GetMiniProgramQrCodeStatusRequest,): Promise<GetMiniProgramQrCodeStatusResponse> {
808
880
  return this.config.request<GetMiniProgramQrCodeStatusResponse>(ApiUrls.GET_MINIPROGRAM_QRCODE_STATUS, {
809
881
  method: 'POST',
810
882
  body: params,
@@ -31,6 +31,11 @@ export enum ApiUrls {
31
31
  AUTH_GET_DEVICE_CODE = '/auth/v1/device/code',
32
32
  CHECK_USERNAME = '/auth/v1/checkUsername',
33
33
  CHECK_IF_USER_EXIST = '/auth/v1/checkIfUserExist',
34
+ AUTHORIZE_URL = '/auth/v1/user/authorize',
35
+ AUTHORIZE_DEVICE_URL = '/auth/v1/user/device/authorize',
36
+ AUTHORIZE_INFO_URL = '/auth/v1/user/authorize/info',
37
+ AUTHORIZED_DEVICES_DELETE_URL = '/auth/v1/user/authorized/devices/',
38
+ AUTH_REVOKE_ALL_URL = '/auth/v1/user/revoke/all',
34
39
  GET_PROVIDER_TYPE = '/auth/v1/mgr/provider/providerSubType',
35
40
 
36
41
  GET_MINIPROGRAM_QRCODE = '/auth/v1/qrcode/generate',
@@ -4,12 +4,12 @@ interface BaseRequest {
4
4
 
5
5
  export type GetCustomSignTicketFn = () => Promise<string>;
6
6
 
7
- export interface SignInRequest extends BaseRequest, EncryptParams {
7
+ export interface SignInRequest extends BaseRequest, EncryptParams {
8
8
  username?: string;
9
9
  password?: string;
10
10
  verification_token?: string;
11
11
  version?: string;
12
- query?: { with_department?: boolean, with_role?: boolean }
12
+ query?: { with_department?: boolean; with_role?: boolean };
13
13
  }
14
14
 
15
15
  export interface SignInWithProviderRequest extends BaseRequest {
@@ -132,20 +132,20 @@ export interface UserProfile {
132
132
  zoneinfo?: string;
133
133
  locale?: string;
134
134
  created_from?: string;
135
- sub?: string
136
- uid?: string
135
+ sub?: string;
136
+ uid?: string;
137
137
  address?: {
138
- formatted?: string,
139
- street_address?: string,
140
- locality?: string,
141
- region?: string,
142
- postal_code?: string,
143
- country?: string
144
- }
145
- nickName?: string // TODO:
146
- province?: string // TODO:
147
- country?: string // TODO:
148
- city?: string // TODO:
138
+ formatted?: string;
139
+ street_address?: string;
140
+ locality?: string;
141
+ region?: string;
142
+ postal_code?: string;
143
+ country?: string;
144
+ };
145
+ nickName?: string; // TODO:
146
+ province?: string; // TODO:
147
+ country?: string; // TODO:
148
+ city?: string; // TODO:
149
149
  }
150
150
 
151
151
  export type UserInfo = UserProfile;
@@ -153,6 +153,9 @@ export type UserInfo = UserProfile;
153
153
  export interface ProviderProfile {
154
154
  provider_id: string;
155
155
  phone_number?: string;
156
+ name?: string;
157
+ picture?: string;
158
+ email?: string;
156
159
  }
157
160
 
158
161
  export interface TransByProviderRequest {
@@ -188,13 +191,11 @@ export interface SetPasswordRequest extends BaseRequest {
188
191
  sudo_token: string;
189
192
  }
190
193
 
191
- export interface ChangeBindedProviderRequest extends BaseRequest {
194
+ export interface ChangeBoundProviderRequest extends BaseRequest {
192
195
  trans_token: string;
193
196
  provider_id: string;
194
197
  }
195
198
 
196
- export type ChangeBindedProviderResponse = BaseRequest
197
-
198
199
  export interface QueryUserProfileReq extends BaseRequest {
199
200
  appended_params: string;
200
201
  }
@@ -256,7 +257,7 @@ export interface GrantProviderTokenRequest {
256
257
  provider_params?: {
257
258
  provider_code_type?: string;
258
259
  appid?: string;
259
- }
260
+ };
260
261
  }
261
262
 
262
263
  export interface GrantProviderTokenResponse {
@@ -287,7 +288,7 @@ export interface GenProviderRedirectUriRequest {
287
288
  provider_redirect_uri: string;
288
289
  state: string;
289
290
  other_params?: {
290
- [key: string]: string
291
+ [key: string]: string;
291
292
  };
292
293
  }
293
294
 
@@ -352,11 +353,23 @@ export interface CheckPasswordrRequest {
352
353
  password: string;
353
354
  }
354
355
 
356
+ export interface EditContactRequest {
357
+ phone_number?: string;
358
+ email?: string;
359
+ sudo_token: string;
360
+ verification_token: string;
361
+ // 冲突解决方案
362
+ // 可选:DEFAULT, 默认解决方案:提示用户该手机号已被其他人绑定
363
+ // DELETE_ACCOUNT_TRANSFER,标记原来的账号为已注销,并将手机换绑给自己
364
+ // TRANSFER 仅换绑手机号,不注销原有账号。(原有账号无法登录情况下,则自动标记为注销)
365
+ conflict_resolution?: string;
366
+ }
367
+
355
368
  export interface BindPhoneRequest {
356
369
  phone_number: string;
357
370
  sudo_token: string;
358
371
  verification_token: string;
359
- conflict_resolution: string
372
+ conflict_resolution: string;
360
373
  // 1. DEFAULT 0, 默认提示用户手机号已被绑定
361
374
  // 2. DELETE_ACCOUNT_TRANSFER 1, 标记原账号已被注销,并将手机换绑给自己
362
375
  // 3. TRANSFER 2, 仅换绑手机号,不注销原有账号(换绑后原账号无法登录时,则自动注销原账号)
@@ -373,7 +386,6 @@ export interface SetPasswordRequest {
373
386
  sudo_token: string;
374
387
  }
375
388
 
376
-
377
389
  export interface SetPasswordRequest {
378
390
  new_password: string;
379
391
  sudo_token: string;
@@ -387,11 +399,11 @@ export interface UpdatePasswordRequest {
387
399
  // password 和 verification_token 而选一,如果绑定了手机号,则必须使用verification_token 进行sudo
388
400
  export interface SudoRequest {
389
401
  password?: string;
390
- verification_token?: string
402
+ verification_token?: string;
391
403
  }
392
404
 
393
405
  export interface SudoResponse {
394
- sudo_token?: string
406
+ sudo_token?: string;
395
407
  }
396
408
 
397
409
  export interface WithSudoRequest {
@@ -399,7 +411,6 @@ export interface WithSudoRequest {
399
411
  version?: string;
400
412
  }
401
413
 
402
-
403
414
  export interface ChangeBoundProviderRequest {
404
415
  trans_token: string;
405
416
  provider_id: string;
@@ -418,27 +429,81 @@ export interface QueryUserProfileRequest {
418
429
 
419
430
  export interface QueryUserProfileResponse {
420
431
  total: string;
421
- data: SimpleUserProfile[]
432
+ data: SimpleUserProfile[];
422
433
  }
423
434
 
424
435
  export interface ResetPasswordRequest extends BaseRequest {
425
- email: string
426
- phone_number: string
427
- new_password: string
428
- verification_token: string
436
+ email: string;
437
+ phone_number: string;
438
+ new_password: string;
439
+ verification_token: string;
429
440
  }
430
441
 
431
442
  export interface DeviceAuthorizeRequest extends BaseRequest {
432
- scope?: string
443
+ scope?: string;
444
+ }
445
+
446
+ export interface AuthorizeRequest extends BaseRequest {
447
+ response_type?: string;
448
+ redirect_uri?: string;
449
+ state?: string;
450
+ scope?: string;
451
+ code_challenge?: string;
452
+ code_challenge_method?: string;
453
+ sign_out_uri?: string;
454
+ }
455
+
456
+ export interface AuthorizeResponse {
457
+ code?: string;
458
+ access_token?: string;
459
+ id_token?: string;
460
+ token_type?: string;
461
+ scope?: string;
462
+ state?: string;
463
+ expires_in?: number;
464
+ }
465
+
466
+ export interface AuthorizeInfoRequest extends BaseRequest {
467
+ response_type?: string;
468
+ redirect_uri?: string;
469
+ state?: string;
470
+ scope?: string;
471
+ sign_out_uri?: string;
472
+ locale?: string;
473
+ }
474
+
475
+ interface Scope {
476
+ id: string;
477
+ name: string;
478
+ description?: string;
479
+ picture?: string;
480
+ url?: string;
481
+ children?: Scope[];
482
+ }
483
+ export interface AuthorizeInfoResponse {
484
+ client: {
485
+ id: string;
486
+ name: string;
487
+ description?: string;
488
+ picture?: string;
489
+ url?: string;
490
+ };
491
+ scopes?: Scope[];
492
+ }
493
+
494
+ export interface AuthorizeDeviceRequest extends BaseRequest {
495
+ user_code: string;
496
+ scope?: string;
497
+ state?: string;
433
498
  }
434
499
 
435
500
  export interface DeviceAuthorizeResponse {
436
- device_code: string
437
- user_code: string
438
- expires_in: number
439
- interval: number
440
- verification_url: string
441
- verification_uri_complete: string
501
+ device_code: string;
502
+ user_code: string;
503
+ expires_in: number;
504
+ interval: number;
505
+ verification_url: string;
506
+ verification_uri_complete: string;
442
507
  }
443
508
 
444
509
  // 简化版用户信息
@@ -453,7 +518,7 @@ export interface SimpleUserProfile {
453
518
  }
454
519
 
455
520
  export interface CheckUsernameRequest {
456
- username: string
521
+ username: string;
457
522
  }
458
523
 
459
524
  export interface CheckIfUserExistRequest {
package/src/index.ts CHANGED
@@ -17,17 +17,12 @@ export {
17
17
 
18
18
  export { AuthClient, SimpleStorage } from './oauth2client/interface'
19
19
 
20
- export {
21
- Credentials,
22
- ResponseError,
23
- OAuth2ClientOptions,
24
- AuthClientRequestOptions,
25
- } from './oauth2client/models'
20
+ export { Credentials, ResponseError, OAuth2ClientOptions, AuthClientRequestOptions } from './oauth2client/models'
26
21
 
27
22
  export { AuthOptions, Auth } from './auth/apis'
28
23
  export { authModels }
29
- export { ApiUrls } from './auth/consts'
30
-
24
+ export { ApiUrls } from './auth/consts'
25
+ export type { ProviderProfile } from './auth/models'
31
26
 
32
27
  export class CloudbaseOAuth {
33
28
  public oauth2client: OAuth2Client
@@ -53,4 +48,3 @@ export class CloudbaseOAuth {
53
48
  })
54
49
  }
55
50
  }
56
-
@@ -41,6 +41,7 @@ export interface AuthClientRequestOptions extends RequestOptions {
41
41
  [key: string]: any;
42
42
  } | null;
43
43
  withCredentials?: boolean;
44
+ withBasicAuth?: boolean;
44
45
  retry?: number;
45
46
  useWxCloud?: boolean;
46
47
 
@@ -17,6 +17,7 @@ import { uuidv4 } from '../utils/uuid'
17
17
  import { getPathName } from '../utils/index'
18
18
 
19
19
  import { SinglePromise } from '../utils/function/single-promise'
20
+ import { weBtoa } from '../utils/base64'
20
21
 
21
22
  const RequestIdHeaderName = 'x-request-id'
22
23
  const DeviceIdHeaderName = 'x-device-id'
@@ -99,12 +100,17 @@ export function generateRequestId(): string {
99
100
  * Default Storage.
100
101
  */
101
102
  class DefaultStorage implements SimpleStorage {
103
+ private readonly _env: string
104
+
105
+ constructor(opts?: { env: string }) {
106
+ this._env = opts?.env || ''
107
+ }
102
108
  /**
103
109
  * Get item.
104
110
  * @param {string} key
105
111
  */
106
112
  async getItem(key: string): Promise<string | null> {
107
- return window.localStorage.getItem(key)
113
+ return window.localStorage.getItem(`${key}${this._env}`)
108
114
  }
109
115
 
110
116
  /**
@@ -112,7 +118,7 @@ class DefaultStorage implements SimpleStorage {
112
118
  * @param {string} key
113
119
  */
114
120
  async removeItem(key: string): Promise<void> {
115
- window.localStorage.removeItem(key)
121
+ window.localStorage.removeItem(`${key}${this._env}`)
116
122
  }
117
123
 
118
124
  /**
@@ -121,7 +127,7 @@ class DefaultStorage implements SimpleStorage {
121
127
  * @param {string} value
122
128
  */
123
129
  async setItem(key: string, value: string): Promise<void> {
124
- window.localStorage.setItem(key, value)
130
+ window.localStorage.setItem(`${key}${this._env}`, value)
125
131
  }
126
132
 
127
133
  /**
@@ -129,7 +135,7 @@ class DefaultStorage implements SimpleStorage {
129
135
  * @param {string} key
130
136
  */
131
137
  getItemSync(key: string): string | null {
132
- return window.localStorage.getItem(key)
138
+ return window.localStorage.getItem(`${key}${this._env}`)
133
139
  }
134
140
 
135
141
  /**
@@ -137,7 +143,7 @@ class DefaultStorage implements SimpleStorage {
137
143
  * @param {string} key
138
144
  */
139
145
  removeItemSync(key: string): void {
140
- window.localStorage.removeItem(key)
146
+ window.localStorage.removeItem(`${key}${this._env}`)
141
147
  }
142
148
 
143
149
  /**
@@ -146,7 +152,7 @@ class DefaultStorage implements SimpleStorage {
146
152
  * @param {string} value
147
153
  */
148
154
  setItemSync(key: string, value: string): void {
149
- window.localStorage.setItem(key, value)
155
+ window.localStorage.setItem(`${key}${this._env}`, value)
150
156
  }
151
157
  }
152
158
 
@@ -289,6 +295,7 @@ export class OAuth2Client implements AuthClient {
289
295
  private singlePromise: SinglePromise = new SinglePromise()
290
296
  private anonymousSignInFunc: (Credentials) => Promise<Credentials | void>
291
297
  private wxCloud: any
298
+ private basicAuth: string
292
299
 
293
300
  /**
294
301
  * constructor
@@ -306,12 +313,15 @@ export class OAuth2Client implements AuthClient {
306
313
  this.tokenInURL = options.tokenInURL
307
314
  this.headers = options.headers
308
315
  // @ts-ignore
309
- this.storage = options.storage || defaultStorage
316
+ this.storage = options.storage || new DefaultStorage({ env: options.env })
310
317
  this.localCredentials = new LocalCredentials({
311
318
  tokenSectionName: `credentials_${options.clientId}`,
312
319
  storage: this.storage,
313
320
  })
314
321
  this.clientSecret = options.clientSecret
322
+ if (options.clientId !== '') {
323
+ this.basicAuth = `Basic ${weBtoa(`${options.clientId}:${options.clientSecret}`)}`
324
+ }
315
325
  this.wxCloud = options.wxCloud
316
326
  try {
317
327
  if (adapterForWxMp.isMatch() && this.wxCloud === undefined && options.env) {
@@ -370,6 +380,9 @@ export class OAuth2Client implements AuthClient {
370
380
  const deviceId = await this.getDeviceId()
371
381
  options.headers[DeviceIdHeaderName] = deviceId
372
382
  }
383
+ if (options?.withBasicAuth && this.basicAuth) {
384
+ options.headers.Authorization = this.basicAuth
385
+ }
373
386
  if (options?.withCredentials) {
374
387
  const credentials = await this.getCredentials()
375
388
  if (credentials) {
@@ -416,10 +429,7 @@ export class OAuth2Client implements AuthClient {
416
429
  return response
417
430
  }
418
431
 
419
- public async wxCloudCallFunction<T>(
420
- url: string,
421
- options?: RequestOptions,
422
- ): Promise<T> {
432
+ public async wxCloudCallFunction<T>(url: string, options?: RequestOptions): Promise<T> {
423
433
  let result: T | null = null
424
434
  let responseError: ResponseError | null = null
425
435
  try {
@@ -462,6 +472,9 @@ export class OAuth2Client implements AuthClient {
462
472
  */
463
473
  public async getCredentials(): Promise<Credentials | null> {
464
474
  let credentials: Credentials = await this.localCredentials.getCredentials()
475
+ if (!credentials) {
476
+ return this.unAuthenticatedError('credentials not found')
477
+ }
465
478
  if (isCredentialsExpired(credentials)) {
466
479
  if (credentials && credentials.scope === 'anonymous') {
467
480
  if (this.anonymousSignInFunc) {
@@ -585,12 +598,10 @@ export class OAuth2Client implements AuthClient {
585
598
  return this.unAuthenticatedError('no anonymous in credentials')
586
599
  }
587
600
  try {
588
- const newCredentials: Credentials = await this.request('/auth/v1/signin/anonymously', {
601
+ const newCredentials: Credentials = await this.request(ApiUrls.AUTH_SIGN_IN_ANONYMOUSLY_URL, {
589
602
  method: 'POST',
590
- body: {
591
- client_id: this.clientId,
592
- client_secret: this.clientSecret,
593
- },
603
+ withBasicAuth: true,
604
+ body: {},
594
605
  })
595
606
  await this.localCredentials.setCredentials(newCredentials)
596
607
  return newCredentials