@@ -95,10 +95,11 @@ func RegisterAuthenticatorFactory(name string, factory AuthenticatorFactory) {
95
95
// function. DBUser is optional but must be of the form <dbname.username>;
96
96
// if non-empty, then the connection will do SASL mechanism negotiation.
97
97
type HandshakeOptions struct {
98
- AppName string
99
- Authenticator Authenticator
100
- Compressors []string
101
- DBUser string
98
+ AppName string
99
+ Authenticator Authenticator
100
+ Compressors []string
101
+ DBUser string
102
+ PerformAuthentication func (description.Server ) bool
102
103
}
103
104
104
105
// Handshaker creates a connection handshaker for the given authenticator.
@@ -114,9 +115,21 @@ func Handshaker(h connection.Handshaker, options *HandshakeOptions) connection.H
114
115
return description.Server {}, newAuthError ("handshake failure" , err )
115
116
}
116
117
117
- err = options .Authenticator .Auth (ctx , desc , rw )
118
- if err != nil {
119
- return description.Server {}, newAuthError ("auth error" , err )
118
+ performAuth := options .PerformAuthentication
119
+ if performAuth == nil {
120
+ performAuth = func (serv description.Server ) bool {
121
+ return serv .Kind == description .RSPrimary ||
122
+ serv .Kind == description .RSSecondary ||
123
+ serv .Kind == description .Mongos ||
124
+ serv .Kind == description .Standalone
125
+ }
126
+ }
127
+ if performAuth (desc ) && options .Authenticator != nil {
128
+ err = options .Authenticator .Auth (ctx , desc , rw )
129
+ if err != nil {
130
+ return description.Server {}, newAuthError ("auth error" , err )
131
+ }
132
+
120
133
}
121
134
if h == nil {
122
135
return desc , nil
0 commit comments