refreshToken method

Future<AccessTokenResponse> refreshToken(
  1. AccessTokenResponse curTknResp, {
  2. Client? httpClient,
})

Performs a refresh_token request using the refreshToken. curTknResp must contain a valid refreshToken.

Implementation

Future<AccessTokenResponse> refreshToken(AccessTokenResponse curTknResp,
    {http.Client? httpClient}) async {
  httpClient ??= this.httpClient;

  AccessTokenResponse tknResp;
  final refreshToken = curTknResp.refreshToken!;
  try {
    tknResp = await client.refreshToken(refreshToken,
        clientId: clientId,
        clientSecret: clientSecret,
        scopes: curTknResp.scope,
        httpClient: httpClient);
  } catch (_) {
    return await fetchToken(httpClient: httpClient);
  }

  if (tknResp.isValid()) {
    //If the response doesn't contain a refresh token, keep using the current one
    if (!tknResp.hasRefreshToken()) {
      tknResp.refreshToken = refreshToken;
    }

    await tokenStorage.addToken(tknResp);
  } else {
    // invalid_grant is usually produced in this case.
    // Some major backend providers (such as League/oauth2-server) seem
    // to throw invalid_request errors as well, though...
    if (tknResp.error == 'invalid_grant' ||
        tknResp.error == 'invalid_request') {
      //The refresh token is expired too
      await tokenStorage.deleteToken(scopes ?? []);
      //Fetch another access token
      tknResp = await getToken(httpClient: httpClient);
    } else {
      throw OAuth2Exception(tknResp.error ?? 'Error',
          errorDescription: tknResp.errorDescription);
    }
  }

  return tknResp;
}