Skip to content

Commit 68d3c4b

Browse files
authored
v3.1.0 (#22)
* WIP * Update sample projects * Docs tweak
1 parent a93f2ff commit 68d3c4b

8 files changed

Lines changed: 95 additions & 35 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<img align="center" src="https://user-images.githubusercontent.com/975824/128343470-8d97e39d-ff8a-4daf-8ebf-f9039a46abd6.png" height="130px" />
77
</h1>
88

9-
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.
9+
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.
1010

1111
This library hopes to solve that problem, featuring:
1212

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

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

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

2323
## Install
2424

build/_build.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Nuke.Common" Version="7.0.2" />
14+
<PackageReference Include="Nuke.Common" Version="7.0.6" />
1515
</ItemGroup>
1616

1717
</Project>

samples/Sample.AspNetCore/Sample.AspNetCore.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<UserSecretsId>ed89c14f-ea75-4b37-83e7-078870067ffc</UserSecretsId>
66
<nullable>enable</nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Grpc.AspNetCore" Version="2.54.0" />
12-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.7" />
11+
<PackageReference Include="Grpc.AspNetCore" Version="2.59.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<nullable>enable</nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<UserSecretsId>096fd757-814d-4d2a-a02e-ef4f19cdc656</UserSecretsId>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
13-
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.54.0" />
14-
<PackageReference Include="Grpc.Tools" Version="2.54.0">
12+
<PackageReference Include="Google.Protobuf" Version="3.25.1" />
13+
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.59.0" />
14+
<PackageReference Include="Grpc.Tools" Version="2.60.0">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
18+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

src/Auth0Net.DependencyInjection/Auth0Extensions.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ public static IHttpClientBuilder AddAuth0AuthenticationClientCore(this IServiceC
3232
.Configure(x => x.Domain = domain)
3333
.Validate(x => !string.IsNullOrWhiteSpace(x.Domain), "Auth0 Domain cannot be null or empty");
3434

35-
services.AddSingleton<IAuthenticationApiClient, InjectableAuthenticationApiClient>();
36-
return services.AddHttpClient<IAuthenticationConnection, HttpClientAuthenticationConnection>()
37-
.ConfigurePrimaryHttpMessageHandler(() =>
38-
new SocketsHttpHandler()
39-
{
40-
PooledConnectionLifetime = TimeSpan.FromMinutes(2)
41-
})
42-
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
35+
return services.AddAuth0AuthenticationClientInternal();
4336
}
4437

4538
/// <summary>
@@ -60,10 +53,41 @@ public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceColle
6053
.Configure(config)
6154
.Validate(x => !string.IsNullOrWhiteSpace(x.ClientId) && !string.IsNullOrWhiteSpace(x.Domain) && !string.IsNullOrWhiteSpace(x.ClientSecret),
6255
"Auth0 Configuration cannot have empty values");
56+
57+
return services.AddAuth0AuthenticationClientInternal(true);
58+
}
59+
60+
61+
/// <summary>
62+
/// 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" />.
63+
/// </summary>
64+
/// <remarks>
65+
/// This configuration is required to use the <see cref="IHttpClientBuilder"/> and token caching integration.
66+
/// </remarks>
67+
/// <param name="services">The <see cref="IServiceCollection" />.</param>
68+
/// <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>
69+
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClientAuthenticationConnection"/>.</returns>
70+
public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceCollection services, Action<Auth0Configuration, IServiceProvider> config)
71+
{
72+
if(services.Any(x=> x.ServiceType == typeof(IAuthenticationApiClient)))
73+
throw new InvalidOperationException("AuthenticationApiClient has already been registered!");
6374

64-
services.AddFusionCache(Constants.FusionCacheInstance);
75+
services.AddOptions<Auth0Configuration>()
76+
.Configure(config)
77+
.Validate(x => !string.IsNullOrWhiteSpace(x.ClientId) && !string.IsNullOrWhiteSpace(x.Domain) && !string.IsNullOrWhiteSpace(x.ClientSecret),
78+
"Auth0 Configuration cannot have empty values");
79+
80+
return services.AddAuth0AuthenticationClientInternal(true);
81+
}
6582

66-
services.AddSingleton<IAuth0TokenCache, Auth0TokenCache>();
83+
private static IHttpClientBuilder AddAuth0AuthenticationClientInternal(this IServiceCollection services,
84+
bool withCache = false)
85+
{
86+
if (withCache)
87+
{
88+
services.AddFusionCache(Constants.FusionCacheInstance);
89+
services.AddSingleton<IAuth0TokenCache, Auth0TokenCache>();
90+
}
6791

6892
services.AddSingleton<IAuthenticationApiClient, InjectableAuthenticationApiClient>();
6993
return services.AddHttpClient<IAuthenticationConnection, HttpClientAuthenticationConnection>()
@@ -74,12 +98,14 @@ public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceColle
7498
})
7599
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
76100
}
101+
102+
77103

78104
/// <summary>
79105
/// Adds a <see cref="ManagementApiClient" /> integrated with <see cref="IHttpClientBuilder" /> to the <see cref="IServiceCollection" />.
80106
/// </summary>
81107
/// <remarks>
82-
/// The domain used to construct the Management connection is the same as set in <see cref="AddAuth0AuthenticationClient"/>.
108+
/// 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})"/>.
83109
/// </remarks>
84110
/// <param name="services">The <see cref="IServiceCollection" />.</param>
85111
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClientManagementConnection"/>.</returns>
@@ -118,10 +144,10 @@ public static IHttpClientBuilder AddAccessToken(this IHttpClientBuilder builder,
118144
/// Adds a <see cref="DelegatingHandler"/> to the <see cref="IHttpClientBuilder"/> that will automatically add a Auth0 Management Access Token token to the Authorization header.
119145
/// </summary>
120146
/// <remarks>
121-
/// The domain used to resolve the token is the same as set in <see cref="AddAuth0AuthenticationClient"/>.
147+
/// 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.
122148
/// </remarks>
123149
/// <param name="builder">The <see cref="IHttpClientBuilder"/> you wish to configure.</param>
124-
/// <param name="config">Additional configuration for the management client.</param>
150+
/// <param name="config">Additional configuration for the management client for custom domain scenarios.</param>
125151
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClient"/>.</returns>
126152
public static IHttpClientBuilder AddManagementAccessToken(this IHttpClientBuilder builder, Action<Auth0ManagementTokenConfiguration>? config = null)
127153
{

src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
8-
<Version>3.0.0</Version>
8+
<Version>3.1.0</Version>
99
<Authors>Hawxy</Authors>
1010
<Description>Dependency Injection, HttpClientFactory &amp; ASP.NET Core extensions for Auth0.NET</Description>
1111
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -20,10 +20,10 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="Auth0.AuthenticationApi" Version="7.20.0" />
24-
<PackageReference Include="Auth0.ManagementApi" Version="7.20.0" />
23+
<PackageReference Include="Auth0.AuthenticationApi" Version="7.25.1" />
24+
<PackageReference Include="Auth0.ManagementApi" Version="7.25.1" />
2525
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All"/>
26-
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.21.0" />
26+
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.24.0" />
2727
</ItemGroup>
2828

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

41+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
42+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
43+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
44+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
45+
</ItemGroup>
46+
4147
<ItemGroup>
4248
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
4349
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>

tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
5-
4+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

98
<ItemGroup>
10-
<PackageReference Include="FakeItEasy" Version="7.4.0" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
12-
<PackageReference Include="xunit" Version="2.4.2" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
9+
<PackageReference Include="FakeItEasy" Version="8.0.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
11+
<PackageReference Include="xunit" Version="2.6.3" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
1413
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1514
<PrivateAssets>all</PrivateAssets>
1615
</PackageReference>

tests/Auth0Net.DependencyInjection.Tests/ExtensionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@ public void AddAuth0AuthenticationClient_Resolves_AuthenticationClient()
128128
Assert.Equal(clientSecret, configuration.Value.ClientSecret);
129129
}
130130

131+
public sealed record FakeConfiguration(string Domain, string ClientId, string ClientSecret);
132+
133+
[Fact]
134+
public void AddAuth0AuthenticationClient_WithServiceCollection_CanBeResolved()
135+
{
136+
var domain = "test.au.auth0.com";
137+
var clientId = "fake-id";
138+
var clientSecret = "fake-secret";
139+
140+
var services = new ServiceCollection();
141+
142+
services.AddSingleton(new FakeConfiguration(domain, clientId, clientSecret));
143+
144+
services.AddAuth0AuthenticationClient((x, p) =>
145+
{
146+
var config = p.GetRequiredService<FakeConfiguration>();
147+
148+
x.Domain = config.Domain;
149+
x.ClientId = config.ClientId;
150+
x.ClientSecret = config.ClientSecret;
151+
});
152+
153+
var collection = services.BuildServiceProvider();
154+
155+
var authenticationClient = collection.GetService<IAuthenticationApiClient>();
156+
Assert.NotNull(authenticationClient);
157+
Assert.IsType<InjectableAuthenticationApiClient>(authenticationClient);
158+
}
159+
131160
[Fact]
132161
public void AddAccessToken_Rejects_InvalidConfig()
133162
{

0 commit comments

Comments
 (0)