Skip to content

Commit

Permalink
Merge pull request #63 from achilleasa/support-registration-of-bad-ce…
Browse files Browse the repository at this point in the history
…rt-handler

Support registration of bad cert handler
  • Loading branch information
achilleasa committed Sep 30, 2021
2 parents 6615054 + 3c7ec24 commit e869edf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/client/connection_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ConnectionSettings {

// TLS settings (if TLS connection is required)
SecurityContext? tlsContext;
bool Function(X509Certificate)? onBadCertificate;

ConnectionSettings({
this.host = "127.0.0.1",
Expand All @@ -40,5 +41,6 @@ class ConnectionSettings {
this.reconnectWaitTime = const Duration(milliseconds: 1500),
TuningSettings? tuningSettings,
this.tlsContext,
this.onBadCertificate,
}) : tuningSettings = tuningSettings ?? TuningSettings();
}
1 change: 1 addition & 0 deletions lib/src/client/impl/client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class _ClientImpl implements Client {
settings.host,
settings.port,
context: settings.tlsContext,
onBadCertificate: settings.onBadCertificate,
);
} else {
connectionLogger.info(
Expand Down
19 changes: 19 additions & 0 deletions test/lib/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,24 @@ main({bool enableLogger = true}) {
client = Client(settings: settings);
await client.connect();
});

test("bad certificate handler", () async {
Completer testCompleter = Completer();

SecurityContext ctx = SecurityContext(withTrustedRoots: true);
ConnectionSettings settings = ConnectionSettings(
port: 5671,
tlsContext: ctx,
onBadCertificate: (X509Certificate cert) {
print(
" [x] onBadCertificate: allowing TLS connection to be established even though we cannot verify the certificate");
testCompleter.complete();
return true; // allow connection to proceed
});
client = Client(settings: settings);
await client.connect();

return testCompleter.future;
});
}, skip: skipTLSTests);
}

0 comments on commit e869edf

Please sign in to comment.