fetchToken method

Future<AccessTokenResponse> fetchToken()

Fetches a new token and saves it in the storage

Implementation

Future<AccessTokenResponse> fetchToken() async {
  _validateAuthorizationParams();

  AccessTokenResponse tknResp;

  if (grantType == authorizationCode) {
    tknResp = await client.getTokenWithAuthCodeFlow(
        clientId: clientId,
        clientSecret: clientSecret,
        scopes: scopes,
        enablePKCE: enablePKCE,
        enableState: enableState,
        authCodeParams: authCodeParams,
        accessTokenParams: accessTokenParams,
        accessTokenHeaders: accessTokenHeaders,
        afterAuthorizationCodeCb: afterAuthorizationCodeCb,
        webAuthClient: webAuthClient,
        webAuthOpts: webAuthOpts);
  } else if (grantType == clientCredentials) {
    tknResp = await client.getTokenWithClientCredentialsFlow(
        clientId: clientId,
        //The clientSecret param can't be null at this point... It has been validated by the above _validateAuthorizationParams call...
        clientSecret: clientSecret!,
        customHeaders: accessTokenHeaders,
        scopes: scopes);
  } else if (grantType == implicitGrant) {
    tknResp = await client.getTokenWithImplicitGrantFlow(
        clientId: clientId,
        scopes: scopes,
        enableState: enableState,
        webAuthClient: webAuthClient,
        webAuthOpts: webAuthOpts,
        customParams: authCodeParams);
  } else {
    tknResp = AccessTokenResponse.errorResponse();
  }

  if (tknResp.isValid()) {
    await tokenStorage.addToken(tknResp);
  }

  return tknResp;
}