@@ -41,6 +41,7 @@ public class ConnectionSettingsBuilder
4141 private Uri ? _uri ;
4242 private List < Uri > ? _uris ;
4343 private IUriSelector ? _uriSelector ;
44+ private OAuth2Options ? _oAuth2Options ;
4445
4546 public static ConnectionSettingsBuilder Create ( )
4647 {
@@ -104,6 +105,7 @@ public ConnectionSettingsBuilder MaxFrameSize(uint maxFrameSize)
104105 throw new ArgumentOutOfRangeException ( nameof ( maxFrameSize ) ,
105106 "maxFrameSize must be 0 (no limit) or greater than or equal to 512" ) ;
106107 }
108+
107109 return this ;
108110 }
109111
@@ -152,6 +154,13 @@ public ConnectionSettingsBuilder UriSelector(IUriSelector uriSelector)
152154 return this ;
153155 }
154156
157+ public ConnectionSettingsBuilder OAuth2Options ( OAuth2Options ? oAuth2Options = null )
158+ {
159+ _oAuth2Options = oAuth2Options ;
160+ return this ;
161+
162+ }
163+
155164 public ConnectionSettings Build ( )
156165 {
157166 // TODO this should do something similar to consolidate in the Java code
@@ -205,6 +214,7 @@ public class ConnectionSettings : IEquatable<ConnectionSettings>
205214 private readonly TlsSettings ? _tlsSettings ;
206215 private readonly SaslMechanism _saslMechanism = SaslMechanism . Plain ;
207216 private readonly IRecoveryConfiguration _recoveryConfiguration = new RecoveryConfiguration ( ) ;
217+ private readonly OAuth2Options ? _oAuth2Options = null ;
208218
209219 public ConnectionSettings ( Uri uri ,
210220 string ? containerId = null ,
@@ -272,7 +282,8 @@ protected ConnectionSettings(
272282 SaslMechanism ? saslMechanism = null ,
273283 IRecoveryConfiguration ? recoveryConfiguration = null ,
274284 uint ? maxFrameSize = null ,
275- TlsSettings ? tlsSettings = null )
285+ TlsSettings ? tlsSettings = null ,
286+ OAuth2Options ? oAuth2Options = null )
276287 {
277288 if ( containerId is not null )
278289 {
@@ -289,6 +300,15 @@ protected ConnectionSettings(
289300 _recoveryConfiguration = recoveryConfiguration ;
290301 }
291302
303+ _oAuth2Options = oAuth2Options ;
304+ if ( _oAuth2Options is not null )
305+ {
306+ // in case of OAuth2, we need to use plain mechanism
307+ _saslMechanism = SaslMechanism . Plain ;
308+ _address = new Address ( _address . Host , _address . Port , _address . User , _oAuth2Options . Token , _address . Path ,
309+ _address . Scheme ) ;
310+ }
311+
292312 if ( maxFrameSize is not null )
293313 {
294314 _maxFrameSize = ( uint ) maxFrameSize ;
@@ -408,7 +428,8 @@ protected static string ProcessUriSegmentsForVirtualHost(Uri uri)
408428 // that has at least the path segment "/"
409429 if ( uri . Segments . Length > 2 )
410430 {
411- throw new ArgumentException ( $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } ") ;
431+ throw new ArgumentException (
432+ $ "Multiple segments in path of AMQP URI: { string . Join ( ", " , uri . Segments ) } ") ;
412433 }
413434
414435 if ( uri . Segments . Length == 2 )
@@ -454,16 +475,17 @@ public ClusterConnectionSettings(IEnumerable<Uri> uris,
454475 SaslMechanism ? saslMechanism = null ,
455476 IRecoveryConfiguration ? recoveryConfiguration = null ,
456477 uint ? maxFrameSize = null ,
457- TlsSettings ? tlsSettings = null )
458- : base ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings )
478+ TlsSettings ? tlsSettings = null ,
479+ OAuth2Options ? oAuth2Options = null )
480+ : base ( containerId , saslMechanism , recoveryConfiguration , maxFrameSize , tlsSettings , oAuth2Options )
459481 {
460482 _uris = uris . ToList ( ) ;
461483 if ( _uris . Count == 0 )
462484 {
463485 throw new ArgumentOutOfRangeException ( nameof ( uris ) , "At least one Uri is required." ) ;
464486 }
465487
466- _uriToAddress = new ( _uris . Count ) ;
488+ _uriToAddress = new Dictionary < Uri , Address > ( _uris . Count ) ;
467489
468490 if ( uriSelector is not null )
469491 {
@@ -492,7 +514,8 @@ public ClusterConnectionSettings(IEnumerable<Uri> uris,
492514 string thisVirtualHost = ProcessUriSegmentsForVirtualHost ( uri ) ;
493515 if ( false == thisVirtualHost . Equals ( tmpVirtualHost , StringComparison . InvariantCultureIgnoreCase ) )
494516 {
495- throw new ArgumentException ( $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
517+ throw new ArgumentException (
518+ $ "All AMQP URIs must use the same virtual host. Expected '{ tmpVirtualHost } ', got '{ thisVirtualHost } '") ;
496519 }
497520 }
498521
@@ -551,6 +574,7 @@ public override int GetHashCode()
551574 {
552575 hashCode ^= _uris [ i ] . GetHashCode ( ) ;
553576 }
577+
554578 return hashCode ;
555579 }
556580
@@ -600,4 +624,14 @@ private bool trustEverythingCertValidationCallback(object sender, X509Certificat
600624 return ( sslPolicyErrors & ~ AcceptablePolicyErrors ) == SslPolicyErrors . None ;
601625 }
602626 }
627+
628+ public class OAuth2Options
629+ {
630+ public OAuth2Options ( string token )
631+ {
632+ Token = token ;
633+ }
634+
635+ public string Token { get ; set ; }
636+ }
603637}
0 commit comments