Skip to content

Commit 573da75

Browse files
author
Giorgio Franceschetti
committed
x509 delayed authentication
1 parent 5266c8e commit 573da75

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/src/database/db.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,15 +730,33 @@ class Db {
730730
.toList();
731731
}
732732

733+
/// Method for authentication with X509 certificate.
734+
/// In the conection parameters you have not to set
735+
/// X509 if you want to use this delayed auth function.
736+
Future<bool> authenticateX509({Connection? connection}) async =>
737+
authenticate(null, null,
738+
connection: connection,
739+
authScheme: AuthenticationScheme.X509,
740+
authDb: r'$external');
741+
733742
Future<bool> authenticate(String? userName, String? password,
734-
{Connection? connection}) async {
743+
{Connection? connection,
744+
AuthenticationScheme? authScheme,
745+
String? authDb}) async {
735746
var credential = UsernamePasswordCredential()
736747
..username = userName
737748
..password = password;
738749

739750
(connection ?? masterConnection).serverConfig.userName ??= userName;
740751
(connection ?? masterConnection).serverConfig.password ??= password;
741752

753+
if (authScheme != null) {
754+
_authenticationScheme = authScheme;
755+
}
756+
if (authDb != null) {
757+
authSourceDb = Db._authDb(authDb);
758+
}
759+
742760
if (_authenticationScheme == null) {
743761
throw MongoDartError('Authentication scheme not specified');
744762
}

test/auth_tests/client_x_509_test.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'dart:io';
55
import 'package:test/test.dart';
66
import 'package:mongo_dart/mongo_dart.dart';
77

8+
import '../utils/throw_utils.dart';
9+
810
// Run server1 with these parameters:
911
// mongod --port 27038 --dbpath <your-data-path> --oplogSize 128
1012
// --tlsMode requireTLS --tlsCertificateKeyFile
@@ -15,6 +17,7 @@ import 'package:mongo_dart/mongo_dart.dart';
1517
const dbName = 'mongodb-auth';
1618
const defaultUri =
1719
'mongodb://127.0.0.1:27038/$dbName?authMechanism=MONGODB-X509';
20+
const lateAuthUri = 'mongodb://127.0.0.1:27038/$dbName';
1821

1922
void main() async {
2023
bool serverfound = false;
@@ -194,13 +197,16 @@ void main() async {
194197
await db.close();
195198
});
196199
test('Connect & Authenticate - Simple find()', () async {
197-
var db = Db(defaultUri);
200+
var db = Db(lateAuthUri);
198201

199202
await db.open(
200203
secure: true,
201204
tlsCAFile: caCertFile.path,
202205
tlsCertificateKeyFile: pemFile.path);
203-
await db.collection('test').find().toList();
206+
var collection = db.collection('test');
207+
expect(() => collection.find().toList(), throwsMongoDartError);
208+
await db.authenticateX509();
209+
await collection.find().toList();
204210
expect(db.masterConnection.isAuthenticated, isTrue);
205211
await db.close();
206212
});

0 commit comments

Comments
 (0)