Skip to content

Commit

Permalink
Move read URL credentials into NatsOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Nov 4, 2024
1 parent 63b3bf2 commit a9f9d49
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
43 changes: 1 addition & 42 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public NatsConnection()
public NatsConnection(NatsOpts opts)
{
_logger = opts.LoggerFactory.CreateLogger<NatsConnection>();
Opts = ReadUserInfoFromConnectionString(opts);
Opts = opts.ReadUserInfoFromConnectionString();
ConnectionState = NatsConnectionState.Closed;
_waitForOpenConnection = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_disposedCancellationTokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -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");
Expand Down
39 changes: 39 additions & 0 deletions src/NATS.Client.Core/NatsOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
};
}
}
}

0 comments on commit a9f9d49

Please sign in to comment.