File tree Expand file tree Collapse file tree 5 files changed +18
-17
lines changed
x/mongo/driver/connstring Expand file tree Collapse file tree 5 files changed +18
-17
lines changed Original file line number Diff line number Diff line change 352
352
"credential" : null
353
353
},
354
354
{
355
- "description" : " authSource without username is invalid (default mechanism)" ,
355
+ "description" : " authSource without username doesn't create credential (default mechanism)" ,
356
356
"uri" : " mongodb://localhost/?authSource=foo" ,
357
- "valid" : false
358
- },
357
+ "valid" : true ,
358
+ "credential" : null
359
+ },
359
360
{
360
361
"description" : " should recognise the mechanism (MONGODB-AWS)" ,
361
362
"uri" : " mongodb://localhost/?authMechanism=MONGODB-AWS" ,
Original file line number Diff line number Diff line change @@ -288,9 +288,10 @@ tests:
288
288
valid : true
289
289
credential : ~
290
290
-
291
- description : " authSource without username is invalid (default mechanism)"
291
+ description : " authSource without username doesn't create credential (default mechanism)"
292
292
uri : " mongodb://localhost/?authSource=foo"
293
- valid : false
293
+ valid : true
294
+ credential : ~
294
295
-
295
296
description : " should recognise the mechanism (MONGODB-AWS)"
296
297
uri : " mongodb://localhost/?authMechanism=MONGODB-AWS"
Original file line number Diff line number Diff line change @@ -174,8 +174,8 @@ func (c *ClientOptions) ApplyURI(uri string) *ClientOptions {
174
174
c .AppName = & cs .AppName
175
175
}
176
176
177
- if cs . AuthMechanism != "" || cs . AuthMechanismProperties != nil || cs . AuthSource != "" ||
178
- cs . Username != "" || cs .PasswordSet {
177
+ // Only create a Credential if there is a request for authentication via non-empty credentials in the URI.
178
+ if cs .HasAuthParameters () {
179
179
c .Auth = & Credential {
180
180
AuthMechanism : cs .AuthMechanism ,
181
181
AuthMechanismProperties : cs .AuthMechanismProperties ,
Original file line number Diff line number Diff line change @@ -240,13 +240,6 @@ func TestClientOptions(t *testing.T) {
240
240
Username : "foo" ,
241
241
}),
242
242
},
243
- {
244
- "AuthSourceNoUsername" ,
245
- "mongodb://localhost/?authSource=random-database-example" ,
246
- & ClientOptions {err : internal .WrapErrorf (
247
- errors .New ("authsource without username is invalid" ), "error validating uri" ,
248
- )},
249
- },
250
243
{
251
244
"AuthSource" ,
252
245
"mongodb://foo@localhost/?authSource=random-database-example" ,
Original file line number Diff line number Diff line change @@ -130,6 +130,14 @@ func (u *ConnString) String() string {
130
130
return u .Original
131
131
}
132
132
133
+ // HasAuthParameters returns true if this ConnString has any authentication parameters set and therefore represents
134
+ // a request for authentication.
135
+ func (u * ConnString ) HasAuthParameters () bool {
136
+ // Check all auth parameters except for AuthSource because an auth source without other credentials is semantically
137
+ // valid and must not be interpreted as a request for authentication.
138
+ return u .AuthMechanism != "" || u .AuthMechanismProperties != nil || u .Username != "" || u .PasswordSet
139
+ }
140
+
133
141
// Validate checks that the Auth and SSL parameters are valid values.
134
142
func (u * ConnString ) Validate () error {
135
143
p := parser {
@@ -345,6 +353,7 @@ func (p *parser) setDefaultAuthParams(dbName string) error {
345
353
}
346
354
}
347
355
case "" :
356
+ // Only set auth source if there is a request for authentication via non-empty credentials.
348
357
if p .AuthSource == "" && (p .AuthMechanismProperties != nil || p .Username != "" || p .PasswordSet ) {
349
358
p .AuthSource = dbName
350
359
if p .AuthSource == "" {
@@ -433,9 +442,6 @@ func (p *parser) validateAuth() error {
433
442
return fmt .Errorf ("SCRAM-SHA-256 cannot have mechanism properties" )
434
443
}
435
444
case "" :
436
- if p .Username == "" && p .AuthSource != "" {
437
- return fmt .Errorf ("authsource without username is invalid" )
438
- }
439
445
default :
440
446
return fmt .Errorf ("invalid auth mechanism" )
441
447
}
You can’t perform that action at this time.
0 commit comments