Skip to content

Commit

Permalink
Fixes for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
o.nadymov committed May 24, 2024
1 parent 98feaf3 commit 2023853
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Spoleto.RestClient.Tests/Spoleto.RestClient.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Spoleto.RestClient\Spoleto.RestClient.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Spoleto.RestClient.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Spoleto.RestClient.Tests
namespace Spoleto.RestClient5.Tests
{
public class Tests
{
Expand Down
33 changes: 22 additions & 11 deletions src/Spoleto.RestClient/Authentication/DynamicAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public abstract class DynamicAuthenticator : AuthenticatorBase, IDynamicAuthenti
private readonly string _tokenType;

private string? _token;
private bool _inProcess = false;


public DynamicAuthenticator(string tokenType)
Expand All @@ -22,29 +23,39 @@ public sealed override async Task Authenticate(IRestClient client, HttpRequestMe
{
if (_token == null)
{
await _semaphore.WaitAsync().ConfigureAwait(false);
try
if (!_inProcess)
{
if (_token == null)
await _semaphore.WaitAsync().ConfigureAwait(false);
try
{
// Get a new token:
_token = await GetAuthenticationToken(client, request).ConfigureAwait(false);
if (_token == null)
{
_inProcess = true;

// Get a new token:
_token = await GetAuthenticationToken(client).ConfigureAwait(false);

_inProcess = false;
}
}
finally
{
_semaphore.Release();
}
}
finally
{
_semaphore.Release();
}
}

request.Headers.Authorization = new AuthenticationHeaderValue(_tokenType, _token);
if (_token != null)
{
request.Headers.Authorization = new AuthenticationHeaderValue(_tokenType, _token);
}
}

public virtual Task<bool> IsExpired(HttpResponseMessage response) => Task.FromResult(response.StatusCode == HttpStatusCode.Unauthorized);

public void SetExpired() => _token = null;

protected abstract Task<string> GetAuthenticationToken(IRestClient client, HttpRequestMessage request);
protected abstract Task<string> GetAuthenticationToken(IRestClient client);

#region IDisposable
bool _disposed;
Expand Down
6 changes: 6 additions & 0 deletions src/Spoleto.RestClient/Client/IRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
{
public interface IRestClient : IDisposable
{
JsonRestRequest<TObj> CreateJsonRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, TObj? content = null) where TObj : class;

XmlRestRequest<TObj> CreateXmlRestRequest<TObj>(string uri, HttpMethod httpMethod = HttpMethod.Get, TObj? content = null) where TObj : class;

BinaryRestRequest CreateBinaryRestRequest(string uri, HttpMethod httpMethod = HttpMethod.Get, byte[]? content = null);

Task<TextRestResponse> ExecuteAsStringAsync(RestRequest request, CancellationToken cancellationToken = default);

Task<BinaryRestResponse> ExecuteAsBytesAsync(RestRequest request, CancellationToken cancellationToken = default);
Expand Down
2 changes: 1 addition & 1 deletion src/Spoleto.RestClient/Client/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class RestClient : IRestClient
{
private readonly HttpClient _httpClient;
private readonly IAuthenticator? _authenticator;
private readonly RestClientOptions _options;
private readonly RestClientOptions _options;//todo:
private readonly bool _disposeHttpClient;

public RestClient(HttpClient httpClient, IAuthenticator? authenticator = null, RestClientOptions? options = null, bool disposeHttpClient = false)
Expand Down

0 comments on commit 2023853

Please sign in to comment.