Skip to content

Commit d36568a

Browse files
authored
CSHARP-5253: Make IAuthenticator internal (#1431)
1 parent 1e97f44 commit d36568a

16 files changed

+102
-180
lines changed

src/MongoDB.Driver/Authentication/AuthenticationHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.

src/MongoDB.Driver/Authentication/AuthenticatorFactory.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -18,23 +18,15 @@
1818

1919
namespace MongoDB.Driver.Authentication
2020
{
21-
/// <summary>
22-
/// Represents an authenticator factory.
23-
/// </summary>
24-
public class AuthenticatorFactory : IAuthenticatorFactory
21+
internal sealed class AuthenticatorFactory : IAuthenticatorFactory
2522
{
2623
private readonly Func<IAuthenticator> _authenticatorFactoryFunc;
2724

28-
/// <summary>
29-
/// Create an authenticatorFactory.
30-
/// </summary>
31-
/// <param name="authenticatorFactoryFunc">The authenticatorFactoryFunc.</param>
3225
public AuthenticatorFactory(Func<IAuthenticator> authenticatorFactoryFunc)
3326
{
3427
_authenticatorFactoryFunc = Ensure.IsNotNull(authenticatorFactoryFunc, nameof(authenticatorFactoryFunc));
3528
}
3629

37-
/// <inheritdoc/>
3830
public IAuthenticator Create()
3931
{
4032
return _authenticatorFactoryFunc();
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -20,39 +20,14 @@
2020

2121
namespace MongoDB.Driver.Authentication
2222
{
23-
/// <summary>
24-
/// Represents a connection authenticator.
25-
/// </summary>
26-
public interface IAuthenticator
23+
internal interface IAuthenticator
2724
{
28-
/// <summary>
29-
/// Gets the name of the authenticator.
30-
/// </summary>
3125
string Name { get; }
3226

33-
/// <summary>
34-
/// Authenticates the connection.
35-
/// </summary>
36-
/// <param name="connection">The connection.</param>
37-
/// <param name="description">The connection description.</param>
38-
/// <param name="cancellationToken">The cancellation token.</param>
3927
void Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken);
4028

41-
/// <summary>
42-
/// Authenticates the connection.
43-
/// </summary>
44-
/// <param name="connection">The connection.</param>
45-
/// <param name="description">The connection description.</param>
46-
/// <param name="cancellationToken">The cancellation token.</param>
47-
/// <returns>A Task.</returns>
4829
Task AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken);
4930

50-
/// <summary>
51-
/// Optionally customizes hello or legacy hello command.
52-
/// </summary>
53-
/// <param name="helloCommand">Initial command.</param>
54-
/// <param name="cancellationToken">The cancellation token.</param>
55-
/// <returns>Optionally mutated command.</returns>
5631
BsonDocument CustomizeInitialHelloCommand(BsonDocument helloCommand, CancellationToken cancellationToken);
5732
}
5833
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -15,15 +15,8 @@
1515

1616
namespace MongoDB.Driver.Authentication
1717
{
18-
/// <summary>
19-
/// Represents an authenticator factory.
20-
/// </summary>
21-
public interface IAuthenticatorFactory
18+
internal interface IAuthenticatorFactory
2219
{
23-
/// <summary>
24-
/// Create an authenticator.
25-
/// </summary>
26-
/// <returns>The authenticator.</returns>
2720
IAuthenticator Create();
2821
}
2922
}

src/MongoDB.Driver/Authentication/MongoDBX509Authenticator.cs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -24,58 +24,27 @@
2424

2525
namespace MongoDB.Driver.Authentication
2626
{
27-
/// <summary>
28-
/// A MongoDB-X509 authenticator.
29-
/// </summary>
3027
internal sealed class MongoDBX509Authenticator : IAuthenticator
3128
{
32-
// static properties
33-
/// <summary>
34-
/// Gets the name of the mechanism.
35-
/// </summary>
36-
/// <value>
37-
/// The name of the mechanism.
38-
/// </value>
3929
public static string MechanismName
4030
{
4131
get { return "MONGODB-X509"; }
4232
}
4333

44-
// fields
4534
private readonly string _username;
4635
private readonly ServerApi _serverApi;
4736

48-
// constructors
49-
/// <summary>
50-
/// Initializes a new instance of the <see cref="MongoDBX509Authenticator"/> class.
51-
/// </summary>
52-
/// <param name="username">The username.</param>
53-
[Obsolete("Use the newest overload instead.")]
54-
public MongoDBX509Authenticator(string username)
55-
: this(username, serverApi: null)
56-
{
57-
}
58-
59-
/// <summary>
60-
/// Initializes a new instance of the <see cref="MongoDBX509Authenticator"/> class.
61-
/// </summary>
62-
/// <param name="username">The username.</param>
63-
/// <param name="serverApi">The server API.</param>
6437
public MongoDBX509Authenticator(string username, ServerApi serverApi)
6538
{
6639
_username = Ensure.IsNullOrNotEmpty(username, nameof(username));
6740
_serverApi = serverApi; // can be null
6841
}
6942

70-
// properties
71-
/// <inheritdoc/>
7243
public string Name
7344
{
7445
get { return MechanismName; }
7546
}
7647

77-
// public methods
78-
/// <inheritdoc/>
7948
public void Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
8049
{
8150
Ensure.IsNotNull(connection, nameof(connection));
@@ -97,7 +66,6 @@ public void Authenticate(IConnection connection, ConnectionDescription descripti
9766
}
9867
}
9968

100-
/// <inheritdoc/>
10169
public async Task AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
10270
{
10371
Ensure.IsNotNull(connection, nameof(connection));
@@ -119,15 +87,12 @@ public async Task AuthenticateAsync(IConnection connection, ConnectionDescriptio
11987
}
12088
}
12189

122-
/// <inheritdoc/>
12390
public BsonDocument CustomizeInitialHelloCommand(BsonDocument helloCommand, CancellationToken cancellationToken)
12491
{
12592
helloCommand.Add("speculativeAuthenticate", CreateAuthenticateCommand());
12693
return helloCommand;
12794
}
12895

129-
// private methods
130-
13196
private BsonDocument CreateAuthenticateCommand()
13297
{
13398
return new BsonDocument
@@ -155,7 +120,7 @@ private CommandWireProtocol<BsonDocument> CreateAuthenticateProtocol()
155120

156121
private MongoAuthenticationException CreateException(IConnection connection, Exception ex)
157122
{
158-
var message = string.Format("Unable to authenticate username '{0}' using protocol '{1}'.", _username, Name);
123+
var message = $"Unable to authenticate username '{_username}' using protocol '{Name}'.";
159124
return new MongoAuthenticationException(connection.ConnectionId, message, ex);
160125
}
161126
}

src/MongoDB.Driver/Authentication/UsernamePasswordCredential.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919

2020
namespace MongoDB.Driver.Authentication
2121
{
22-
/// <summary>
23-
/// Represents a username/password credential.
24-
/// </summary>
25-
public sealed class UsernamePasswordCredential
22+
internal sealed class UsernamePasswordCredential
2623
{
27-
// fields
2824
private readonly Lazy<SecureString> _saslPreppedPassword;
2925
private string _source;
3026
private SecureString _password;
@@ -41,7 +37,7 @@ public UsernamePasswordCredential(string source, string username, string passwor
4137
: this(source, username, SecureStringHelper.ToSecureString(password))
4238
{
4339
// Compute saslPreppedPassword immediately and store it securely while the password is already in
44-
// managed memory. We don't create a closure over the password so that it will hopefully get
40+
// managed memory. We don't create a closure over the password so that it will hopefully get
4541
// garbage-collected sooner rather than later.
4642
var saslPreppedPassword = SecureStringHelper.ToSecureString(SaslPrepHelper.SaslPrepStored(password));
4743
_saslPreppedPassword = new Lazy<SecureString>(() => saslPreppedPassword);
@@ -68,56 +64,26 @@ public UsernamePasswordCredential(string source, string username, SecureString p
6864
() => SecureStringHelper.ToSecureString(SaslPrepHelper.SaslPrepStored(GetInsecurePassword())));
6965
}
7066

71-
// properties
72-
/// <summary>
73-
/// Gets the password.
74-
/// </summary>
75-
/// <value>
76-
/// The password.
77-
/// </value>
7867
public SecureString Password
7968
{
8069
get { return _password; }
8170
}
8271

83-
/// <summary>
84-
/// Gets the the SASLprepped password.
85-
/// May create a cleartext copy of the password in managed memory the first time it is accessed.
86-
/// Use only as needed e.g. for SCRAM-SHA-256.
87-
/// </summary>
88-
/// <returns>The SASLprepped password.</returns>
8972
public SecureString SaslPreppedPassword
9073
{
9174
get { return _saslPreppedPassword.Value; }
9275
}
9376

94-
/// <summary>
95-
/// Gets the source.
96-
/// </summary>
97-
/// <value>
98-
/// The source.
99-
/// </value>
10077
public string Source
10178
{
10279
get { return _source; }
10380
}
10481

105-
/// <summary>
106-
/// Gets the username.
107-
/// </summary>
108-
/// <value>
109-
/// The username.
110-
/// </value>
11182
public string Username
11283
{
11384
get { return _username; }
11485
}
11586

116-
// methods
117-
/// <summary>
118-
/// Gets the password (converts the password from a SecureString to a regular string).
119-
/// </summary>
120-
/// <returns>The password.</returns>
12187
public string GetInsecurePassword()
12288
{
12389
return SecureStringHelper.ToInsecureString(_password);

src/MongoDB.Driver/ClusterRegistry.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System;
1716
using System.Collections.Generic;
1817
using System.Linq;
1918
using System.Net;
@@ -119,18 +118,15 @@ private ConnectionPoolSettings ConfigureConnectionPool(ConnectionPoolSettings se
119118
private ConnectionSettings ConfigureConnection(ConnectionSettings settings, ClusterKey clusterKey)
120119
{
121120
var endPoints = clusterKey.Servers.Select(s => new DnsEndPoint(s.Host, s.Port)).ToArray();
122-
var authenticatorFactories = Array.Empty<IAuthenticatorFactory>();
121+
IAuthenticatorFactory authenticatorFactory = null;
123122

124123
if (clusterKey.Credential != null)
125124
{
126-
authenticatorFactories = new[]
127-
{
128-
new AuthenticatorFactory(() => clusterKey.Credential.ToAuthenticator(endPoints, clusterKey.ServerApi))
129-
};
125+
authenticatorFactory = new AuthenticatorFactory(() => clusterKey.Credential.ToAuthenticator(endPoints, clusterKey.ServerApi));
130126
}
131127

132128
return settings.With(
133-
authenticatorFactories: Optional.Enumerable<IAuthenticatorFactory>(authenticatorFactories),
129+
authenticatorFactory: Optional.Create(authenticatorFactory),
134130
compressors: Optional.Enumerable(clusterKey.Compressors),
135131
libraryInfo: clusterKey.LibraryInfo,
136132
loadBalanced: clusterKey.LoadBalanced,

src/MongoDB.Driver/Core/Configuration/ClusterBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private ServerFactory CreateServerFactory()
262262
private IServerMonitorFactory CreateServerMonitorFactory()
263263
{
264264
var serverMonitorConnectionSettings = _connectionSettings
265-
.With(authenticatorFactories: new IAuthenticatorFactory[] { });
265+
.With(authenticatorFactory: null);
266266

267267
var heartbeatConnectTimeout = _tcpStreamSettings.ConnectTimeout;
268268
if (heartbeatConnectTimeout == TimeSpan.Zero || heartbeatConnectTimeout == Timeout.InfiniteTimeSpan)

src/MongoDB.Driver/Core/Configuration/ClusterBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static ClusterBuilder ConfigureWithConnectionString(
162162
if (connectionString.Username != null)
163163
{
164164
var authenticatorFactory = new AuthenticatorFactory(() => CreateAuthenticator(connectionString, serverApi));
165-
builder = builder.ConfigureConnection(s => s.With(authenticatorFactories: new[] { authenticatorFactory }));
165+
builder = builder.ConfigureConnection(s => s.With(authenticatorFactory: authenticatorFactory));
166166
}
167167
if (connectionString.ApplicationName != null)
168168
{

0 commit comments

Comments
 (0)