Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRefreshToken() #11

Open
psyntium opened this issue Aug 14, 2020 · 3 comments
Open

Add getRefreshToken() #11

psyntium opened this issue Aug 14, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@psyntium
Copy link

psyntium commented Aug 14, 2020

For containers API, they require the refresh_token to be sent at the header. For some of its API.

Refer:
https://containers.cloud.ibm.com/global/swagger-global-api/#/clusters/GetClusterConfig

@csantanapr
Copy link
Contributor

Thanks @psyntium for reporting the issue 👍

Wow, that's crazy, why do they want 2 tokens, sending the refresh token is only useful to get a new token if the primary token expired by the time they actually going to use it.

I welcome a PR with a backward-compatible change.
Maybe adding a plural version of the API functions getTokens and getHeathers that returns both tokens. and use header name X-Auth-Refresh-Token and default but allow override if there is another service that requires the refresh token with a different name,

@csantanapr csantanapr added the enhancement New feature or request label Oct 1, 2020
@csantanapr
Copy link
Contributor

Adding the function getRefreshToken also is useful I just notice now on the title of the issue

@psyntium
Copy link
Author

psyntium commented Apr 6, 2021

   * This function sends a refresh token back through a Promise. The source of the token
   * is determined by the following logic:
   * 1. If the token is expired (that is, we already have one, but it is no longer valid, or about to time out), we
   *    load a new one
   * 2. If the token is not expired, we obviously have a valid token, so just resolve with it's value
   * 3. If we haven't got a token at all, but a loading is already in process, we wait for the loading promise to settle
   *    and depending on the result
   *    3a) use the newly returned and cached token
   *    3b) in case of error, trigger a fresh loading attempt
   * 4. If there is no token available and also no loading in progress, trigger the token loading
   *
   * @returns {Promise} - resolved with token value
   */
   getRefreshToken () {
    return new Promise((resolve, reject) => {
      const loadToken = () => {
        this.loadToken()
          .then(() => {
            resolve(this.tokenInfo.refresh_token)
          })
          .catch(error => reject(error))
      }

      if (this.isTokenExpired()) {
        // 1. load a new token
        loadToken()
      } else if (this.tokenInfo.refresh_token) {
        // 2. return the cached valid token
        resolve(this.tokenInfo.refresh_token)
      } else if (this.tokenLoadingPromise) {
        // 3. a token loading operation is already running
        this.tokenLoadingPromise
          .then(() => {
            // 3a) it was successful, so return the fresh token
            resolve(this.tokenInfo.refresh_token)
          })
          .catch(() => {
            // 3b) give it one more try - obviously, we hoped for a Promise triggered by another invocation to
            // return the token for us, but it didn't work out. So we need to trigger another attempt.
            loadToken()
          })
      } else {
        // 4. just trigger the token loading
        loadToken()
      }
    })
  }```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants