Skip to content

Commit 1ecf305

Browse files
author
Divjot Arora
authored
GODRIVER-1473 Allow authSource without credentials (#381)
1 parent e2623ca commit 1ecf305

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

data/auth/connection-string.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@
352352
"credential": null
353353
},
354354
{
355-
"description": "authSource without username is invalid (default mechanism)",
355+
"description": "authSource without username doesn't create credential (default mechanism)",
356356
"uri": "mongodb://localhost/?authSource=foo",
357-
"valid": false
358-
},
357+
"valid": true,
358+
"credential": null
359+
},
359360
{
360361
"description": "should recognise the mechanism (MONGODB-AWS)",
361362
"uri": "mongodb://localhost/?authMechanism=MONGODB-AWS",

data/auth/connection-string.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ tests:
288288
valid: true
289289
credential: ~
290290
-
291-
description: "authSource without username is invalid (default mechanism)"
291+
description: "authSource without username doesn't create credential (default mechanism)"
292292
uri: "mongodb://localhost/?authSource=foo"
293-
valid: false
293+
valid: true
294+
credential: ~
294295
-
295296
description: "should recognise the mechanism (MONGODB-AWS)"
296297
uri: "mongodb://localhost/?authMechanism=MONGODB-AWS"

mongo/options/clientoptions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ func (c *ClientOptions) ApplyURI(uri string) *ClientOptions {
174174
c.AppName = &cs.AppName
175175
}
176176

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() {
179179
c.Auth = &Credential{
180180
AuthMechanism: cs.AuthMechanism,
181181
AuthMechanismProperties: cs.AuthMechanismProperties,

mongo/options/clientoptions_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ func TestClientOptions(t *testing.T) {
240240
Username: "foo",
241241
}),
242242
},
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-
},
250243
{
251244
"AuthSource",
252245
"mongodb://foo@localhost/?authSource=random-database-example",

x/mongo/driver/connstring/connstring.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func (u *ConnString) String() string {
130130
return u.Original
131131
}
132132

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+
133141
// Validate checks that the Auth and SSL parameters are valid values.
134142
func (u *ConnString) Validate() error {
135143
p := parser{
@@ -345,6 +353,7 @@ func (p *parser) setDefaultAuthParams(dbName string) error {
345353
}
346354
}
347355
case "":
356+
// Only set auth source if there is a request for authentication via non-empty credentials.
348357
if p.AuthSource == "" && (p.AuthMechanismProperties != nil || p.Username != "" || p.PasswordSet) {
349358
p.AuthSource = dbName
350359
if p.AuthSource == "" {
@@ -433,9 +442,6 @@ func (p *parser) validateAuth() error {
433442
return fmt.Errorf("SCRAM-SHA-256 cannot have mechanism properties")
434443
}
435444
case "":
436-
if p.Username == "" && p.AuthSource != "" {
437-
return fmt.Errorf("authsource without username is invalid")
438-
}
439445
default:
440446
return fmt.Errorf("invalid auth mechanism")
441447
}

0 commit comments

Comments
 (0)