@cloudbase/oauth 2.6.3-beta.0 → 2.6.5-beta.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 (68) hide show
  1. package/dist/cjs/auth/apis.d.ts +17 -8
  2. package/dist/cjs/auth/apis.js +166 -23
  3. package/dist/cjs/auth/consts.d.ts +39 -4
  4. package/dist/cjs/auth/consts.js +38 -3
  5. package/dist/cjs/auth/models.d.ts +83 -6
  6. package/dist/cjs/auth/models.js +1 -1
  7. package/dist/cjs/index.d.ts +5 -8
  8. package/dist/cjs/index.js +3 -17
  9. package/dist/cjs/oauth2client/consts.d.ts +1 -48
  10. package/dist/cjs/oauth2client/consts.js +4 -51
  11. package/dist/cjs/oauth2client/models.d.ts +1 -0
  12. package/dist/cjs/oauth2client/models.js +1 -1
  13. package/dist/cjs/oauth2client/oauth2client.d.ts +5 -0
  14. package/dist/cjs/oauth2client/oauth2client.js +24 -18
  15. package/dist/cjs/utils/base64.d.ts +4 -0
  16. package/dist/cjs/utils/base64.js +99 -0
  17. package/dist/cjs/utils/cloudbase-adapter-wx_mp.d.ts +1 -0
  18. package/dist/cjs/utils/cloudbase-adapter-wx_mp.js +40 -0
  19. package/dist/cjs/utils/encrypt.d.ts +2 -2
  20. package/dist/cjs/utils/encrypt.js +5 -5
  21. package/dist/cjs/utils/encryptlong/index.d.ts +537 -145
  22. package/dist/cjs/utils/encryptlong/index.js +2712 -3765
  23. package/dist/esm/auth/apis.d.ts +17 -8
  24. package/dist/esm/auth/apis.js +468 -722
  25. package/dist/esm/auth/consts.d.ts +39 -4
  26. package/dist/esm/auth/consts.js +37 -3
  27. package/dist/esm/auth/models.d.ts +83 -6
  28. package/dist/esm/auth/models.js +0 -1
  29. package/dist/esm/captcha/captcha.js +155 -258
  30. package/dist/esm/index.d.ts +5 -8
  31. package/dist/esm/index.js +17 -31
  32. package/dist/esm/oauth2client/consts.d.ts +1 -48
  33. package/dist/esm/oauth2client/consts.js +1 -50
  34. package/dist/esm/oauth2client/interface.js +2 -7
  35. package/dist/esm/oauth2client/models.d.ts +1 -0
  36. package/dist/esm/oauth2client/models.js +0 -1
  37. package/dist/esm/oauth2client/oauth2client.d.ts +5 -0
  38. package/dist/esm/oauth2client/oauth2client.js +365 -631
  39. package/dist/esm/utils/base64.d.ts +4 -0
  40. package/dist/esm/utils/base64.js +91 -0
  41. package/dist/esm/utils/cloudbase-adapter-wx_mp.d.ts +1 -0
  42. package/dist/esm/utils/cloudbase-adapter-wx_mp.js +35 -0
  43. package/dist/esm/utils/encrypt.d.ts +2 -2
  44. package/dist/esm/utils/encrypt.js +5 -7
  45. package/dist/esm/utils/encryptlong/index.d.ts +537 -145
  46. package/dist/esm/utils/encryptlong/index.js +2605 -3687
  47. package/dist/esm/utils/function/single-promise.js +26 -89
  48. package/dist/esm/utils/index.js +7 -8
  49. package/dist/esm/utils/mp.js +2 -3
  50. package/dist/esm/utils/uuid.js +3 -4
  51. package/dist/miniprogram/index.js +1 -0
  52. package/package.json +11 -6
  53. package/src/auth/apis.ts +179 -36
  54. package/src/auth/consts.ts +37 -3
  55. package/src/auth/models.ts +318 -234
  56. package/src/index.ts +5 -29
  57. package/src/oauth2client/consts.ts +1 -51
  58. package/src/oauth2client/models.ts +1 -0
  59. package/src/oauth2client/oauth2client.ts +31 -17
  60. package/src/utils/base64.ts +100 -0
  61. package/src/utils/cloudbase-adapter-wx_mp.ts +42 -0
  62. package/src/utils/encrypt.ts +8 -6
  63. package/src/utils/encryptlong/index.js +2843 -4895
  64. package/tsconfig.esm.json +8 -19
  65. package/tsconfig.json +5 -16
  66. package/webpack/web.prod.js +100 -0
  67. package/webpack/webpack.miniprogram.js +23 -0
  68. package/src/index.d.ts +0 -1
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,17 +47,41 @@ import {
49
47
  GetMiniProgramQrCodeResponse,
50
48
  GetMiniProgramQrCodeStatusResponse,
51
49
  ModifyUserBasicInfoRequest,
50
+ EditContactRequest,
51
+ AuthorizeInfoRequest,
52
+ AuthorizeInfoResponse,
53
+ AuthorizeDeviceRequest,
54
+ AuthorizeRequest,
55
+ AuthorizeResponse,
56
+ GetUserBehaviorLog,
57
+ GetUserBehaviorLogRes,
52
58
  } from './models'
53
59
  import { SimpleStorage, RequestFunction } from '../oauth2client/interface'
54
60
  import { OAuth2Client, defaultStorage } from '../oauth2client/oauth2client'
55
61
  import { Credentials } from '../oauth2client/models'
56
62
  import { Captcha, CaptchaOptions } from '../captcha/captcha'
57
63
  import { deepClone } from '../utils'
58
- import { getEncryptInfo } from '../utils/encrypt'
64
+
65
+ function getEncryptUtils(isEncrypt) {
66
+ if (globalThis.IS_MP_BUILD) {
67
+ return
68
+ }
69
+ if (isEncrypt) {
70
+ /* eslint-disable */
71
+ // @ts-ignore
72
+ const utils = require('../utils/encrypt')
73
+ /* eslint-enable */
74
+ return utils
75
+ }
76
+ }
59
77
 
60
78
  export interface AuthOptions {
61
79
  apiOrigin: string
62
80
  clientId: string
81
+ /**
82
+ * basic auth
83
+ */
84
+ clientSecret?: string
63
85
  credentialsClient?: OAuth2Client
64
86
  request?: RequestFunction
65
87
  baseRequest?: RequestFunction
@@ -100,6 +122,8 @@ export class Auth {
100
122
  clientId: opts.clientId,
101
123
  storage: opts.storage,
102
124
  env: opts.env,
125
+ baseRequest: opts.baseRequest /* || opts.request */, // opts.request 废弃不用来表示 oauth rquest
126
+ anonymousSignInFunc: opts.anonymousSignInFunc,
103
127
  wxCloud: opts.wxCloud,
104
128
  }
105
129
  oAuth2Client = new OAuth2Client(initOptions)
@@ -210,6 +234,28 @@ export class Auth {
210
234
  return Promise.resolve(data)
211
235
  }
212
236
 
237
+ /**
238
+ * Revoke All Devices
239
+ * @return {Object} A Promise<void> object.
240
+ */
241
+ public async revokeAllDevices(): Promise<void> {
242
+ await this.config.request<void>(ApiUrls.AUTH_REVOKE_ALL_URL, {
243
+ method: 'DELETE',
244
+ withCredentials: true,
245
+ })
246
+ }
247
+
248
+ /**
249
+ * Revoke Device
250
+ * @return {Object} A Promise<void> object.
251
+ */
252
+ public async revokeDevice(params: { device_id: string }): Promise<void> {
253
+ await this.config.request<void>(ApiUrls.AUTHORIZED_DEVICES_DELETE_URL + params.device_id, {
254
+ method: 'DELETE',
255
+ withCredentials: true,
256
+ })
257
+ }
258
+
213
259
  /**
214
260
  * Get the verification.
215
261
  * @param {GetVerificationRequest} params A GetVerificationRequest Object.
@@ -493,25 +539,12 @@ export class Auth {
493
539
  }
494
540
 
495
541
  /**
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>}
542
+ * Edit Contact 修改 手机号 或 邮箱
543
+ * @param {EditContactRequest} params
544
+ * @return {Promise<void>}
512
545
  */
513
- public async bindEmail(params: BindEmailRequest): Promise<void> {
514
- return this.config.request<any>(`${ApiUrls.BIND_CONTACT_URL}`, {
546
+ public async editContact(params: EditContactRequest): Promise<void> {
547
+ return this.config.request<void>(`${ApiUrls.BIND_CONTACT_URL}`, {
515
548
  method: 'PATCH',
516
549
  withCredentials: true,
517
550
  body: params,
@@ -562,7 +595,7 @@ export class Auth {
562
595
  * @param {GetVerificationRequest} params A GetVerificationRequest Object.
563
596
  * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
564
597
  */
565
- public async getCurUserVerification(params: GetVerificationRequest): Promise<GetVerificationResponse> {
598
+ public async sendVerificationCodeToCurrentUser(params: GetVerificationRequest): Promise<GetVerificationResponse> {
566
599
  params.target = 'CUR_USER'
567
600
  return this.config.request<GetVerificationResponse>(ApiUrls.VERIFICATION_URL, {
568
601
  method: 'POST',
@@ -573,12 +606,12 @@ export class Auth {
573
606
  }
574
607
 
575
608
  /**
576
- * change binded provider.
577
- * @param {GetVerificationRequest} params A GetVerificationRequest Object.
578
- * @return {Promise<GetVerificationResponse>} A Promise<GetVerificationResponse> object.
609
+ * change Bound provider.
610
+ * @param {ChangeBoundProviderRequest} params A GetVerificationRequest Object.
611
+ * @return {Promise<ChangeBoundProviderResponse>} A Promise<GetVerificationResponse> object.
579
612
  */
580
- public async changeBindedProvider(params: ChangeBindedProviderRequest): Promise<ChangeBindedProviderResponse> {
581
- return this.config.request<ChangeBindedProviderResponse>(`${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`, {
613
+ public async changeBoundProvider(params: ChangeBoundProviderRequest): Promise<ChangeBoundProviderResponse> {
614
+ return this.config.request<ChangeBoundProviderResponse>(`${ApiUrls.PROVIDER_LIST}/${params.provider_id}/trans`, {
582
615
  method: 'POST',
583
616
  body: {
584
617
  provider_trans_token: params.trans_token,
@@ -640,7 +673,14 @@ export class Auth {
640
673
  * @constructor
641
674
  */
642
675
  public async signInWithCustomTicket(params?: { version?: string }): Promise<Credentials> {
643
- const customTicket = await this.getCustomSignTicketFn()
676
+ const customSignTicketFn = this.getCustomSignTicketFn
677
+ if (!customSignTicketFn) {
678
+ return Promise.reject({
679
+ error: 'failed_precondition',
680
+ error_description: 'please use setCustomSignFunc to set custom sign function',
681
+ })
682
+ }
683
+ const customTicket = await customSignTicketFn()
644
684
  return this.signInWithProvider({
645
685
  ...params,
646
686
  provider_id: 'custom',
@@ -662,6 +702,31 @@ export class Auth {
662
702
  })
663
703
  }
664
704
 
705
+ /**
706
+ * Authorize oauth Authorize
707
+ * @param params
708
+ * @constructor
709
+ */
710
+ public async authorize(params: AuthorizeRequest): Promise<AuthorizeResponse> {
711
+ return this.config.request<AuthorizeResponse>(ApiUrls.AUTHORIZE_URL, {
712
+ method: 'POST',
713
+ withCredentials: true,
714
+ body: params,
715
+ })
716
+ }
717
+
718
+ /**
719
+ * authorize device
720
+ * @param params
721
+ */
722
+ public async authorizeDevice(params: AuthorizeDeviceRequest): Promise<void> {
723
+ return this.config.request<void>(ApiUrls.AUTHORIZE_DEVICE_URL, {
724
+ method: 'POST',
725
+ withCredentials: true,
726
+ body: params,
727
+ })
728
+ }
729
+
665
730
  /**
666
731
  * device authorization
667
732
  * @param {DeviceAuthorizeRequest} params
@@ -676,6 +741,27 @@ export class Auth {
676
741
  })
677
742
  }
678
743
 
744
+ /**
745
+ * OAuth get authorize info
746
+ * @param params
747
+ * @constructor
748
+ */
749
+ public async authorizeInfo(params: AuthorizeInfoRequest): Promise<AuthorizeInfoResponse> {
750
+ const url = `${ApiUrls.AUTHORIZE_INFO_URL}?${Auth.parseParamsToSearch(params)}`
751
+ let withBasicAuth = true
752
+ let withCredentials = false
753
+ const hasLogin = await this.hasLoginState()
754
+ if (hasLogin) {
755
+ withCredentials = true
756
+ withBasicAuth = false
757
+ }
758
+ return this.config.request<AuthorizeInfoResponse>(url, {
759
+ method: 'GET',
760
+ withBasicAuth,
761
+ withCredentials,
762
+ })
763
+ }
764
+
679
765
  public async checkUsername(params: CheckUsernameRequest): Promise<void> {
680
766
  return this.config.request(ApiUrls.CHECK_USERNAME, {
681
767
  method: 'GET',
@@ -729,9 +815,14 @@ export class Auth {
729
815
  * @returns
730
816
  */
731
817
  public async getEncryptParams(params: Record<any, any>): Promise<EncryptParams> {
818
+ const { isEncrypt } = params
819
+ delete params.isEncrypt
820
+
732
821
  const payload = deepClone(params)
733
822
 
734
- if (!payload.isEncrypt) {
823
+ const encryptUtils = getEncryptUtils(isEncrypt)
824
+
825
+ if (!encryptUtils) {
735
826
  return params
736
827
  }
737
828
 
@@ -748,10 +839,8 @@ export class Auth {
748
839
  throw new Error('public_key or public_key_thumbprint is empty')
749
840
  }
750
841
 
751
- delete payload.isEncrypt
752
-
753
842
  return {
754
- params: getEncryptInfo({ publicKey, payload }),
843
+ params: encryptUtils.getEncryptInfo({ publicKey, payload }),
755
844
  public_key_thumbprint,
756
845
  }
757
846
  }
@@ -782,7 +871,7 @@ export class Auth {
782
871
  }
783
872
 
784
873
  public async createCaptchaData({ state, redirect_uri = undefined }) {
785
- return this.config.request<{ captcha_token: string; expires_in: number }>(ApiUrls.CAPTCHA_DATA_URL, {
874
+ return this.config.request<{ token: string; data: string }>(ApiUrls.CAPTCHA_DATA_URL, {
786
875
  method: 'POST',
787
876
  body: { state, redirect_uri },
788
877
  withCredentials: false,
@@ -804,10 +893,64 @@ export class Auth {
804
893
  * mini-program scan code status
805
894
  * @returns
806
895
  */
807
- public async checkMiniProgramCode(params: GetMiniProgramQrCodeStatusRequest,): Promise<GetMiniProgramQrCodeStatusResponse> {
896
+ public async getMiniProgramQrCodeStatus(params: GetMiniProgramQrCodeStatusRequest,): Promise<GetMiniProgramQrCodeStatusResponse> {
808
897
  return this.config.request<GetMiniProgramQrCodeStatusResponse>(ApiUrls.GET_MINIPROGRAM_QRCODE_STATUS, {
809
898
  method: 'POST',
810
899
  body: params,
811
900
  })
812
901
  }
902
+
903
+ /**
904
+ * get user behavior log
905
+ * @param params
906
+ */
907
+ public async getUserBehaviorLog(params: GetUserBehaviorLog): Promise<GetUserBehaviorLogRes> {
908
+ const action = { LOGIN: 'query[action]=USER_LOGIN', MODIFY: 'ne_query[action]=USER_LOGIN' }
909
+ const url = `${ApiUrls.GET_USER_BEHAVIOR_LOG}?${action[params.type]}&limit=${params.limit}${
910
+ params.page_token ? `&page_token=${params.page_token}` : ''
911
+ }`
912
+ return this.config.request(url, {
913
+ method: 'GET',
914
+ withCredentials: true,
915
+ })
916
+ }
917
+
918
+ /**
919
+ * 这个方法是用户自己修改自己的密码,不同于/auth/v1/user/password接口,该接口是管理员修改个人的
920
+ * @param {SignInRequest} params A SignInRequest Object.
921
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
922
+ */
923
+ public async modifyPassword(params: ModifyUserBasicInfoRequest): Promise<void> {
924
+ let publicKey = ''
925
+ let public_key_thumbprint = ''
926
+
927
+ const encryptUtils = getEncryptUtils(true)
928
+
929
+ if (!encryptUtils) {
930
+ throw new Error('do not support encrypt, a encrypt util required.')
931
+ }
932
+
933
+ try {
934
+ const res = await this.getPublicKey()
935
+ publicKey = res.public_key
936
+ public_key_thumbprint = res.public_key_thumbprint
937
+ } catch (error) {}
938
+
939
+ if (!publicKey || !public_key_thumbprint) {
940
+ throw new Error('public_key or public_key_thumbprint is empty')
941
+ }
942
+
943
+ const encrypt_password = params.password ? encryptUtils.getEncryptInfo({ publicKey, payload: params.password }) : ''
944
+ const encrypt_new_password = encryptUtils.getEncryptInfo({ publicKey, payload: params.new_password })
945
+ return this.config.request(ApiUrls.USER_BASIC_EDIT_URL, {
946
+ method: 'POST',
947
+ withCredentials: true,
948
+ body: {
949
+ user_id: params.user_id,
950
+ encrypt_password,
951
+ encrypt_new_password,
952
+ public_key_thumbprint,
953
+ },
954
+ })
955
+ }
813
956
  }
@@ -31,10 +31,15 @@ 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
-
36
40
  GET_MINIPROGRAM_QRCODE = '/auth/v1/qrcode/generate',
37
41
  GET_MINIPROGRAM_QRCODE_STATUS = '/auth/v1/qrcode/get/status',
42
+ GET_USER_BEHAVIOR_LOG = '/auth/v1/user/security/history',
38
43
  }
39
44
 
40
45
  export enum ApiUrlsV2 {
@@ -55,6 +60,13 @@ export enum VerificationUsages {
55
60
  }
56
61
 
57
62
  export enum ErrorType {
63
+ UNREACHABLE = 'unreachable',
64
+ LOCAL = 'local',
65
+ CANCELLED = 'cancelled',
66
+ UNKNOWN = 'unknown',
67
+ UNAUTHENTICATED = 'unauthenticated',
68
+ RESOURCE_EXHAUSTED = 'resource_exhausted',
69
+ FAILED_PRECONDITION = 'failed_precondition',
58
70
  INVALID_ARGUMENT = 'invalid_argument',
59
71
  DEADLINE_EXCEEDED = 'deadline_exceeded',
60
72
  NOT_FOUND = 'not_found',
@@ -67,11 +79,33 @@ export enum ErrorType {
67
79
  UNAVAILABLE = 'unavailable',
68
80
  DATA_LOSS = 'data_loss',
69
81
  // CommonError
70
- CAPTCHA_REQUIRED = 'captcha_required',
71
- CAPTCHA_INVALID = 'captcha_invalid',
72
82
  INVALID_PASSWORD = 'invalid_password',
73
83
  PASSWORD_NOT_SET = 'password_not_set',
74
84
  INVALID_STATUS = 'invalid_status',
75
85
  USER_PENDING = 'user_pending',
76
86
  USER_BLOCKED = 'user_blocked',
87
+ INVALID_VERIFICATION_CODE = 'invalid_verification_code',
88
+ TWO_FACTOR_REQUIRED = 'two_factor_required',
89
+ INVALID_TWO_FACTOR = 'invalid_two_factor',
90
+ INVALID_TWO_FACTOR_RECOVERY = 'invalid_two_factor_recovery',
91
+ UNDER_REVIEW = 'under_review',
92
+ INVALID_REQUEST = 'invalid_request',
93
+ UNAUTHORIZED_CLIENT = 'unauthorized_client',
94
+ ACCESS_DENIED = 'access_denied',
95
+ UNSUPPORTED_RESPONSE_TYPE = 'unsupported_response_type',
96
+ INVALID_SCOPE = 'invalid_scope',
97
+ INVALID_GRANT = 'invalid_grant',
98
+ SERVER_ERROR = 'server_error',
99
+ TEMPORARILY_UNAVAILABLE = 'temporarily_unavailable',
100
+ INTERACTION_REQUIRED = 'interaction_required',
101
+ LOGIN_REQUIRED = 'login_required',
102
+ ACCOUNT_SELECTION_REQUIRED = 'account_selection_required',
103
+ CONSENT_REQUIRED = 'consent_required',
104
+ INVALID_REQUEST_URI = 'invalid_request_uri',
105
+ INVALID_REQUEST_OBJECT = 'invalid_request_object',
106
+ REQUEST_NOT_SUPPORTED = 'request_not_supported',
107
+ REQUEST_URI_NOT_SUPPORTED = 'request_uri_not_supported',
108
+ REGISTRATION_NOT_SUPPORTED = 'registration_not_supported',
109
+ CAPTCHA_REQUIRED = 'captcha_required',
110
+ CAPTCHA_INVALID = 'captcha_invalid',
77
111
  }