Skip to content

Commit 3e1f52f

Browse files
committed
initial removal of MongoAuthenticationMechanism enum
1 parent ca9ea7e commit 3e1f52f

12 files changed

+59
-109
lines changed

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.Mechanism != MongoAuthenticationMechanism.Gssapi || !(credential.Identity is MongoExternalIdentity))
52+
if (!credential.Mechanism.Equals("GSSAPI", StringComparison.InvariantCultureIgnoreCase) || !(credential.Identity is MongoExternalIdentity))
5353
{
5454
return false;
5555
}

MongoDB.Driver/Communication/Security/MongoCRAuthenticationProtocol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public void Authenticate(MongoConnection connection, MongoCredential credential)
7777
/// </returns>
7878
public bool CanUse(MongoCredential credential)
7979
{
80-
return credential.Mechanism == MongoAuthenticationMechanism.Mongo_CR &&
80+
return credential.Mechanism.Equals("MONGO-CR", StringComparison.InvariantCultureIgnoreCase) &&
8181
credential.Evidence is PasswordEvidence;
8282
}
8383
}
84-
}
84+
}

MongoDB.Driver/MongoAuthenticationMechanism.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

MongoDB.Driver/MongoConnectionStringBuilder.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class MongoConnectionStringBuilder : DbConnectionStringBuilder
7575

7676
// private fields
7777
// default values are set in ResetValues
78-
private MongoAuthenticationMechanism _authenticationMechanism;
78+
private string _authenticationMechanism;
7979
private string _authenticationSource;
8080
private ConnectionMode _connectionMode;
8181
private TimeSpan _connectTimeout;
@@ -127,16 +127,12 @@ public MongoConnectionStringBuilder(string connectionString)
127127
/// <summary>
128128
/// Gets or sets the authentication mechanism.
129129
/// </summary>
130-
public MongoAuthenticationMechanism AuthenticationMechanism
130+
public string AuthenticationMechanism
131131
{
132132
get { return _authenticationMechanism; }
133133
set
134134
{
135-
_authenticationMechanism = value;
136-
base["authMechanism"] = value
137-
.ToString()
138-
.ToUpper()
139-
.Replace("_", "-");
135+
base["authMechanism"] = _authenticationMechanism = value;
140136
}
141137
}
142138

@@ -674,15 +670,7 @@ public override object this[string keyword]
674670
switch (keyword.ToLower())
675671
{
676672
case "authmechanism":
677-
if (value is string)
678-
{
679-
string mechanism = ((string)value).Replace("-", "_");
680-
AuthenticationMechanism = (MongoAuthenticationMechanism)Enum.Parse(typeof(MongoAuthenticationMechanism), mechanism, true);
681-
}
682-
else
683-
{
684-
AuthenticationMechanism = (MongoAuthenticationMechanism)value;
685-
}
673+
AuthenticationMechanism = (string)value;
686674
break;
687675
case "authsource":
688676
AuthenticationSource = (string)value;
@@ -966,7 +954,7 @@ private IEnumerable<MongoServerAddress> ParseServersString(string value)
966954
private void ResetValues()
967955
{
968956
// set fields and not properties so base class items aren't set
969-
_authenticationMechanism = MongoAuthenticationMechanism.Mongo_CR;
957+
_authenticationMechanism = MongoDefaults.AuthenticationMechanism;
970958
_authenticationSource = null;
971959
_connectionMode = ConnectionMode.Automatic;
972960
_connectTimeout = MongoDefaults.ConnectTimeout;

MongoDB.Driver/MongoCredential.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class MongoCredential : IEquatable<MongoCredential>
2929
// private fields
3030
private readonly MongoIdentityEvidence _evidence;
3131
private readonly MongoIdentity _identity;
32-
private readonly MongoAuthenticationMechanism _mechanism;
32+
private readonly string _mechanism;
3333

3434
// constructors
3535
/// <summary>
@@ -38,7 +38,7 @@ public class MongoCredential : IEquatable<MongoCredential>
3838
/// <param name="mechanism">Mechanism to authenticate with.</param>
3939
/// <param name="identity">The identity.</param>
4040
/// <param name="evidence">The evidence.</param>
41-
public MongoCredential(MongoAuthenticationMechanism mechanism, MongoIdentity identity, MongoIdentityEvidence evidence)
41+
public MongoCredential(string mechanism, MongoIdentity identity, MongoIdentityEvidence evidence)
4242
{
4343
if (identity == null)
4444
{
@@ -74,7 +74,7 @@ public MongoIdentity Identity
7474
/// <summary>
7575
/// Gets the mechanism to authenticate with.
7676
/// </summary>
77-
public MongoAuthenticationMechanism Mechanism
77+
public string Mechanism
7878
{
7979
get { return _mechanism; }
8080
}
@@ -145,7 +145,7 @@ public string Username
145145
/// <remarks>This overload is used primarily on linux.</remarks>
146146
public static MongoCredential CreateGssapiCredential(string username)
147147
{
148-
return FromComponents(MongoAuthenticationMechanism.Gssapi,
148+
return FromComponents("GSSAPI",
149149
"$external",
150150
username,
151151
(PasswordEvidence)null);
@@ -159,7 +159,7 @@ public static MongoCredential CreateGssapiCredential(string username)
159159
/// <returns>A credential for GSSAPI.</returns>
160160
public static MongoCredential CreateGssapiCredential(string username, string password)
161161
{
162-
return FromComponents(MongoAuthenticationMechanism.Gssapi,
162+
return FromComponents("GSSAPI",
163163
"$external",
164164
username,
165165
new PasswordEvidence(password));
@@ -173,7 +173,7 @@ public static MongoCredential CreateGssapiCredential(string username, string pas
173173
/// <returns>A credential for GSSAPI.</returns>
174174
public static MongoCredential CreateGssapiCredential(string username, SecureString password)
175175
{
176-
return FromComponents(MongoAuthenticationMechanism.Gssapi,
176+
return FromComponents("GSSAPI",
177177
"$external",
178178
username,
179179
new PasswordEvidence(password));
@@ -188,7 +188,7 @@ public static MongoCredential CreateGssapiCredential(string username, SecureStri
188188
/// <returns></returns>
189189
public static MongoCredential CreateMongoCRCredential(string databaseName, string username, string password)
190190
{
191-
return FromComponents(MongoAuthenticationMechanism.Mongo_CR,
191+
return FromComponents("MONGO-CR",
192192
databaseName,
193193
username,
194194
new PasswordEvidence(password));
@@ -203,7 +203,7 @@ public static MongoCredential CreateMongoCRCredential(string databaseName, strin
203203
/// <returns></returns>
204204
public static MongoCredential CreateMongoCRCredential(string databaseName, string username, SecureString password)
205205
{
206-
return FromComponents(MongoAuthenticationMechanism.Mongo_CR,
206+
return FromComponents("MONGO-CR",
207207
databaseName,
208208
username,
209209
new PasswordEvidence(password));
@@ -255,7 +255,7 @@ public override string ToString()
255255
}
256256

257257
// internal static methods
258-
internal static MongoCredential FromComponents(MongoAuthenticationMechanism mechanism, string source, string username, string password)
258+
internal static MongoCredential FromComponents(string mechanism, string source, string username, string password)
259259
{
260260
var evidence = password == null ? (MongoIdentityEvidence)new ExternalEvidence() : new PasswordEvidence(password);
261261
return FromComponents(mechanism, source, username, evidence);
@@ -275,41 +275,45 @@ private void ValidatePassword(string password)
275275
}
276276

277277
// private static methods
278-
private static MongoCredential FromComponents(MongoAuthenticationMechanism mechanism, string source, string username, MongoIdentityEvidence evidence)
278+
private static MongoCredential FromComponents(string mechanism, string source, string username, MongoIdentityEvidence evidence)
279279
{
280+
if (string.IsNullOrEmpty(mechanism))
281+
{
282+
throw new ArgumentException("Cannot be null or empty.", "mechanism");
283+
}
280284
if (string.IsNullOrEmpty(username))
281285
{
282286
return null;
283287
}
284288

285-
switch (mechanism)
289+
switch (mechanism.ToUpperInvariant())
286290
{
287-
case MongoAuthenticationMechanism.Mongo_CR:
291+
case "MONGO-CR":
288292
// it is allowed for a password to be an empty string, but not a username
289293
source = source ?? "admin";
290294
if (evidence == null || !(evidence is PasswordEvidence))
291295
{
292-
throw new ArgumentException(string.Format("A {0} credential must have a password.", mechanism));
296+
throw new ArgumentException("A MONGO-CR credential must have a password.");
293297
}
294298

295299
return new MongoCredential(
296300
mechanism,
297301
new MongoInternalIdentity(source, username),
298302
evidence);
299-
case MongoAuthenticationMechanism.Gssapi:
303+
case "GSSAPI":
300304
source = source ?? "$external";
301305
if (source != "$external")
302306
{
303307
throw new ArgumentException("The source for GSSAPI must be $external.");
304308
}
305309

306310
return new MongoCredential(
307-
MongoAuthenticationMechanism.Gssapi,
311+
"GSSAPI",
308312
new MongoExternalIdentity(source, username),
309313
evidence);
310314
default:
311315
throw new NotSupportedException(string.Format("Unsupported MongoAuthenticationMechanism {0}.", mechanism));
312316
}
313317
}
314318
}
315-
}
319+
}

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@
170170
<Compile Include="IMongoScope.cs" />
171171
<Compile Include="IMongoSortBy.cs" />
172172
<Compile Include="IMongoUpdate.cs" />
173-
<Compile Include="MongoAuthenticationMechanism.cs" />
174173
<Compile Include="MongoClient.cs" />
175174
<Compile Include="MongoClientSettings.cs" />
176175
<Compile Include="MongoCollectionSettings.cs" />

MongoDB.Driver/MongoDefaults.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class MongoDefaults
2525
{
2626
// private static fields
2727
private static bool __assignIdOnInsert = true;
28+
private static string __authenticationMechanism = "MONGO-CR";
2829
private static TimeSpan __connectTimeout = TimeSpan.FromSeconds(30);
2930
private static TimeSpan __maxConnectionIdleTime = TimeSpan.FromMinutes(10);
3031
private static TimeSpan __maxConnectionLifeTime = TimeSpan.FromMinutes(30);
@@ -52,6 +53,15 @@ public static bool AssignIdOnInsert
5253
set { __assignIdOnInsert = value; }
5354
}
5455

56+
/// <summary>
57+
/// Gets or sets the default authentication mechanism.
58+
/// </summary>
59+
public static string AuthenticationMechanism
60+
{
61+
get { return __authenticationMechanism; }
62+
set { __authenticationMechanism = value; }
63+
}
64+
5565
/// <summary>
5666
/// Gets the actual wait queue size (either WaitQueueSize or WaitQueueMultiple x MaxConnectionPoolSize).
5767
/// </summary>

MongoDB.Driver/MongoUrl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class MongoUrl : IEquatable<MongoUrl>
5555
private static Dictionary<string, MongoUrl> __cache = new Dictionary<string, MongoUrl>();
5656

5757
// private fields
58-
private readonly MongoAuthenticationMechanism _authenticationMechanism;
58+
private readonly string _authenticationMechanism;
5959
private readonly string _authenticationSource;
6060
private readonly ConnectionMode _connectionMode;
6161
private readonly TimeSpan _connectTimeout;
@@ -130,7 +130,7 @@ public MongoUrl(string url)
130130
/// <summary>
131131
/// Gets the authentication mechanism.
132132
/// </summary>
133-
public MongoAuthenticationMechanism AuthenticationMechanism
133+
public string AuthenticationMechanism
134134
{
135135
get { return _authenticationMechanism; }
136136
}

MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace MongoDB.Driver
3131
public class MongoUrlBuilder
3232
{
3333
// private fields
34-
private MongoAuthenticationMechanism _authenticationMechanism;
34+
private string _authenticationMechanism;
3535
private string _authenticationSource;
3636
private ConnectionMode _connectionMode;
3737
private TimeSpan _connectTimeout;
@@ -66,7 +66,7 @@ public class MongoUrlBuilder
6666
/// </summary>
6767
public MongoUrlBuilder()
6868
{
69-
_authenticationMechanism = MongoAuthenticationMechanism.Mongo_CR;
69+
_authenticationMechanism = MongoDefaults.AuthenticationMechanism;
7070
_authenticationSource = null;
7171
_connectionMode = ConnectionMode.Automatic;
7272
_connectTimeout = MongoDefaults.ConnectTimeout;
@@ -110,7 +110,7 @@ public MongoUrlBuilder(string url)
110110
/// <summary>
111111
/// Gets or sets the authentication mechanism.
112112
/// </summary>
113-
public MongoAuthenticationMechanism AuthenticationMechanism
113+
public string AuthenticationMechanism
114114
{
115115
get { return _authenticationMechanism; }
116116
set { _authenticationMechanism = value; }
@@ -588,20 +588,6 @@ internal static string FormatTimeSpan(TimeSpan value)
588588
}
589589
}
590590

591-
internal static MongoAuthenticationMechanism ParseAuthenticationMechanism(string name, string s)
592-
{
593-
try
594-
{
595-
// enumerations cannot use -'s, so we replace them with _'s.
596-
s = s.Replace("-", "_");
597-
return (MongoAuthenticationMechanism)Enum.Parse(typeof(MongoAuthenticationMechanism), s, true); // ignoreCase
598-
}
599-
catch (ArgumentException)
600-
{
601-
throw new FormatException(FormatMessage(name, s));
602-
}
603-
}
604-
605591
internal static ConnectionMode ParseConnectionMode(string name, string s)
606592
{
607593
try
@@ -827,7 +813,7 @@ public void Parse(string url)
827813
switch (name.ToLower())
828814
{
829815
case "authmechanism":
830-
_authenticationMechanism = ParseAuthenticationMechanism(name, value);
816+
_authenticationMechanism = value;
831817
break;
832818
case "authsource":
833819
_authenticationSource = value;
@@ -1014,14 +1000,9 @@ public override string ToString()
10141000
url.Append(_databaseName);
10151001
}
10161002
var query = new StringBuilder();
1017-
if (_authenticationMechanism != MongoAuthenticationMechanism.Mongo_CR)
1003+
if (!_authenticationMechanism.Equals("MONGO-CR", StringComparison.InvariantCultureIgnoreCase))
10181004
{
1019-
string mechanismName = _authenticationMechanism
1020-
.ToString()
1021-
.ToUpper()
1022-
.Replace("_", "-");
1023-
1024-
query.AppendFormat("authMechanism={0};", mechanismName);
1005+
query.AppendFormat("authMechanism={0};", _authenticationMechanism);
10251006
}
10261007
if (_authenticationSource != null)
10271008
{

MongoDB.DriverUnitTests/MongoConnectionStringBuilderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestAll()
3737
};
3838
var built = new MongoConnectionStringBuilder()
3939
{
40-
AuthenticationMechanism = MongoAuthenticationMechanism.Gssapi,
40+
AuthenticationMechanism = "GSSAPI",
4141
AuthenticationSource = "db",
4242
ConnectionMode = ConnectionMode.ReplicaSet,
4343
ConnectTimeout = TimeSpan.FromSeconds(1),
@@ -96,7 +96,7 @@ public void TestAll()
9696

9797
foreach (var builder in EnumerateBuiltAndParsedBuilders(built, connectionString))
9898
{
99-
Assert.AreEqual(MongoAuthenticationMechanism.Gssapi, builder.AuthenticationMechanism);
99+
Assert.AreEqual("GSSAPI", builder.AuthenticationMechanism);
100100
Assert.AreEqual("db", builder.AuthenticationSource);
101101
Assert.AreEqual(123, builder.ComputedWaitQueueSize);
102102
Assert.AreEqual(ConnectionMode.ReplicaSet, builder.ConnectionMode);
@@ -135,9 +135,9 @@ public void TestAll()
135135
}
136136

137137
[Test]
138-
[TestCase(MongoAuthenticationMechanism.Mongo_CR, "server=localhost;authMechanism=MONGO-CR")]
139-
[TestCase(MongoAuthenticationMechanism.Gssapi, "server=localhost;authMechanism=GSSAPI")]
140-
public void TestAuthMechanism(MongoAuthenticationMechanism mechanism, string connectionString)
138+
[TestCase("MONGO-CR", "server=localhost;authMechanism=MONGO-CR")]
139+
[TestCase("GSSAPI", "server=localhost;authMechanism=GSSAPI")]
140+
public void TestAuthMechanism(string mechanism, string connectionString)
141141
{
142142
var built = new MongoConnectionStringBuilder { Server = _localhost, AuthenticationMechanism = mechanism };
143143

0 commit comments

Comments
 (0)