diff --git a/src/NATS.Client.Core/NatsConnection.cs b/src/NATS.Client.Core/NatsConnection.cs index a596fa32..0601ea6d 100644 --- a/src/NATS.Client.Core/NatsConnection.cs +++ b/src/NATS.Client.Core/NatsConnection.cs @@ -75,7 +75,7 @@ public NatsConnection() public NatsConnection(NatsOpts opts) { _logger = opts.LoggerFactory.CreateLogger(); - Opts = ReadUserInfoFromConnectionString(opts); + Opts = opts.ReadUserInfoFromConnectionString(); ConnectionState = NatsConnectionState.Closed; _waitForOpenConnection = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); _disposedCancellationTokenSource = new CancellationTokenSource(); @@ -288,47 +288,6 @@ internal ValueTask UnsubscribeAsync(int sid) return default; } - private static NatsOpts ReadUserInfoFromConnectionString(NatsOpts opts) - { - // Setting credentials in options takes precedence over URL credentials - if (opts.AuthOpts.Username is { Length: > 0 } || opts.AuthOpts.Password is { Length: > 0 } || opts.AuthOpts.Token is { Length: > 0 }) - { - return opts; - } - - var natsUri = opts.GetSeedUris(suppressRandomization: true).First(); - var uriBuilder = new UriBuilder(natsUri.Uri); - - if (uriBuilder.UserName is not { Length: > 0 }) - { - return opts; - } - - if (uriBuilder.Password is { Length: > 0 }) - { - opts = opts with - { - AuthOpts = opts.AuthOpts with - { - Username = Uri.UnescapeDataString(uriBuilder.UserName), - Password = Uri.UnescapeDataString(uriBuilder.Password), - }, - }; - } - else - { - opts = opts with - { - AuthOpts = opts.AuthOpts with - { - Token = Uri.UnescapeDataString(uriBuilder.UserName), - }, - }; - } - - return opts; - } - private async ValueTask InitialConnectAsync() { Debug.Assert(ConnectionState == NatsConnectionState.Connecting, "Connection state"); diff --git a/src/NATS.Client.Core/NatsOpts.cs b/src/NATS.Client.Core/NatsOpts.cs index f4213ccc..5dc7b208 100644 --- a/src/NATS.Client.Core/NatsOpts.cs +++ b/src/NATS.Client.Core/NatsOpts.cs @@ -146,4 +146,43 @@ internal NatsUri[] GetSeedUris(bool suppressRandomization = false) ? urls.Select(x => new NatsUri(x, true)).Distinct().ToArray() : urls.Select(x => new NatsUri(x, true)).OrderBy(_ => Guid.NewGuid()).Distinct().ToArray(); } + + internal NatsOpts ReadUserInfoFromConnectionString() + { + // Setting credentials in options takes precedence over URL credentials + if (AuthOpts.Username is { Length: > 0 } || AuthOpts.Password is { Length: > 0 } || AuthOpts.Token is { Length: > 0 }) + { + return this; + } + + var natsUri = GetSeedUris(suppressRandomization: true).First(); + var uriBuilder = new UriBuilder(natsUri.Uri); + + if (uriBuilder.UserName is not { Length: > 0 }) + { + return this; + } + + if (uriBuilder.Password is { Length: > 0 }) + { + return this with + { + AuthOpts = AuthOpts with + { + Username = Uri.UnescapeDataString(uriBuilder.UserName), + Password = Uri.UnescapeDataString(uriBuilder.Password), + }, + }; + } + else + { + return this with + { + AuthOpts = AuthOpts with + { + Token = Uri.UnescapeDataString(uriBuilder.UserName), + }, + }; + } + } }