Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NotoriousClient.Builder;
using NotoriousClient.Sender;

namespace NotoriousClient.Clients.Authentication
namespace NotoriousClient.Clients.Authentication.BasicAuth
{
/// <summary>
/// Base class for HTTP Client preconfigured with Basic Authentication.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
namespace NotoriousClient.Clients
namespace NotoriousClient.Clients.Authentication.M2M
{
public class AuthorizationServerOptions
{
/// <summary>
/// Gets or sets the unique identifier for the client.
/// </summary>
public string ClientId { get; set; }

Check warning on line 8 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'ClientId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'ClientId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Gets or sets the client secret used for authentication.
/// </summary>
public string ClientSecret { get; set; }

Check warning on line 13 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'ClientSecret' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 13 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'ClientSecret' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Gets or sets the authority information for the current context.
/// </summary>
public string Authority { get; set; }

Check warning on line 18 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Authority' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 18 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Authority' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Gets or sets the list of audiences that are allowed to access the resource.
/// </summary>
public string[] Audiences { get; set; }

Check warning on line 23 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Audiences' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Audiences' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// Gets or sets the scopes requested during the authorization process.
/// </summary>
public string[] Scopes { get; set; }

Check warning on line 28 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Scopes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 28 in NotoriousClient/Clients/Authentication/M2M/AuthorizationServerOptions.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Scopes' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NotoriousClient.Builder;
using NotoriousClient.Sender;

namespace NotoriousClient.Clients.Authentication.M2M
{
public abstract class ClientCredentialsBaseClient : BaseClient
{
private readonly ITokenClient _tokenClient;

public ClientCredentialsBaseClient(ITokenClient tokenClient, IRequestSender sender, string baseUrl) : base(sender, baseUrl)
{
_tokenClient = tokenClient;
}

protected override async Task<IRequestBuilder> GetBuilderAsync(string route, Method method = Method.Get, string? version = null)
{
string token = await _tokenClient.GetAccessToken();

return (await base.GetBuilderAsync(route, method, version)).WithAuthentication(token);
}
}
}
7 changes: 7 additions & 0 deletions NotoriousClient/Clients/Authentication/M2M/ITokenClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NotoriousClient.Clients.Authentication.M2M
{
public interface ITokenClient
{
Task<string> GetAccessToken();
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

using NotoriousClient.Builder;
using NotoriousClient.Clients.Authentication.Models;
using NotoriousClient.Clients.Authentication.M2M.Models;
using NotoriousClient.Sender;

namespace NotoriousClient.Clients.Authentication
namespace NotoriousClient.Clients.Authentication.M2M
{
public class MemoryCachedClientCredentialsBaseClient : ClientCredentialsBaseClient
public class MemoryCachedTokenClient : TokenClient
{
private const int SKEW_IN_SECONDS = 60;
private const int MinimumCacheExpiry = 1;
private readonly IMemoryCache _cache;

public MemoryCachedClientCredentialsBaseClient(IRequestSender sender, IMemoryCache cache, IOptions<AuthorizationServerOptions> server) : base(sender, server)
public MemoryCachedTokenClient(IRequestSender sender, IMemoryCache cache, IOptions<AuthorizationServerOptions> server) : base(sender, server)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}

/// <summary>
/// Get a preconfigured <see cref="IRequestBuilder"/> with Bearer Authentication using ClientCredentials OAuth flow.
/// </summary>
protected override async Task<IRequestBuilder> GetBuilderAsync(string route, Method method = Method.Get, string? version = null)
{
DiscoveryDocument? discovery = await GetDiscoveryDocument();
TokenEndpointResponse response = await GetToken(discovery);

return (await base.GetBuilderAsync(route, method, version)).WithAuthentication(response.AccessToken);
}

protected override async Task<TokenEndpointResponse> GetToken(DiscoveryDocument? discovery)

Expand All @@ -53,7 +42,7 @@
return response;
}

return cachedToken;

Check warning on line 45 in NotoriousClient/Clients/Authentication/M2M/MemoryCachedTokenClient.cs

View workflow job for this annotation

GitHub Actions / publish

Possible null reference return.

Check warning on line 45 in NotoriousClient/Clients/Authentication/M2M/MemoryCachedTokenClient.cs

View workflow job for this annotation

GitHub Actions / publish

Possible null reference return.
}

protected override async Task<DiscoveryDocument?> GetDiscoveryDocument()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Text.Json.Serialization;

namespace NotoriousClient.Clients.Authentication.Models
namespace NotoriousClient.Clients.Authentication.M2M.Models
{
public class DiscoveryDocument
{
[JsonPropertyName("token_endpoint")]
public string TokenEndpoint { get; set; }

Check warning on line 8 in NotoriousClient/Clients/Authentication/M2M/Models/DiscoveryDocument.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'TokenEndpoint' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in NotoriousClient/Clients/Authentication/M2M/Models/DiscoveryDocument.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'TokenEndpoint' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace NotoriousClient.Clients.Authentication.Models
namespace NotoriousClient.Clients.Authentication.M2M.Models
{
public class TokenEndpointResponse
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
using Microsoft.Extensions.Options;

using NotoriousClient.Builder;
using NotoriousClient.Clients.Authentication.Models;
using NotoriousClient.Clients.Authentication.M2M.Models;
using NotoriousClient.Sender;

namespace NotoriousClient.Clients.Authentication
namespace NotoriousClient.Clients.Authentication.M2M
{
public class ClientCredentialsBaseClient : BaseClient
public class TokenClient : BaseClient, ITokenClient
{
protected IOptions<AuthorizationServerOptions> AuthenticationServerOptions { get; private init; }
private readonly Endpoint DISCOVERY_ENDPOINT = new Endpoint("/.well-known/openid-configuration", Method.Get);

public ClientCredentialsBaseClient(IRequestSender sender, IOptions<AuthorizationServerOptions> server) : base(sender, server.Value.Authority)
public TokenClient(IRequestSender sender, IOptions<AuthorizationServerOptions> server) : base(sender, server.Value.Authority)
{
AuthenticationServerOptions = server ?? throw new ArgumentNullException(nameof(server));
}

protected override async Task<IRequestBuilder> GetBuilderAsync(string route, Method method = Method.Get, string? version = null)
public async Task<string> GetAccessToken()
{
string tokenEndpoint = string.Empty;
DiscoveryDocument? discovery = await GetDiscoveryDocument();
TokenEndpointResponse response = await GetToken(discovery);
DiscoveryDocument? discord = await GetDiscoveryDocument();

return (await base.GetBuilderAsync(route, method, version)).WithAuthentication(response.AccessToken);
return (await GetToken(discord)).AccessToken;
}

protected virtual async Task<TokenEndpointResponse> GetToken(DiscoveryDocument? discovery)
{
HttpRequestMessage request = new RequestBuilder(discovery.TokenEndpoint, "", Method.Post)

Check warning on line 28 in NotoriousClient/Clients/Authentication/M2M/TokenClient.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.

Check warning on line 28 in NotoriousClient/Clients/Authentication/M2M/TokenClient.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
.WithContentBody(new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
Expand Down
Loading