@cloudbase/auth 3.0.3 → 3.0.4

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.
package/src/adapter.ts CHANGED
@@ -180,6 +180,7 @@ const dealUserAdapter = (config: AuthOptions, adapter: SDKAdapterInterface) => {
180
180
  const reqClassParams = {
181
181
  timeout: 10000,
182
182
  restrictedMethods: ['get', 'post', 'upload', 'download', 'request'],
183
+ auth: config.auth,
183
184
  }
184
185
  const adapterReq = new adapter.reqClass(reqClassParams)
185
186
  data.baseRequest = async function (url: string, options?: { method?: string; [key: string]: any }) {
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  weAppJwtDecodeAll,
15
15
  AuthError,
16
16
  authModels,
17
+ DEFAULT_NODE_ACCESS_SCOPE,
17
18
  } from '@cloudbase/oauth'
18
19
  import { useAuthAdapter } from './adapter'
19
20
  import {
@@ -362,18 +363,20 @@ export class LoginState implements ILoginState {
362
363
  }
363
364
  }
364
365
 
366
+ interface IAuthConfig extends ICloudbaseAuthConfig {
367
+ cache: ICloudbaseCache
368
+ request?: ICloudbaseRequest
369
+ runtime?: string
370
+ }
371
+
365
372
  class Auth {
366
- readonly config: ICloudbaseAuthConfig
373
+ readonly config: IAuthConfig
367
374
  oauthInstance: CloudbaseOAuth
368
375
  readonly cache: ICloudbaseCache
369
376
  private listeners: Map<string, Set<OnAuthStateChangeCallback>> = new Map()
370
377
  private hasListenerSetUp = false
371
378
 
372
- constructor(config: ICloudbaseAuthConfig & {
373
- cache: ICloudbaseCache
374
- request?: ICloudbaseRequest
375
- runtime?: string
376
- },) {
379
+ constructor(config: IAuthConfig) {
377
380
  this.config = config
378
381
  this.oauthInstance = config.oauthInstance
379
382
  this.cache = config.cache
@@ -1026,6 +1029,10 @@ class Auth {
1026
1029
  return null
1027
1030
  }
1028
1031
 
1032
+ /**
1033
+ * @deprecated 使用 getCurrentUser 代替,因为与node-sdk方法名冲突
1034
+ * @returns
1035
+ */
1029
1036
  @catchErrorsDecorator({
1030
1037
  title: '获取用户信息失败',
1031
1038
  messages: [
@@ -1344,18 +1351,30 @@ class Auth {
1344
1351
  }
1345
1352
 
1346
1353
  async setAccessKey() {
1354
+ let accessToken = ''
1355
+ let scope = ''
1347
1356
  if (this.config.accessKey) {
1348
- try {
1349
- this.oauthInstance.oauth2client.setAccessKeyCredentials({
1350
- access_token: this.config.accessKey,
1351
- token_type: 'Bearer',
1352
- scope: 'accessKey',
1353
- expires_at: new Date(+new Date() + +new Date()),
1354
- expires_in: +new Date() + +new Date(),
1355
- })
1356
- } catch (error) {
1357
- console.warn('accessKey error: ', error)
1358
- }
1357
+ accessToken = this.config.accessKey
1358
+ scope = 'accessKey'
1359
+ } else if (this.config.auth?.secretId && this.config.auth?.secretKey) {
1360
+ accessToken = ''
1361
+ scope = DEFAULT_NODE_ACCESS_SCOPE
1362
+ }
1363
+
1364
+ if (!scope) {
1365
+ return
1366
+ }
1367
+
1368
+ try {
1369
+ this.oauthInstance.oauth2client.setAccessKeyCredentials({
1370
+ access_token: accessToken,
1371
+ token_type: 'Bearer',
1372
+ scope,
1373
+ expires_at: new Date(+new Date() + +new Date()),
1374
+ expires_in: +new Date() + +new Date(),
1375
+ })
1376
+ } catch (error) {
1377
+ console.warn('accessKey error: ', error)
1359
1378
  }
1360
1379
  }
1361
1380
 
@@ -1924,7 +1943,7 @@ class Auth {
1924
1943
  */
1925
1944
  async getUser(): Promise<GetUserRes> {
1926
1945
  try {
1927
- const user = this.convertToUser(await this.getUserInfo())
1946
+ const user = this.convertToUser(await this.getCurrentUser())
1928
1947
  return { data: { user }, error: null }
1929
1948
  } catch (error) {
1930
1949
  return { data: {}, error: new AuthError(error) }
@@ -2057,7 +2076,12 @@ class Auth {
2057
2076
  try {
2058
2077
  const providers = await this.oauthInstance.authApi.getProviders()
2059
2078
 
2060
- return { data: { identities: providers?.data?.filter(v => !!v.bind) as unknown as GetUserIdentitiesRes['data']['identities'] }, error: null }
2079
+ return {
2080
+ data: {
2081
+ identities: providers?.data?.filter(v => !!v.bind) as unknown as GetUserIdentitiesRes['data']['identities'],
2082
+ },
2083
+ error: null,
2084
+ }
2061
2085
  } catch (error) {
2062
2086
  return { data: {}, error: new AuthError(error) }
2063
2087
  }
@@ -2569,7 +2593,10 @@ class Auth {
2569
2593
  }
2570
2594
  }
2571
2595
 
2572
- type TInitAuthOptions = Pick<ICloudbaseAuthConfig, 'region' | 'persistence' | 'i18n' | 'accessKey' | 'useWxCloud'> &
2596
+ type TInitAuthOptions = Pick<
2597
+ ICloudbaseAuthConfig,
2598
+ 'region' | 'persistence' | 'i18n' | 'accessKey' | 'useWxCloud' | 'auth'
2599
+ > &
2573
2600
  Partial<AuthOptions> & {
2574
2601
  detectSessionInUrl?: boolean
2575
2602
  }
@@ -2622,6 +2649,7 @@ export function generateAuthInstance(
2622
2649
  headers: { 'X-SDK-Version': `@cloudbase/js-sdk/${config.sdkVersion}`, ...(config.headers || {}) },
2623
2650
  detectSessionInUrl: config.detectSessionInUrl,
2624
2651
  debug,
2652
+ auth: options.app?.config?.auth,
2625
2653
  }),)
2626
2654
 
2627
2655
  const authInstance = new Auth({
@@ -2638,6 +2666,7 @@ export function generateAuthInstance(
2638
2666
  runtime: runtime || 'web',
2639
2667
  _fromApp: cloudbase,
2640
2668
  oauthInstance,
2669
+ auth: options.app?.config?.auth,
2641
2670
  })
2642
2671
 
2643
2672
  // Initialize session with user info callback