Skip to content

Commit 4503c54

Browse files
committed
Ensure authsource defaults to dbname before admin
GODRIVER-486 Change-Id: Ie79cf08351b9f2bbbf1a4a47f548827ffee89cbf
1 parent 88e57ff commit 4503c54

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

core/connstring/connstring.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ func (p *parser) setDefaultAuthParams(dbName string) error {
310310
}
311311
case "":
312312
if p.AuthSource == "" {
313-
p.AuthSource = "admin"
313+
p.AuthSource = dbName
314+
if p.AuthSource == "" {
315+
p.AuthSource = "admin"
316+
}
314317
}
315318
default:
316319
return fmt.Errorf("invalid auth mechanism")

core/connstring/connstring_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ func TestAuthMechanism(t *testing.T) {
6666
}
6767
}
6868

69+
func TestAuthSource(t *testing.T) {
70+
tests := []struct {
71+
s string
72+
expected string
73+
err bool
74+
}{
75+
{s: "foobar?authSource=bazqux", expected: "bazqux"},
76+
{s: "foobar", expected: "foobar"},
77+
{s: "", expected: "admin"},
78+
}
79+
80+
for _, test := range tests {
81+
s := fmt.Sprintf("mongodb://user:pass@localhost/%s", test.s)
82+
t.Run(s, func(t *testing.T) {
83+
cs, err := connstring.Parse(s)
84+
if test.err {
85+
require.Error(t, err)
86+
} else {
87+
require.NoError(t, err)
88+
require.Equal(t, test.expected, cs.AuthSource)
89+
}
90+
})
91+
}
92+
}
93+
6994
func TestConnect(t *testing.T) {
7095
tests := []struct {
7196
s string

0 commit comments

Comments
 (0)