onRequest method

  1. @override
void onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)

Called when the request is about to be sent.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
  SafePrint("Checking SHA 256 Finger Prints...");
  try {
    if (allowedSHAFingerprints.isEmpty) {
      SafePrint("⚠ No SHA Finger Prints provided, allowing connection...");
      return handler.next(options);
    }

    final isValidCertificate = await _validateCertificate(options.uri.host);

    if (isValidCertificate) {
      SafePrint("✅ Allowed SHA Finger Prints");
      return handler.next(options);
    } else {
      SafePrint("⚠ Error SHA Finger Prints is invalid!");
    }
  } catch (e) {
    SafePrint(e);
    throw DioException(
      error: '⚠ Error validating certificate \n$e',
      requestOptions: options,
      response: Response(
        requestOptions: options,
        statusCode: 400,
      ),
    );
  }
}