Skip to content

Commit cf93994

Browse files
committed
GODRIVER-519 Consolidate SCRAM implementations
Change-Id: I1f8aa73bd3b36e0ce13575d3fae9c8976472ead9
1 parent 1f112eb commit cf93994

File tree

5 files changed

+35
-792
lines changed

5 files changed

+35
-792
lines changed

core/auth/auth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestCreateAuthenticator(t *testing.T) {
2525
auther Authenticator
2626
}{
2727
{name: "", auther: &DefaultAuthenticator{}},
28-
{name: "SCRAM-SHA-1", auther: &ScramSHA1Authenticator{}},
29-
{name: "SCRAM-SHA-256", auther: &ScramSHA256Authenticator{}},
28+
{name: "SCRAM-SHA-1", auther: &ScramAuthenticator{}},
29+
{name: "SCRAM-SHA-256", auther: &ScramAuthenticator{}},
3030
{name: "MONGODB-CR", auther: &MongoDBCRAuthenticator{}},
3131
{name: "PLAIN", auther: &PlainAuthenticator{}},
3232
{name: "MONGODB-X509", auther: &MongoDBX509Authenticator{}},

core/auth/scramsha256.go renamed to core/auth/scram.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,26 @@ import (
1616
"github.com/xdg/stringprep"
1717
)
1818

19-
// SCRAMSHA256 is the mechanism name for SCRAM-SHA-256.
19+
// SCRAMSHA1 holds the mechanism name "SCRAM-SHA-1"
20+
const SCRAMSHA1 = "SCRAM-SHA-1"
21+
22+
// SCRAMSHA256 holds the mechanism name "SCRAM-SHA-256"
2023
const SCRAMSHA256 = "SCRAM-SHA-256"
2124

25+
func newScramSHA1Authenticator(cred *Cred) (Authenticator, error) {
26+
passdigest := mongoPasswordDigest(cred.Username, cred.Password)
27+
client, err := scram.SHA1.NewClientUnprepped(cred.Username, passdigest, "")
28+
if err != nil {
29+
return nil, newAuthError("error initializing SCRAM-SHA-1 client", err)
30+
}
31+
client.WithMinIterations(4096)
32+
return &ScramAuthenticator{
33+
mechanism: SCRAMSHA1,
34+
source: cred.Source,
35+
client: client,
36+
}, nil
37+
}
38+
2239
func newScramSHA256Authenticator(cred *Cred) (Authenticator, error) {
2340
passprep, err := stringprep.SASLprep.Prepare(cred.Password)
2441
if err != nil {
@@ -29,38 +46,41 @@ func newScramSHA256Authenticator(cred *Cred) (Authenticator, error) {
2946
return nil, newAuthError("error initializing SCRAM-SHA-256 client", err)
3047
}
3148
client.WithMinIterations(4096)
32-
return &ScramSHA256Authenticator{
33-
DB: cred.Source,
34-
client: client,
49+
return &ScramAuthenticator{
50+
mechanism: SCRAMSHA256,
51+
source: cred.Source,
52+
client: client,
3553
}, nil
3654
}
3755

38-
// ScramSHA256Authenticator uses the SCRAM-SHA-256 algorithm over SASL to authenticate a connection.
39-
type ScramSHA256Authenticator struct {
40-
DB string
41-
client *scram.Client
56+
// ScramAuthenticator uses the SCRAM algorithm over SASL to authenticate a connection.
57+
type ScramAuthenticator struct {
58+
mechanism string
59+
source string
60+
client *scram.Client
4261
}
4362

4463
// Auth authenticates the connection.
45-
func (a *ScramSHA256Authenticator) Auth(ctx context.Context, desc description.Server, rw wiremessage.ReadWriter) error {
46-
adapter := &scramSaslAdapter{conversation: a.client.NewConversation()}
47-
err := ConductSaslConversation(ctx, desc, rw, a.DB, adapter)
64+
func (a *ScramAuthenticator) Auth(ctx context.Context, desc description.Server, rw wiremessage.ReadWriter) error {
65+
adapter := &scramSaslAdapter{conversation: a.client.NewConversation(), mechanism: a.mechanism}
66+
err := ConductSaslConversation(ctx, desc, rw, a.source, adapter)
4867
if err != nil {
4968
return newAuthError("sasl conversation error", err)
5069
}
5170
return nil
5271
}
5372

5473
type scramSaslAdapter struct {
74+
mechanism string
5575
conversation *scram.ClientConversation
5676
}
5777

5878
func (a *scramSaslAdapter) Start() (string, []byte, error) {
5979
step, err := a.conversation.Step("")
6080
if err != nil {
61-
return SCRAMSHA256, nil, err
81+
return a.mechanism, nil, err
6282
}
63-
return SCRAMSHA256, []byte(step), nil
83+
return a.mechanism, []byte(step), nil
6484
}
6585

6686
func (a *scramSaslAdapter) Next(challenge []byte) ([]byte, error) {

core/auth/scramsha1.go

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

core/auth/scramsha1_internal_test.go

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

0 commit comments

Comments
 (0)