Skip to content

Commit ba375d8

Browse files
committed
CSHARP-603: update for changes to driver authentication spec.
1 parent 83c2612 commit ba375d8

22 files changed

+156
-155
lines changed

MongoDB.Driver/Communication/Security/Authenticator.cs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ namespace MongoDB.Driver.Communication.Security
2828
internal class Authenticator
2929
{
3030
// private static fields
31-
private static readonly List<IAuthenticationMethod> __clientSupportedMethods = new List<IAuthenticationMethod>
31+
private static readonly List<IAuthenticationProtocol> __clientSupportedProtocols = new List<IAuthenticationProtocol>
3232
{
33-
new SaslAuthenticationMethod(new GssapiMechanism()),
34-
new SaslAuthenticationMethod(new CramMD5Mechanism()),
35-
new SaslAuthenticationMethod(new DigestMD5Mechanism()),
36-
new MongoCRAuthenticationMethod()
33+
// when we start negotiating, MONGO-CR should be moved to the bottom of the list...
34+
new MongoCRAuthenticationProtocol(),
35+
new SaslAuthenticationProtocol(new GssapiMechanism()),
36+
new SaslAuthenticationProtocol(new CramMD5Mechanism()),
37+
new SaslAuthenticationProtocol(new DigestMD5Mechanism())
3738
};
3839

3940
// private fields
@@ -63,50 +64,27 @@ public void Authenticate()
6364
return;
6465
}
6566

66-
var serverSupportedMethods = GetServerSupportedMethods();
6767
foreach (var credential in _credentials)
6868
{
69-
Authenticate(credential, serverSupportedMethods);
69+
Authenticate(credential);
7070
}
7171
}
7272

7373
// private methods
74-
private void Authenticate(MongoCredential credential, List<string> serverSupportedMethods)
74+
private void Authenticate(MongoCredential credential)
7575
{
76-
foreach (var clientSupportedMethod in __clientSupportedMethods)
76+
foreach (var clientSupportedProtocol in __clientSupportedProtocols)
7777
{
78-
if (serverSupportedMethods.Contains(clientSupportedMethod.Name) && clientSupportedMethod.CanUse(credential))
78+
if (clientSupportedProtocol.CanUse(credential))
7979
{
80-
clientSupportedMethod.Authenticate(_connection, credential);
80+
clientSupportedProtocol.Authenticate(_connection, credential);
8181
return;
8282
}
8383
}
8484

85-
var message = string.Format("Unable to negotiate a protocol to authenticate. The credential for source {0}, username {1} over protocol {2} could not be authenticated.", credential.Source, credential.Username, credential.AuthenticationProtocol);
85+
var message = string.Format("Unable to find a protocol to authenticate. The credential for source {0}, username {1} over mechanism {2} could not be authenticated.", credential.Source, credential.Username, credential.Mechanism);
8686
throw new MongoSecurityException(message);
8787
}
88-
89-
private List<string> GetServerSupportedMethods()
90-
{
91-
var command = new CommandDocument
92-
{
93-
{ "saslStart", 1 },
94-
{ "mechanism", ""}, // forces a response that contains a list of supported mechanisms...
95-
{ "payload", new byte[0] }
96-
};
97-
98-
var list = new List<string>();
99-
var result = _connection.RunCommand("admin", QueryFlags.SlaveOk, command, false);
100-
if (result.Response.Contains("supportedMechanisms"))
101-
{
102-
list.AddRange(result.Response["supportedMechanisms"].AsBsonArray.Select(x => x.AsString));
103-
}
104-
105-
// because MONGO-CR is last in the list, we don't need to check if the server supports it...
106-
// in the future, we may need to add a check.
107-
list.Add("MONGO-CR");
108-
return list;
109-
}
11088
}
11189

11290
}

MongoDB.Driver/Communication/Security/IAuthenticationMethod.cs renamed to MongoDB.Driver/Communication/Security/IAuthenticationProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Driver.Communication.Security
2424
/// <summary>
2525
/// Authenticates a MongoConnection.
2626
/// </summary>
27-
internal interface IAuthenticationMethod
27+
internal interface IAuthenticationProtocol
2828
{
2929
/// <summary>
3030
/// Gets the name.

MongoDB.Driver/Communication/Security/Mechanisms/CramMD5Mechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public string Name
4747
/// <exception cref="System.NotImplementedException"></exception>
4848
public bool CanUse(MongoCredential credential)
4949
{
50-
return credential.AuthenticationProtocol == MongoAuthenticationProtocol.Strongest &&
50+
return credential.Mechanism == MongoAuthenticationMechanism.CRAM_MD5 &&
5151
credential.Evidence is PasswordEvidence;
5252
}
5353

MongoDB.Driver/Communication/Security/Mechanisms/DigestMD5Mechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public string Name
4747
/// <exception cref="System.NotImplementedException"></exception>
4848
public bool CanUse(MongoCredential credential)
4949
{
50-
return credential.AuthenticationProtocol == MongoAuthenticationProtocol.Strongest &&
50+
return credential.Mechanism == MongoAuthenticationMechanism.DIGEST_MD5 &&
5151
credential.Evidence is PasswordEvidence;
5252
}
5353

MongoDB.Driver/Communication/Security/Mechanisms/GssapiMechanism.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public string Name
4949
/// <exception cref="System.NotImplementedException"></exception>
5050
public bool CanUse(MongoCredential credential)
5151
{
52-
if(credential.AuthenticationProtocol != MongoAuthenticationProtocol.Gssapi || !(credential.Identity is MongoExternalIdentity))
52+
if(credential.Mechanism != MongoAuthenticationMechanism.GSSAPI || !(credential.Identity is MongoExternalIdentity))
5353
{
5454
return false;
5555
}

MongoDB.Driver/Communication/Security/Mechanisms/Sspi/SecurityContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public void Initialize(string servicePrincipalName, byte[] inBytes, out byte[] o
292292
{
293293
try
294294
{
295-
var flags = SspiContextFlags.MutualAuth | SspiContextFlags.Confidentiality | SspiContextFlags.InitIntegrity;
295+
var flags = SspiContextFlags.MutualAuth;
296296

297297
uint result;
298298
long timestamp;

MongoDB.Driver/Communication/Security/MongoCRAuthenticationMethod.cs renamed to MongoDB.Driver/Communication/Security/MongoCRAuthenticationProtocol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace MongoDB.Driver.Communication.Security
2222
/// <summary>
2323
/// Authenticates a credential using the MONGO-CR protocol.
2424
/// </summary>
25-
internal class MongoCRAuthenticationMethod : IAuthenticationMethod
25+
internal class MongoCRAuthenticationProtocol : IAuthenticationProtocol
2626
{
2727
// public properties
2828
public string Name
@@ -77,7 +77,7 @@ public void Authenticate(MongoConnection connection, MongoCredential credential)
7777
/// </returns>
7878
public bool CanUse(MongoCredential credential)
7979
{
80-
return credential.AuthenticationProtocol == MongoAuthenticationProtocol.Strongest &&
80+
return credential.Mechanism == MongoAuthenticationMechanism.MONGO_CR &&
8181
credential.Evidence is PasswordEvidence;
8282
}
8383
}

MongoDB.Driver/Communication/Security/SaslAuthenticationMethod.cs renamed to MongoDB.Driver/Communication/Security/SaslAuthenticationProtocol.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ namespace MongoDB.Driver.Communication.Security
2525
/// <summary>
2626
/// Authenticates a credential using the SASL protocol.
2727
/// </summary>
28-
internal class SaslAuthenticationMethod : IAuthenticationMethod
28+
internal class SaslAuthenticationProtocol : IAuthenticationProtocol
2929
{
3030
// private fields
3131
private readonly ISaslMechanism _mechanism;
3232

3333
// constructors
3434
/// <summary>
35-
/// Initializes a new instance of the <see cref="SaslAuthenticationMethod" /> class.
35+
/// Initializes a new instance of the <see cref="SaslAuthenticationProtocol" /> class.
3636
/// </summary>
3737
/// <param name="mechanism">The mechanism.</param>
38-
public SaslAuthenticationMethod(ISaslMechanism mechanism)
38+
public SaslAuthenticationProtocol(ISaslMechanism mechanism)
3939
{
4040
_mechanism = mechanism;
4141
}

MongoDB.Driver/MongoAuthenticationProtocol.cs renamed to MongoDB.Driver/MongoAuthenticationMechanism.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@
1616
namespace MongoDB.Driver
1717
{
1818
/// <summary>
19-
/// The protocol used to authenticate with MongoDB.
19+
/// The mechanism used to authenticate with MongoDB.
2020
/// </summary>
21-
public enum MongoAuthenticationProtocol
21+
public enum MongoAuthenticationMechanism
2222
{
2323
/// <summary>
24-
/// Authenticate to the server using the strongest means possible.
24+
/// Authenticate to the server using the Mongo Challenge Response (MONGO-CR) protocol.
2525
/// </summary>
26-
Strongest,
26+
MONGO_CR,
27+
/// <summary>
28+
/// Authenticate to the server using CRAM-MD5.
29+
/// </summary>
30+
CRAM_MD5,
31+
/// <summary>
32+
/// Authenticate to the server using DIGEST-MD5.
33+
/// </summary>
34+
DIGEST_MD5,
2735
/// <summary>
2836
/// Authenticate to the server using GSSAPI.
2937
/// </summary>
30-
Gssapi
38+
GSSAPI
3139
}
3240
}

MongoDB.Driver/MongoClientSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public WriteConcern WriteConcern
422422
public static MongoClientSettings FromConnectionStringBuilder(MongoConnectionStringBuilder builder)
423423
{
424424
var credential = MongoCredential.FromComponents(
425-
builder.AuthenticationProtocol,
425+
builder.AuthenticationMechanism,
426426
builder.AuthenticationSource ?? builder.DatabaseName,
427427
builder.Username,
428428
builder.Password);
@@ -462,7 +462,7 @@ public static MongoClientSettings FromConnectionStringBuilder(MongoConnectionStr
462462
public static MongoClientSettings FromUrl(MongoUrl url)
463463
{
464464
var credential = MongoCredential.FromComponents(
465-
url.AuthenticationProtocol,
465+
url.AuthenticationMechanism,
466466
url.AuthenticationSource ?? url.DatabaseName,
467467
url.Username,
468468
url.Password);

0 commit comments

Comments
 (0)