@cloudbase/oauth 2.19.2 → 2.19.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,62 +1,64 @@
1
+ export const AUTH_API_PREFIX = '/auth'
2
+
1
3
  export enum ApiUrls {
2
- AUTH_SIGN_UP_URL = '/auth/v1/signup',
3
- AUTH_TOKEN_URL = '/auth/v1/token',
4
- AUTH_REVOKE_URL = '/auth/v1/revoke',
5
- WEDA_USER_URL = '/auth/v1/plugin/weda/userinfo',
6
- AUTH_RESET_PASSWORD = '/auth/v1/reset',
7
- AUTH_GET_DEVICE_CODE = '/auth/v1/device/code',
8
- CHECK_USERNAME = '/auth/v1/checkUsername',
9
- CHECK_IF_USER_EXIST = '/auth/v1/checkIfUserExist',
10
- GET_PROVIDER_TYPE = '/auth/v1/mgr/provider/providerSubType',
4
+ AUTH_SIGN_UP_URL = '/v1/signup',
5
+ AUTH_TOKEN_URL = '/v1/token',
6
+ AUTH_REVOKE_URL = '/v1/revoke',
7
+ WEDA_USER_URL = '/v1/plugin/weda/userinfo',
8
+ AUTH_RESET_PASSWORD = '/v1/reset',
9
+ AUTH_GET_DEVICE_CODE = '/v1/device/code',
10
+ CHECK_USERNAME = '/v1/checkUsername',
11
+ CHECK_IF_USER_EXIST = '/v1/checkIfUserExist',
12
+ GET_PROVIDER_TYPE = '/v1/mgr/provider/providerSubType',
11
13
 
12
- AUTH_SIGN_IN_URL = '/auth/v1/signin',
13
- AUTH_SIGN_IN_ANONYMOUSLY_URL = '/auth/v1/signin/anonymously',
14
- AUTH_SIGN_IN_WITH_PROVIDER_URL = '/auth/v1/signin/with/provider',
15
- AUTH_SIGN_IN_WITH_WECHAT_URL = '/auth/v1/signin/wechat/noauth',
16
- AUTH_SIGN_IN_CUSTOM = '/auth/v1/signin/custom',
14
+ AUTH_SIGN_IN_URL = '/v1/signin',
15
+ AUTH_SIGN_IN_ANONYMOUSLY_URL = '/v1/signin/anonymously',
16
+ AUTH_SIGN_IN_WITH_PROVIDER_URL = '/v1/signin/with/provider',
17
+ AUTH_SIGN_IN_WITH_WECHAT_URL = '/v1/signin/wechat/noauth',
18
+ AUTH_SIGN_IN_CUSTOM = '/v1/signin/custom',
17
19
 
18
- PROVIDER_TOKEN_URL = '/auth/v1/provider/token',
19
- PROVIDER_URI_URL = '/auth/v1/provider/uri',
20
+ PROVIDER_TOKEN_URL = '/v1/provider/token',
21
+ PROVIDER_URI_URL = '/v1/provider/uri',
20
22
 
21
- USER_ME_URL = '/auth/v1/user/me',
22
- AUTH_SIGNOUT_URL = '/auth/v1/user/signout',
23
- USER_QUERY_URL = '/auth/v1/user/query',
24
- USER_PRIFILE_URL = '/auth/v1/user/profile',
25
- USER_BASIC_EDIT_URL = '/auth/v1/user/basic/edit',
26
- USER_TRANS_BY_PROVIDER_URL = '/auth/v1/user/trans/by/provider',
27
- PROVIDER_LIST = '/auth/v1/user/provider',
28
- PROVIDER_BIND_URL = '/auth/v1/user/provider/bind',
29
- PROVIDER_UNBIND_URL = '/auth/v1/user/provider',
30
- CHECK_PWD_URL = '/auth/v1/user/sudo',
31
- SUDO_URL = '/auth/v1/user/sudo',
32
- BIND_CONTACT_URL = '/auth/v1/user/contact',
33
- AUTH_SET_PASSWORD = '/auth/v1/user/password',
34
- AUTHORIZE_DEVICE_URL = '/auth/v1/user/device/authorize',
35
- AUTHORIZE_URL = '/auth/v1/user/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',
39
- GET_USER_BEHAVIOR_LOG = '/auth/v1/user/security/history',
23
+ USER_ME_URL = '/v1/user/me',
24
+ AUTH_SIGNOUT_URL = '/v1/user/signout',
25
+ USER_QUERY_URL = '/v1/user/query',
26
+ USER_PRIFILE_URL = '/v1/user/profile',
27
+ USER_BASIC_EDIT_URL = '/v1/user/basic/edit',
28
+ USER_TRANS_BY_PROVIDER_URL = '/v1/user/trans/by/provider',
29
+ PROVIDER_LIST = '/v1/user/provider',
30
+ PROVIDER_BIND_URL = '/v1/user/provider/bind',
31
+ PROVIDER_UNBIND_URL = '/v1/user/provider',
32
+ CHECK_PWD_URL = '/v1/user/sudo',
33
+ SUDO_URL = '/v1/user/sudo',
34
+ BIND_CONTACT_URL = '/v1/user/contact',
35
+ AUTH_SET_PASSWORD = '/v1/user/password',
36
+ AUTHORIZE_DEVICE_URL = '/v1/user/device/authorize',
37
+ AUTHORIZE_URL = '/v1/user/authorize',
38
+ AUTHORIZE_INFO_URL = '/v1/user/authorize/info',
39
+ AUTHORIZED_DEVICES_DELETE_URL = '/v1/user/authorized/devices/',
40
+ AUTH_REVOKE_ALL_URL = '/v1/user/revoke/all',
41
+ GET_USER_BEHAVIOR_LOG = '/v1/user/security/history',
40
42
 
41
- VERIFICATION_URL = '/auth/v1/verification',
42
- VERIFY_URL = '/auth/v1/verification/verify',
43
+ VERIFICATION_URL = '/v1/verification',
44
+ VERIFY_URL = '/v1/verification/verify',
43
45
 
44
- CAPTCHA_DATA_URL = '/auth/v1/captcha/data',
45
- VERIFY_CAPTCHA_DATA_URL = '/auth/v1/captcha/data/verify',
46
- GET_CAPTCHA_URL = '/auth/v1/captcha/init',
46
+ CAPTCHA_DATA_URL = '/v1/captcha/data',
47
+ VERIFY_CAPTCHA_DATA_URL = '/v1/captcha/data/verify',
48
+ GET_CAPTCHA_URL = '/v1/captcha/init',
47
49
 
48
- GET_MINIPROGRAM_QRCODE = '/auth/v1/qrcode/generate',
49
- GET_MINIPROGRAM_QRCODE_STATUS = '/auth/v1/qrcode/get/status',
50
+ GET_MINIPROGRAM_QRCODE = '/v1/qrcode/generate',
51
+ GET_MINIPROGRAM_QRCODE_STATUS = '/v1/qrcode/get/status',
50
52
  }
51
53
 
52
54
  export enum ApiUrlsV2 {
53
- AUTH_SIGN_IN_URL = '/auth/v2/signin/username',
54
- AUTH_TOKEN_URL = '/auth/v2/token',
55
- USER_ME_URL = '/auth/v2/user/me',
56
- VERIFY_URL = '/auth/v2/signin/verificationcode',
57
- AUTH_SIGN_IN_WITH_PROVIDER_URL = '/auth/v2/signin/with/provider',
58
- AUTH_PUBLIC_KEY = '/auth/v2/signin/publichkey',
59
- AUTH_RESET_PASSWORD = '/auth/v2/signin/password/update'
55
+ AUTH_SIGN_IN_URL = '/v2/signin/username',
56
+ AUTH_TOKEN_URL = '/v2/token',
57
+ USER_ME_URL = '/v2/user/me',
58
+ VERIFY_URL = '/v2/signin/verificationcode',
59
+ AUTH_SIGN_IN_WITH_PROVIDER_URL = '/v2/signin/with/provider',
60
+ AUTH_PUBLIC_KEY = '/v2/signin/publichkey',
61
+ AUTH_RESET_PASSWORD = '/v2/signin/password/update'
60
62
  }
61
63
 
62
64
  export enum VerificationUsages {
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { OAuth2Client } from './oauth2client/oauth2client'
2
2
  import { AuthOptions, Auth } from './auth/apis'
3
+ import { AUTH_API_PREFIX } from './auth/consts'
3
4
  export { Auth } from './auth/apis'
5
+ export { AUTH_API_PREFIX } from './auth/consts'
4
6
  export * as authModels from './auth/models'
5
7
  export type { ProviderProfile } from './auth/models'
6
8
  export type { Credentials, OAuth2ClientOptions, ResponseError, AuthClientRequestOptions } from './oauth2client/models'
@@ -11,9 +13,10 @@ export class CloudbaseOAuth {
11
13
  public authApi: Auth
12
14
 
13
15
  constructor(authOptions: AuthOptions) {
14
- const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter, onCredentialsError } = authOptions
16
+ const { apiOrigin, apiPath = AUTH_API_PREFIX, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter, onCredentialsError, headers, i18n } = authOptions
15
17
  this.oauth2client = new OAuth2Client({
16
18
  apiOrigin,
19
+ apiPath,
17
20
  clientId,
18
21
  env,
19
22
  storage,
@@ -21,6 +24,8 @@ export class CloudbaseOAuth {
21
24
  anonymousSignInFunc,
22
25
  wxCloud,
23
26
  onCredentialsError,
27
+ headers: headers || {},
28
+ i18n,
24
29
  })
25
30
 
26
31
  this.authApi = new Auth({
@@ -1,6 +1,7 @@
1
1
  import { SimpleStorage } from './interface'
2
2
  import { ErrorType } from './consts'
3
3
  import { AuthOptions } from '../auth/apis'
4
+ import { ICloudbaseConfig } from '@cloudbase/types'
4
5
 
5
6
  /** Credentials **/
6
7
  export interface Credentials {
@@ -52,6 +53,7 @@ export interface AuthClientRequestOptions extends RequestOptions {
52
53
  export interface OAuth2ClientOptions {
53
54
  devMode?: boolean;
54
55
  apiOrigin: string;
56
+ apiPath?: string;
55
57
  clientId: string;
56
58
  env: string;
57
59
  // default value is 1,min value is 0, max value is 5
@@ -68,4 +70,5 @@ export interface OAuth2ClientOptions {
68
70
  anonymousSignInFunc?: (Credentials) => Promise<Credentials | void>
69
71
  wxCloud?: any;
70
72
  onCredentialsError?: AuthOptions['onCredentialsError']
73
+ i18n?: ICloudbaseConfig['i18n']
71
74
  }
@@ -1,5 +1,5 @@
1
1
  import { ErrorType } from './consts'
2
- import { ApiUrls, ApiUrlsV2 } from '../auth/consts'
2
+ import { ApiUrls, ApiUrlsV2, AUTH_API_PREFIX } from '../auth/consts'
3
3
 
4
4
  import { AuthClient, SimpleStorage } from './interface'
5
5
 
@@ -19,6 +19,8 @@ import { SinglePromise } from '../utils/function/single-promise'
19
19
  import { weBtoa } from '../utils/base64'
20
20
  import { isMatch } from '../utils/cloudbase-adapter-wx_mp'
21
21
  import { AuthOptions } from '../auth/apis'
22
+ import { ICloudbaseConfig } from '@cloudbase/types'
23
+ import { langEvent } from '@cloudbase/utilities'
22
24
 
23
25
  const RequestIdHeaderName = 'x-request-id'
24
26
  const DeviceIdHeaderName = 'x-device-id'
@@ -293,7 +295,9 @@ export class OAuth2Client implements AuthClient {
293
295
  private static retryInterval = 1000
294
296
 
295
297
  private apiOrigin: string
298
+ private apiPath: string
296
299
  private clientId: string
300
+ private i18n: ICloudbaseConfig['i18n']
297
301
  private retry: number
298
302
  private clientSecret?: string
299
303
  private baseRequest: <T>(url: string, options?: RequestOptions) => Promise<T>
@@ -323,7 +327,9 @@ export class OAuth2Client implements AuthClient {
323
327
  }
324
328
 
325
329
  this.apiOrigin = options.apiOrigin
330
+ this.apiPath = options.apiPath || AUTH_API_PREFIX
326
331
  this.clientId = options.clientId
332
+ this.i18n = options.i18n
327
333
  this.singlePromise = new SinglePromise({ clientId: this.clientId })
328
334
  this.retry = this.formatRetry(options.retry, OAuth2Client.defaultRetry)
329
335
  if (options.baseRequest) {
@@ -357,6 +363,10 @@ export class OAuth2Client implements AuthClient {
357
363
  this.refreshTokenFunc = options.refreshTokenFunc || this.defaultRefreshTokenFunc
358
364
  this.anonymousSignInFunc = options.anonymousSignInFunc
359
365
  this.onCredentialsError = options.onCredentialsError
366
+
367
+ langEvent.bus.on(langEvent.LANG_CHANGE_EVENT, (params) => {
368
+ this.i18n = params.data?.i18n || this.i18n
369
+ })
360
370
  }
361
371
 
362
372
  /**
@@ -390,7 +400,7 @@ export class OAuth2Client implements AuthClient {
390
400
  options = {}
391
401
  }
392
402
  const retry: number = this.formatRetry(options.retry, this.retry)
393
- options.headers = options.headers || {}
403
+ options.headers = { ...options.headers, [this.i18n?.LANG_HEADER_KEY]: this.i18n?.lang }
394
404
  if (this.headers) {
395
405
  options.headers = {
396
406
  ...this.headers,
@@ -426,7 +436,7 @@ export class OAuth2Client implements AuthClient {
426
436
  }
427
437
  }
428
438
  if (url.startsWith('/')) {
429
- url = this.apiOrigin + url
439
+ url = `${this.apiOrigin}${this.apiPath}${url}`
430
440
  }
431
441
  let response: T | null = null
432
442
  const maxRequestTimes: number = retry + 1