@cloudbase/oauth 2.17.17 → 2.18.1

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.
@@ -502,15 +502,20 @@ export class OAuth2Client implements AuthClient {
502
502
  return this.unAuthenticatedError('credentials not found')
503
503
  }
504
504
  if (isCredentialsExpired(credentials)) {
505
- if (credentials && credentials.scope === 'anonymous') {
506
- if (this.anonymousSignInFunc) {
507
- const c = await this.anonymousSignInFunc(credentials)
508
- credentials = c || (await this.localCredentials.getCredentials())
509
- } else {
510
- credentials = await this.anonymousSignIn(credentials)
505
+ if (credentials.refresh_token) {
506
+ try {
507
+ credentials = await this.refreshToken(credentials)
508
+ } catch (error) {
509
+ if (credentials.scope === 'anonymous') {
510
+ credentials = await this.anonymousLogin(credentials)
511
+ } else {
512
+ return Promise.reject(error)
513
+ }
511
514
  }
515
+ } else if (credentials.scope === 'anonymous') {
516
+ credentials = await this.anonymousLogin(credentials)
512
517
  } else {
513
- credentials = await this.refreshToken(credentials)
518
+ return this.unAuthenticatedError('no refresh token found in credentials')
514
519
  }
515
520
  }
516
521
  return credentials
@@ -568,6 +573,19 @@ export class OAuth2Client implements AuthClient {
568
573
  })
569
574
  }
570
575
 
576
+ private async anonymousLogin(credentials: Credentials) {
577
+ return this.singlePromise.run('_anonymousLogin', async () => {
578
+ if (this.anonymousSignInFunc) {
579
+ const c = await this.anonymousSignInFunc(credentials)
580
+ credentials = c || (await this.localCredentials.getCredentials())
581
+ } else {
582
+ credentials = await this.anonymousSignIn(credentials)
583
+ }
584
+
585
+ return credentials
586
+ })
587
+ }
588
+
571
589
  /**
572
590
  * Check retry value.
573
591
  * @param {number} retry