secure_pinning_dio 0.0.2
secure_pinning_dio: ^0.0.2 copied to clipboard
Dio integration for secure_pinning — an Interceptor-shaped facade for pinned TLS validation on Dio traffic.
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:secure_pinning/secure_pinning.dart' show SecurePinningException;
import 'package:secure_pinning_dio/secure_pinning_dio.dart';
Future<void> main() async {
final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'));
dio.interceptors.add(
SecurePinningInterceptor.forHost(
dio,
'api.example.com',
// Replace with your own host's real SPKI pins — compute them with:
// openssl s_client -connect HOST:443 -servername HOST < /dev/null 2>/dev/null \
// | openssl x509 -pubkey -noout \
// | openssl pkey -pubin -outform der \
// | openssl dgst -sha256 -hex \
// | awk '{print $NF}'
// Always include a backup pin so a future key rotation doesn't
// brick the app.
pins: ['hex-spki-hash-1', 'hex-spki-hash-2'],
),
);
try {
final response = await dio.get<String>('/');
stdout.writeln(
'HTTP ${response.statusCode} — ${(response.data ?? '').length} bytes',
);
} on DioException catch (error) {
final pinningError = error.error;
if (pinningError is SecurePinningException) {
stdout.writeln('Pinning failed: $pinningError');
} else {
rethrow;
}
} finally {
dio.close();
}
}