fetchToken method

Future<AccessTokenResponse> fetchToken({
  1. Client? httpClient,
})

Fetches a new token and saves it in the storage

Implementation

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

  _validateAuthorizationParams();

  AccessTokenResponse tknResp;

  if (grantType == authorizationCode) {
    tknResp = await client.getTokenWithAuthCodeFlow(
      clientId: clientId,
      clientSecret: clientSecret,
      scopes: scopes,
      enablePKCE: enablePKCE,
      verifierLength: verifierLength,
      enableState: enableState,
      stateLength: stateLength,
      authCodeParams: authCodeParams,
      accessTokenParams: accessTokenParams,
      accessTokenHeaders: accessTokenHeaders,
      afterAuthorizationCodeCb: afterAuthorizationCodeCb,
      webAuthClient: webAuthClient,
      webAuthOpts: webAuthOpts,
      httpClient: httpClient,
    );
  } 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,
      httpClient: httpClient,
    );
  } else if (grantType == implicitGrant) {
    tknResp = await client.getTokenWithImplicitGrantFlow(
      clientId: clientId,
      scopes: scopes,
      enableState: enableState,
      stateLength: stateLength,
      webAuthClient: webAuthClient,
      webAuthOpts: webAuthOpts,
      customParams: authCodeParams,
      httpClient: httpClient,
    );
  } else {
    tknResp = AccessTokenResponse.errorResponse(
      error: 'Invalid grantType',
      errorDescription: '$grantType',
    );
  }

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

  return tknResp;
}