getTokenWithImplicitGrantFlow method

Future<AccessTokenResponse> getTokenWithImplicitGrantFlow({
  1. required String clientId,
  2. List<String>? scopes,
  3. bool enableState = true,
  4. String? state,
  5. dynamic httpClient,
  6. BaseWebAuth? webAuthClient,
  7. Map<String, dynamic>? webAuthOpts,
})

Requests an Access Token to the OAuth2 endpoint using the Implicit grant flow (https://tools.ietf.org/html/rfc6749#page-31)

Implementation

Future<AccessTokenResponse> getTokenWithImplicitGrantFlow(
    {required String clientId,
    List<String>? scopes,
    bool enableState = true,
    String? state,
    httpClient,
    BaseWebAuth? webAuthClient,
    Map<String, dynamic>? webAuthOpts}) async {
  httpClient ??= http.Client();
  webAuthClient ??= this.webAuthClient;

  if (enableState) state ??= randomAlphaNumeric(25);

  final authorizeUrl = getAuthorizeUrl(
      clientId: clientId,
      responseType: 'token',
      scopes: scopes,
      enableState: enableState,
      state: state,
      redirectUri: redirectUri);

  // Present the dialog to the user
  final result = await webAuthClient.authenticate(
      url: authorizeUrl,
      callbackUrlScheme: customUriScheme,
      redirectUrl: redirectUri,
      opts: webAuthOpts);

  final fragment = Uri.splitQueryString(Uri.parse(result).fragment);

  if (enableState) {
    final checkState = fragment['state'];
    if (state != checkState) {
      throw Exception(
          '"state" parameter in response doesn\'t correspond to the expected value');
    }
  }

  return AccessTokenResponse.fromMap({
    'access_token': fragment['access_token'],
    'token_type': fragment['token_type'],
    'scope': fragment['scope'] ?? scopes,
    'expires_in': fragment['expires_in'],
    'http_status_code': 200
  });
}