refreshToken method
Performs a refresh_token request using the refreshToken.
Implementation
Future<AccessTokenResponse> refreshToken(AccessTokenResponse curTknResp,
{http.Client? httpClient}) async {
httpClient ??= this.httpClient;
AccessTokenResponse? tknResp;
var 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!;
}