@@ -51,10 +51,9 @@ public ConnectionInitializerContext Authenticate(IConnection connection, Connect
51
51
{
52
52
Ensure . IsNotNull ( connection , nameof ( connection ) ) ;
53
53
Ensure . IsNotNull ( connectionInitializerContext , nameof ( connectionInitializerContext ) ) ;
54
- var authenticators = Ensure . IsNotNull ( connectionInitializerContext . Authenticators , nameof ( connectionInitializerContext . Authenticators ) ) ;
55
54
var description = Ensure . IsNotNull ( connectionInitializerContext . Description , nameof ( connectionInitializerContext . Description ) ) ;
56
55
57
- AuthenticationHelper . Authenticate ( connection , description , authenticators , cancellationToken ) ;
56
+ AuthenticationHelper . Authenticate ( connection , description , connectionInitializerContext . Authenticator , cancellationToken ) ;
58
57
59
58
// Connection description should be updated only on the initial handshake and not after reauthentication
60
59
if ( ! description . IsInitialized ( ) )
@@ -80,17 +79,16 @@ public ConnectionInitializerContext Authenticate(IConnection connection, Connect
80
79
}
81
80
}
82
81
83
- return new ConnectionInitializerContext ( description , authenticators ) ;
82
+ return new ConnectionInitializerContext ( description , connectionInitializerContext . Authenticator ) ;
84
83
}
85
84
86
85
public async Task < ConnectionInitializerContext > AuthenticateAsync ( IConnection connection , ConnectionInitializerContext connectionInitializerContext , CancellationToken cancellationToken )
87
86
{
88
87
Ensure . IsNotNull ( connection , nameof ( connection ) ) ;
89
88
Ensure . IsNotNull ( connectionInitializerContext , nameof ( connectionInitializerContext ) ) ;
90
- var authenticators = Ensure . IsNotNull ( connectionInitializerContext . Authenticators , nameof ( connectionInitializerContext . Authenticators ) ) ;
91
89
var description = Ensure . IsNotNull ( connectionInitializerContext . Description , nameof ( connectionInitializerContext . Description ) ) ;
92
90
93
- await AuthenticationHelper . AuthenticateAsync ( connection , description , authenticators , cancellationToken ) . ConfigureAwait ( false ) ;
91
+ await AuthenticationHelper . AuthenticateAsync ( connection , description , connectionInitializerContext . Authenticator , cancellationToken ) . ConfigureAwait ( false ) ;
94
92
95
93
// Connection description should be updated only on the initial handshake and not while reauthentication
96
94
if ( ! description . IsInitialized ( ) )
@@ -118,37 +116,37 @@ public async Task<ConnectionInitializerContext> AuthenticateAsync(IConnection co
118
116
}
119
117
}
120
118
121
- return new ConnectionInitializerContext ( description , authenticators ) ;
119
+ return new ConnectionInitializerContext ( description , connectionInitializerContext . Authenticator ) ;
122
120
}
123
121
124
122
public ConnectionInitializerContext SendHello ( IConnection connection , CancellationToken cancellationToken )
125
123
{
126
124
Ensure . IsNotNull ( connection , nameof ( connection ) ) ;
127
- var authenticators = CreateAuthenticators ( connection ) ;
128
- var helloCommand = CreateInitialHelloCommand ( authenticators , connection . Settings . LoadBalanced , cancellationToken ) ;
125
+ var authenticator = CreateAuthenticator ( connection ) ;
126
+ var helloCommand = CreateInitialHelloCommand ( authenticator , connection . Settings . LoadBalanced , cancellationToken ) ;
129
127
var helloProtocol = HelloHelper . CreateProtocol ( helloCommand , _serverApi ) ;
130
128
var helloResult = HelloHelper . GetResult ( connection , helloProtocol , cancellationToken ) ;
131
129
if ( connection . Settings . LoadBalanced && ! helloResult . ServiceId . HasValue )
132
130
{
133
131
throw new InvalidOperationException ( "Driver attempted to initialize in load balancing mode, but the server does not support this mode." ) ;
134
132
}
135
133
136
- return new ( new ConnectionDescription ( connection . ConnectionId , helloResult ) , authenticators ) ;
134
+ return new ( new ConnectionDescription ( connection . ConnectionId , helloResult ) , authenticator ) ;
137
135
}
138
136
139
137
public async Task < ConnectionInitializerContext > SendHelloAsync ( IConnection connection , CancellationToken cancellationToken )
140
138
{
141
139
Ensure . IsNotNull ( connection , nameof ( connection ) ) ;
142
- var authenticators = CreateAuthenticators ( connection ) ;
143
- var helloCommand = CreateInitialHelloCommand ( authenticators , connection . Settings . LoadBalanced , cancellationToken ) ;
140
+ var authenticator = CreateAuthenticator ( connection ) ;
141
+ var helloCommand = CreateInitialHelloCommand ( authenticator , connection . Settings . LoadBalanced , cancellationToken ) ;
144
142
var helloProtocol = HelloHelper . CreateProtocol ( helloCommand , _serverApi ) ;
145
143
var helloResult = await HelloHelper . GetResultAsync ( connection , helloProtocol , cancellationToken ) . ConfigureAwait ( false ) ;
146
144
if ( connection . Settings . LoadBalanced && ! helloResult . ServiceId . HasValue )
147
145
{
148
146
throw new InvalidOperationException ( "Driver attempted to initialize in load balancing mode, but the server does not support this mode." ) ;
149
147
}
150
148
151
- return new ( new ConnectionDescription ( connection . ConnectionId , helloResult ) , authenticators ) ;
149
+ return new ( new ConnectionDescription ( connection . ConnectionId , helloResult ) , authenticator ) ;
152
150
}
153
151
154
152
// private methods
@@ -165,24 +163,24 @@ private CommandWireProtocol<BsonDocument> CreateGetLastErrorProtocol(ServerApi s
165
163
return getLastErrorProtocol ;
166
164
}
167
165
168
- private BsonDocument CreateInitialHelloCommand ( IReadOnlyList < IAuthenticator > authenticators , bool loadBalanced = false , CancellationToken cancellationToken = default )
166
+ private BsonDocument CreateInitialHelloCommand ( IAuthenticator authenticator , bool loadBalanced = false , CancellationToken cancellationToken = default )
169
167
{
170
168
var command = HelloHelper . CreateCommand ( _serverApi , loadBalanced : loadBalanced ) ;
171
169
HelloHelper . AddClientDocumentToCommand ( command , _clientDocument ) ;
172
170
HelloHelper . AddCompressorsToCommand ( command , _compressors ) ;
173
- return HelloHelper . CustomizeCommand ( command , authenticators , cancellationToken ) ;
171
+ return HelloHelper . CustomizeCommand ( command , authenticator , cancellationToken ) ;
174
172
}
175
173
176
- private List < IAuthenticator > CreateAuthenticators ( IConnection connection )
174
+ private IAuthenticator CreateAuthenticator ( IConnection connection )
177
175
{
178
176
if ( connection . Description . IsInitialized ( ) )
179
177
{
180
178
// should never be here.
181
179
throw new InvalidOperationException ( ) ;
182
180
}
183
181
184
- var authenticatorFactories = connection . Settings . AuthenticatorFactories ;
185
- return authenticatorFactories . Select ( c => c . Create ( ) ) . ToList ( ) ;
182
+ var authenticatorFactory = connection . Settings . AuthenticatorFactories . SingleOrDefault ( ) ;
183
+ return authenticatorFactory ? . Create ( ) ;
186
184
}
187
185
188
186
private ConnectionDescription UpdateConnectionIdWithServerValue ( ConnectionDescription description , BsonDocument getLastErrorResult )
0 commit comments