revoke method

Future<void> revoke()

Revokes any outstanding tokens associated with the Authenticator.

Implementation

Future<void> revoke() async {
  final tokens = <Map>[];
  final accessToken = {
    _kTokenKey: credentials.accessToken,
    _kTokenTypeHintKey: 'access_token',
  };
  tokens.add(accessToken);

  if (credentials.refreshToken != null) {
    final refreshToken = {
      _kTokenKey: credentials.refreshToken,
      _kTokenTypeHintKey: 'refresh_token',
    };
    tokens.add(refreshToken);
  }
  for (final token in tokens) {
    final Map<String, String?> revokeAccess = <String, String>{};
    revokeAccess[_kTokenKey] = token[_kTokenKey];
    revokeAccess[_kTokenTypeHintKey] = token[_kTokenTypeHintKey];

    var path = Uri.parse(_config.revokeToken!);

    // Retrieve the client ID and secret.
    final clientId = _config.clientId;
    final clientSecret = _config.clientSecret;

    if ((clientId != null) && (clientSecret != null)) {
      final userInfo = '$clientId:$clientSecret';
      path = path.replace(userInfo: userInfo);
    }
    final Map<String, String?> headers = <String, String>{};
    headers[_kUserAgentKey] = _config.userAgent;

    final httpClient = http.Client();

    // Request the token from the server.
    final response = await httpClient.post(path,
        headers: headers as Map<String, String>?, body: revokeAccess);

    if (response.statusCode != 204) {
      // We should always get a 204 response for this call.
      final parsed = json.decode(response.body);
      _throwAuthenticationError(parsed);
    }
  }
}