16
16
using System ;
17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
- using System . Runtime . InteropServices ;
20
19
using System . Security ;
21
20
using MongoDB . Driver . Core . Authentication ;
22
21
using MongoDB . Shared ;
@@ -165,7 +164,9 @@ public string Username
165
164
/// <returns>A default credential.</returns>
166
165
public static MongoCredential CreateCredential ( string databaseName , string username , string password )
167
166
{
168
- return FromComponents ( null ,
167
+ return FromComponents (
168
+ mechanism : null ,
169
+ source : null ,
169
170
databaseName ,
170
171
username ,
171
172
new PasswordEvidence ( password ) ) ;
@@ -186,7 +187,9 @@ public static MongoCredential CreateCredential(string databaseName, string usern
186
187
/// <returns>A default credential.</returns>
187
188
public static MongoCredential CreateCredential ( string databaseName , string username , SecureString password )
188
189
{
189
- return FromComponents ( null ,
190
+ return FromComponents (
191
+ mechanism : null ,
192
+ source : null ,
190
193
databaseName ,
191
194
username ,
192
195
new PasswordEvidence ( password ) ) ;
@@ -200,8 +203,10 @@ public static MongoCredential CreateCredential(string databaseName, string usern
200
203
/// <remarks>This overload is used primarily on linux.</remarks>
201
204
public static MongoCredential CreateGssapiCredential ( string username )
202
205
{
203
- return FromComponents ( "GSSAPI" ,
204
- "$external" ,
206
+ return FromComponents (
207
+ mechanism : "GSSAPI" ,
208
+ source : "$external" ,
209
+ databaseName : null ,
205
210
username ,
206
211
new ExternalEvidence ( ) ) ;
207
212
}
@@ -214,8 +219,10 @@ public static MongoCredential CreateGssapiCredential(string username)
214
219
/// <returns>A credential for GSSAPI.</returns>
215
220
public static MongoCredential CreateGssapiCredential ( string username , string password )
216
221
{
217
- return FromComponents ( "GSSAPI" ,
218
- "$external" ,
222
+ return FromComponents (
223
+ mechanism : "GSSAPI" ,
224
+ source : "$external" ,
225
+ databaseName : null ,
219
226
username ,
220
227
new PasswordEvidence ( password ) ) ;
221
228
}
@@ -228,8 +235,10 @@ public static MongoCredential CreateGssapiCredential(string username, string pas
228
235
/// <returns>A credential for GSSAPI.</returns>
229
236
public static MongoCredential CreateGssapiCredential ( string username , SecureString password )
230
237
{
231
- return FromComponents ( "GSSAPI" ,
232
- "$external" ,
238
+ return FromComponents (
239
+ mechanism : "GSSAPI" ,
240
+ source : "$external" ,
241
+ databaseName : null ,
233
242
username ,
234
243
new PasswordEvidence ( password ) ) ;
235
244
}
@@ -244,7 +253,9 @@ public static MongoCredential CreateGssapiCredential(string username, SecureStri
244
253
[ Obsolete ( "MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated." ) ]
245
254
public static MongoCredential CreateMongoCRCredential ( string databaseName , string username , string password )
246
255
{
247
- return FromComponents ( "MONGODB-CR" ,
256
+ return FromComponents (
257
+ mechanism : "MONGODB-CR" ,
258
+ source : null ,
248
259
databaseName ,
249
260
username ,
250
261
new PasswordEvidence ( password ) ) ;
@@ -260,7 +271,9 @@ public static MongoCredential CreateMongoCRCredential(string databaseName, strin
260
271
[ Obsolete ( "MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated." ) ]
261
272
public static MongoCredential CreateMongoCRCredential ( string databaseName , string username , SecureString password )
262
273
{
263
- return FromComponents ( "MONGODB-CR" ,
274
+ return FromComponents (
275
+ mechanism : "MONGODB-CR" ,
276
+ source : null ,
264
277
databaseName ,
265
278
username ,
266
279
new PasswordEvidence ( password ) ) ;
@@ -273,8 +286,10 @@ public static MongoCredential CreateMongoCRCredential(string databaseName, strin
273
286
/// <returns>A credential for MONGODB-X509.</returns>
274
287
public static MongoCredential CreateMongoX509Credential ( string username )
275
288
{
276
- return FromComponents ( "MONGODB-X509" ,
277
- "$external" ,
289
+ return FromComponents (
290
+ mechanism : "MONGODB-X509" ,
291
+ source : "$external" ,
292
+ databaseName : null ,
278
293
username ,
279
294
new ExternalEvidence ( ) ) ;
280
295
}
@@ -288,7 +303,9 @@ public static MongoCredential CreateMongoX509Credential(string username)
288
303
/// <returns>A credential for PLAIN.</returns>
289
304
public static MongoCredential CreatePlainCredential ( string databaseName , string username , string password )
290
305
{
291
- return FromComponents ( "PLAIN" ,
306
+ return FromComponents (
307
+ mechanism : "PLAIN" ,
308
+ source : null ,
292
309
databaseName ,
293
310
username ,
294
311
new PasswordEvidence ( password ) ) ;
@@ -303,7 +320,9 @@ public static MongoCredential CreatePlainCredential(string databaseName, string
303
320
/// <returns>A credential for PLAIN.</returns>
304
321
public static MongoCredential CreatePlainCredential ( string databaseName , string username , SecureString password )
305
322
{
306
- return FromComponents ( "PLAIN" ,
323
+ return FromComponents (
324
+ mechanism : "PLAIN" ,
325
+ source : null ,
307
326
databaseName ,
308
327
username ,
309
328
new PasswordEvidence ( password ) ) ;
@@ -452,9 +471,14 @@ internal IAuthenticator ToAuthenticator()
452
471
453
472
// internal static methods
454
473
internal static MongoCredential FromComponents ( string mechanism , string source , string username , string password )
474
+ {
475
+ return FromComponents ( mechanism , source , databaseName : null , username , password ) ;
476
+ }
477
+
478
+ internal static MongoCredential FromComponents ( string mechanism , string source , string databaseName , string username , string password )
455
479
{
456
480
var evidence = password == null ? ( MongoIdentityEvidence ) new ExternalEvidence ( ) : new PasswordEvidence ( password ) ;
457
- return FromComponents ( mechanism , source , username , evidence ) ;
481
+ return FromComponents ( mechanism , source , databaseName , username , evidence ) ;
458
482
}
459
483
460
484
// private methods
@@ -471,7 +495,15 @@ private void ValidatePassword(string password)
471
495
}
472
496
473
497
// private static methods
474
- private static MongoCredential FromComponents ( string mechanism , string source , string username , MongoIdentityEvidence evidence )
498
+ private static void EnsureNullOrExternalSource ( string mechanism , string source )
499
+ {
500
+ if ( source != null && source != "$external" )
501
+ {
502
+ throw new ArgumentException ( $ "A { mechanism } source must be $external.", nameof ( source ) ) ;
503
+ }
504
+ }
505
+
506
+ private static MongoCredential FromComponents ( string mechanism , string source , string databaseName , string username , MongoIdentityEvidence evidence )
475
507
{
476
508
var defaultedMechanism = ( mechanism ?? "DEFAULT" ) . Trim ( ) . ToUpperInvariant ( ) ;
477
509
switch ( defaultedMechanism )
@@ -481,7 +513,7 @@ private static MongoCredential FromComponents(string mechanism, string source, s
481
513
case "SCRAM-SHA-1" :
482
514
case "SCRAM-SHA-256" :
483
515
// it is allowed for a password to be an empty string, but not a username
484
- source = source ?? "admin" ;
516
+ source = source ?? databaseName ?? "admin" ;
485
517
if ( evidence == null || ! ( evidence is PasswordEvidence ) )
486
518
{
487
519
var message = string . Format ( "A {0} credential must have a password." , defaultedMechanism ) ;
@@ -493,8 +525,8 @@ private static MongoCredential FromComponents(string mechanism, string source, s
493
525
new MongoInternalIdentity ( source , username ) ,
494
526
evidence ) ;
495
527
case "MONGODB-X509" :
496
- // always $external for X509.
497
- source = "$external" ;
528
+ // MUST be " $external". Defaults to $external.
529
+ EnsureNullOrExternalSource ( mechanism , source ) ;
498
530
if ( evidence == null || ! ( evidence is ExternalEvidence ) )
499
531
{
500
532
throw new ArgumentException ( "A MONGODB-X509 does not support a password." ) ;
@@ -505,15 +537,15 @@ private static MongoCredential FromComponents(string mechanism, string source, s
505
537
new MongoX509Identity ( username ) ,
506
538
evidence ) ;
507
539
case "GSSAPI" :
508
- // always $external for GSSAPI.
509
- source = "$external" ;
540
+ // MUST be " $external". Defaults to $external.
541
+ EnsureNullOrExternalSource ( mechanism , source ) ;
510
542
511
543
return new MongoCredential (
512
- "GSSAPI" ,
513
- new MongoExternalIdentity ( source , username ) ,
544
+ mechanism ,
545
+ new MongoExternalIdentity ( username ) ,
514
546
evidence ) ;
515
547
case "PLAIN" :
516
- source = source ?? "admin ";
548
+ source = source ?? databaseName ?? "$external ";
517
549
if ( evidence == null || ! ( evidence is PasswordEvidence ) )
518
550
{
519
551
throw new ArgumentException ( "A PLAIN credential must have a password." ) ;
@@ -522,7 +554,7 @@ private static MongoCredential FromComponents(string mechanism, string source, s
522
554
MongoIdentity identity ;
523
555
if ( source == "$external" )
524
556
{
525
- identity = new MongoExternalIdentity ( source , username ) ;
557
+ identity = new MongoExternalIdentity ( username ) ;
526
558
}
527
559
else
528
560
{
0 commit comments