@@ -208,10 +208,11 @@ public class ConnectionSettings : IEquatable<ConnectionSettings>
208208 {
209209 protected Address _address = new ( "amqp://localhost:5672" ) ;
210210 protected string _virtualHost = Consts . DefaultVirtualHost ;
211+ private readonly SaslMechanism _saslMechanism = SaslMechanism . Plain ;
212+ private readonly OAuth2Options ? _oAuth2Options = null ;
211213 private readonly string _containerId = string . Empty ;
212214 private readonly uint _maxFrameSize = Consts . DefaultMaxFrameSize ;
213215 private readonly TlsSettings ? _tlsSettings ;
214- private readonly SaslMechanism _saslMechanism = SaslMechanism . Plain ;
215216 private readonly IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
216217
217218 public ConnectionSettings ( Uri uri ,
@@ -221,7 +222,7 @@ public ConnectionSettings(Uri uri,
221222 uint ? maxFrameSize = null ,
222223 TlsSettings ? tlsSettings = null ,
223224 OAuth2Options ? oAuth2Options = null )
224- : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
225+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings , oAuth2Options )
225226 {
226227 ( string ? user , string ? password ) = ProcessUserInfo ( uri ) ;
227228
@@ -233,22 +234,23 @@ public ConnectionSettings(Uri uri,
233234 throw new ArgumentOutOfRangeException ( "uri.Scheme" , "Uri scheme must be 'amqp' or 'amqps'" ) ;
234235 }
235236
236- _address = new Address ( host : uri . Host ,
237- port : uri . Port ,
238- user : user ,
239- password : password ,
240- path : "/" ,
241- scheme : scheme ) ;
237+ _address = InitAddress ( uri . Host , uri . Port , user , password , scheme ) ;
238+ _tlsSettings = InitTlsSettings ( ) ;
239+ }
242240
243- if ( oAuth2Options is not null )
241+ protected Address InitAddress ( string host , int port , string ? user , string ? password , string scheme )
242+ {
243+ if ( _oAuth2Options is not null )
244244 {
245- // in case of OAuth2, we need to use plain mechanism
246- _saslMechanism = SaslMechanism . Plain ;
247- _address = new Address ( _address . Host , _address . Port , "" , oAuth2Options . Token , _address . Path ,
248- _address . Scheme ) ;
245+ return new Address ( host , port , "" , _oAuth2Options . Token , "/" , scheme ) ;
249246 }
250247
251- _tlsSettings = InitTlsSettings ( ) ;
248+ return new Address ( host ,
249+ port : port ,
250+ user : user ,
251+ password : password ,
252+ path : "/" ,
253+ scheme : scheme ) ;
252254 }
253255
254256 public ConnectionSettings ( string scheme ,
@@ -263,33 +265,19 @@ public ConnectionSettings(string scheme,
263265 uint ? maxFrameSize = null ,
264266 TlsSettings ? tlsSettings = null ,
265267 OAuth2Options ? oAuth2Options = null )
266- : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
268+ : this ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings , oAuth2Options )
267269 {
268270 if ( false == Utils . IsValidScheme ( scheme ) )
269271 {
270272 throw new ArgumentOutOfRangeException ( nameof ( scheme ) , "scheme must be 'amqp' or 'amqps'" ) ;
271273 }
272274
273- _address = new Address ( host : host ,
274- port : port ,
275- user : user ,
276- password : password ,
277- path : "/" ,
278- scheme : scheme ) ;
279-
275+ _address = InitAddress ( host , port , user , password , scheme ) ;
280276 if ( virtualHost is not null )
281277 {
282278 _virtualHost = virtualHost ;
283279 }
284280
285- if ( oAuth2Options is not null )
286- {
287- // in case of OAuth2, we need to use plain mechanism
288- _saslMechanism = SaslMechanism . Plain ;
289- _address = new Address ( _address . Host , _address . Port , "" , oAuth2Options . Token , _address . Path ,
290- _address . Scheme ) ;
291- }
292-
293281 _tlsSettings = InitTlsSettings ( ) ;
294282 }
295283
@@ -298,7 +286,8 @@ protected ConnectionSettings(
298286 SaslMechanism ? saslMechanism = null ,
299287 IRecoveryConfiguration ? recoveryConfiguration = null ,
300288 uint ? maxFrameSize = null ,
301- TlsSettings ? tlsSettings = null )
289+ TlsSettings ? tlsSettings = null ,
290+ OAuth2Options ? oAuth2Options = null )
302291 {
303292 if ( containerId is not null )
304293 {
@@ -310,6 +299,13 @@ protected ConnectionSettings(
310299 _saslMechanism = saslMechanism ;
311300 }
312301
302+ if ( oAuth2Options is not null )
303+ {
304+ // If OAuth2Options is set, then SaslMechanism must be Plain
305+ _oAuth2Options = oAuth2Options ;
306+ _saslMechanism = SaslMechanism . Plain ;
307+ }
308+
313309 if ( recoveryConfiguration is not null )
314310 {
315311 _recoveryConfiguration = recoveryConfiguration ;
@@ -483,7 +479,7 @@ public ClusterConnectionSettings(IEnumerable<Uri> uris,
483479 uint ? maxFrameSize = null ,
484480 TlsSettings ? tlsSettings = null ,
485481 OAuth2Options ? oAuth2Options = null )
486- : base ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
482+ : base ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings , oAuth2Options )
487483 {
488484 _uris = uris . ToList ( ) ;
489485 if ( _uris . Count == 0 )
@@ -525,20 +521,7 @@ public ClusterConnectionSettings(IEnumerable<Uri> uris,
525521 }
526522 }
527523
528- var address = new Address ( host : uri . Host ,
529- port : uri . Port ,
530- user : user ,
531- password : password ,
532- path : "/" ,
533- scheme : scheme ) ;
534-
535- // if (oAuth2Options is not null)
536- // {
537- // // in case of OAuth2, we need to use plain mechanism
538- // _saslMechanism = SaslMechanism.Plain;
539- // _address = new Address(_address.Host, _address.Port, "", oAuth2Options.Token, _address.Path,
540- // _address.Scheme);
541- // }
524+ var address = InitAddress ( uri . Host , uri . Port , user , password , scheme ) ;
542525
543526 _uriToAddress [ uri ] = address ;
544527
0 commit comments