@@ -16,9 +16,26 @@ import (
16
16
"github.com/xdg/stringprep"
17
17
)
18
18
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"
20
23
const SCRAMSHA256 = "SCRAM-SHA-256"
21
24
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
+
22
39
func newScramSHA256Authenticator (cred * Cred ) (Authenticator , error ) {
23
40
passprep , err := stringprep .SASLprep .Prepare (cred .Password )
24
41
if err != nil {
@@ -29,38 +46,41 @@ func newScramSHA256Authenticator(cred *Cred) (Authenticator, error) {
29
46
return nil , newAuthError ("error initializing SCRAM-SHA-256 client" , err )
30
47
}
31
48
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 ,
35
53
}, nil
36
54
}
37
55
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
42
61
}
43
62
44
63
// 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 )
48
67
if err != nil {
49
68
return newAuthError ("sasl conversation error" , err )
50
69
}
51
70
return nil
52
71
}
53
72
54
73
type scramSaslAdapter struct {
74
+ mechanism string
55
75
conversation * scram.ClientConversation
56
76
}
57
77
58
78
func (a * scramSaslAdapter ) Start () (string , []byte , error ) {
59
79
step , err := a .conversation .Step ("" )
60
80
if err != nil {
61
- return SCRAMSHA256 , nil , err
81
+ return a . mechanism , nil , err
62
82
}
63
- return SCRAMSHA256 , []byte (step ), nil
83
+ return a . mechanism , []byte (step ), nil
64
84
}
65
85
66
86
func (a * scramSaslAdapter ) Next (challenge []byte ) ([]byte , error ) {
0 commit comments