Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.1.0 #22

Merged
merged 3 commits into from
Dec 21, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<img align="center" src="https://user-images.githubusercontent.com/975824/128343470-8d97e39d-ff8a-4daf-8ebf-f9039a46abd6.png" height="130px" />
</h1>

Integrating [Auth0.NET](https://github.com/auth0/auth0.net) into your project whilst attempting to follow idiomatic .NET Core conventions can be cumbersome and involve a sizable amount of boilerplate shared between projects.
Integrating [Auth0.NET](https://github.com/auth0/auth0.net) into your project whilst following idiomatic .NET conventions can be cumbersome and involve a sizable amount of boilerplate shared between projects.

This library hopes to solve that problem, featuring:

Expand All @@ -18,7 +18,7 @@ This library hopes to solve that problem, featuring:

:white_check_mark: `IHttpClientBuilder` extensions, providing handlers to automatically append access tokens to outgoing requests.

This library supports .NET 6 & .NET 7, and is suitable for use in ASP.NET Core and standalone .NET Generic Host applications.
This library supports .NET 6+, and is suitable for use in ASP.NET Core and standalone .NET Generic Host applications.

## Install

Expand Down
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="7.0.2" />
<PackageReference Include="Nuke.Common" Version="7.0.6" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions samples/Sample.AspNetCore/Sample.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>ed89c14f-ea75-4b37-83e7-078870067ffc</UserSecretsId>
<nullable>enable</nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.54.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.7" />
<PackageReference Include="Grpc.AspNetCore" Version="2.59.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<nullable>enable</nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>096fd757-814d-4d2a-a02e-ef4f19cdc656</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.54.0" />
<PackageReference Include="Grpc.Tools" Version="2.54.0">
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.59.0" />
<PackageReference Include="Grpc.Tools" Version="2.60.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
52 changes: 39 additions & 13 deletions src/Auth0Net.DependencyInjection/Auth0Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ public static IHttpClientBuilder AddAuth0AuthenticationClientCore(this IServiceC
.Configure(x => x.Domain = domain)
.Validate(x => !string.IsNullOrWhiteSpace(x.Domain), "Auth0 Domain cannot be null or empty");

services.AddSingleton<IAuthenticationApiClient, InjectableAuthenticationApiClient>();
return services.AddHttpClient<IAuthenticationConnection, HttpClientAuthenticationConnection>()
.ConfigurePrimaryHttpMessageHandler(() =>
new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(2)
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
return services.AddAuth0AuthenticationClientInternal();
}

/// <summary>
Expand All @@ -60,10 +53,41 @@ public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceColle
.Configure(config)
.Validate(x => !string.IsNullOrWhiteSpace(x.ClientId) && !string.IsNullOrWhiteSpace(x.Domain) && !string.IsNullOrWhiteSpace(x.ClientSecret),
"Auth0 Configuration cannot have empty values");

return services.AddAuth0AuthenticationClientInternal(true);
}


/// <summary>
/// Adds a <see cref="AuthenticationApiClient" /> integrated with <see cref="IHttpClientBuilder" /> as well as the <see cref="IAuth0TokenCache" /> and related services to the <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// This configuration is required to use the <see cref="IHttpClientBuilder"/> and token caching integration.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="config">A delegate that is used to configure the instance of <see cref="Auth0Configuration" />, with the ability to request services from the <see cref="IServiceProvider"/></param>
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClientAuthenticationConnection"/>.</returns>
public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceCollection services, Action<Auth0Configuration, IServiceProvider> config)
{
if(services.Any(x=> x.ServiceType == typeof(IAuthenticationApiClient)))
throw new InvalidOperationException("AuthenticationApiClient has already been registered!");

services.AddFusionCache(Constants.FusionCacheInstance);
services.AddOptions<Auth0Configuration>()
.Configure(config)
.Validate(x => !string.IsNullOrWhiteSpace(x.ClientId) && !string.IsNullOrWhiteSpace(x.Domain) && !string.IsNullOrWhiteSpace(x.ClientSecret),
"Auth0 Configuration cannot have empty values");

return services.AddAuth0AuthenticationClientInternal(true);
}

services.AddSingleton<IAuth0TokenCache, Auth0TokenCache>();
private static IHttpClientBuilder AddAuth0AuthenticationClientInternal(this IServiceCollection services,
bool withCache = false)
{
if (withCache)
{
services.AddFusionCache(Constants.FusionCacheInstance);
services.AddSingleton<IAuth0TokenCache, Auth0TokenCache>();
}

services.AddSingleton<IAuthenticationApiClient, InjectableAuthenticationApiClient>();
return services.AddHttpClient<IAuthenticationConnection, HttpClientAuthenticationConnection>()
Expand All @@ -74,12 +98,14 @@ public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceColle
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
}



/// <summary>
/// Adds a <see cref="ManagementApiClient" /> integrated with <see cref="IHttpClientBuilder" /> to the <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// The domain used to construct the Management connection is the same as set in <see cref="AddAuth0AuthenticationClient"/>.
/// The domain used to construct the Management connection is the same as set in <see cref="AddAuth0AuthenticationClient(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Auth0Net.DependencyInjection.Cache.Auth0Configuration})"/>.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClientManagementConnection"/>.</returns>
Expand Down Expand Up @@ -118,10 +144,10 @@ public static IHttpClientBuilder AddAccessToken(this IHttpClientBuilder builder,
/// Adds a <see cref="DelegatingHandler"/> to the <see cref="IHttpClientBuilder"/> that will automatically add a Auth0 Management Access Token token to the Authorization header.
/// </summary>
/// <remarks>
/// The domain used to resolve the token is the same as set in <see cref="AddAuth0AuthenticationClient"/>.
/// The domain used to resolve the token is the same as set in <see cref="AddAuth0AuthenticationClient(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Auth0Net.DependencyInjection.Cache.Auth0Configuration})"/>, unless overriden.
/// </remarks>
/// <param name="builder">The <see cref="IHttpClientBuilder"/> you wish to configure.</param>
/// <param name="config">Additional configuration for the management client.</param>
/// <param name="config">Additional configuration for the management client for custom domain scenarios.</param>
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClient"/>.</returns>
public static IHttpClientBuilder AddManagementAccessToken(this IHttpClientBuilder builder, Action<Auth0ManagementTokenConfiguration>? config = null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<Authors>Hawxy</Authors>
<Description>Dependency Injection, HttpClientFactory &amp; ASP.NET Core extensions for Auth0.NET</Description>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand All @@ -20,10 +20,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Auth0.AuthenticationApi" Version="7.20.0" />
<PackageReference Include="Auth0.ManagementApi" Version="7.20.0" />
<PackageReference Include="Auth0.AuthenticationApi" Version="7.25.1" />
<PackageReference Include="Auth0.ManagementApi" Version="7.25.1" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.21.0" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.24.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
Expand All @@ -38,6 +38,12 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>

<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="7.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="FakeItEasy" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
29 changes: 29 additions & 0 deletions tests/Auth0Net.DependencyInjection.Tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ public void AddAuth0AuthenticationClient_Resolves_AuthenticationClient()
Assert.Equal(clientSecret, configuration.Value.ClientSecret);
}

public sealed record FakeConfiguration(string Domain, string ClientId, string ClientSecret);

[Fact]
public void AddAuth0AuthenticationClient_WithServiceCollection_CanBeResolved()
{
var domain = "test.au.auth0.com";
var clientId = "fake-id";
var clientSecret = "fake-secret";

var services = new ServiceCollection();

services.AddSingleton(new FakeConfiguration(domain, clientId, clientSecret));

services.AddAuth0AuthenticationClient((x, p) =>
{
var config = p.GetRequiredService<FakeConfiguration>();

x.Domain = config.Domain;
x.ClientId = config.ClientId;
x.ClientSecret = config.ClientSecret;
});

var collection = services.BuildServiceProvider();

var authenticationClient = collection.GetService<IAuthenticationApiClient>();
Assert.NotNull(authenticationClient);
Assert.IsType<InjectableAuthenticationApiClient>(authenticationClient);
}

[Fact]
public void AddAccessToken_Rejects_InvalidConfig()
{
Expand Down