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
4 changes: 2 additions & 2 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169</NoWarn>
<NukeRootDirectory>..</NukeRootDirectory>
Expand All @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="9.0.4" />
<PackageReference Include="Nuke.Common" Version="10.1.0" />
</ItemGroup>

</Project>
8 changes: 3 additions & 5 deletions samples/Sample.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Auth0.ManagementApi;
using Auth0.ManagementApi.Models;
using Auth0.ManagementApi.Paging;
using Auth0Net.DependencyInjection;
using Auth0Net.DependencyInjection.HttpClient;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -43,7 +41,7 @@
});

// Adds the ManagementApiClient with automatic injection of the management token based on the configuration set above.
builder.Services.AddAuth0ManagementClient().AddManagementAccessToken();
builder.Services.AddAuth0ManagementClient();

builder.Services.AddGrpc();

Expand All @@ -56,9 +54,9 @@

app.MapGet("/users", async ([FromServices] IManagementApiClient client) =>
{
var user = await client.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo());
var user = await client.Users.ListAsync(new ListUsersRequestParameters() { });

return user.Select(x => new Sample.AspNetCore.User(x.UserId, x.FullName, x.Email)).ToArray();
return user.CurrentPage.Select(x => new Sample.AspNetCore.User(x.UserId, x.Name, x.Email)).ToArray();
});


Expand Down
10 changes: 3 additions & 7 deletions samples/Sample.AspNetCore/Protos/UsersService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Auth0.ManagementApi;
using Auth0.ManagementApi.Models;
using Auth0.ManagementApi.Paging;
using Auth0.ManagementApi;
using Grpc.Core;
using User;

Expand All @@ -18,13 +14,13 @@ public UsersService(IManagementApiClient client)

public override async Task<UserResponse> GetUser(UserRequest request, ServerCallContext context)
{
var users = await _client.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo());
var users = await _client.Users.ListAsync(new ListUsersRequestParameters() { PerPage = 10});

return new UserResponse
{
Users =
{
users.Select(x=> new UserDto {UserId = x.UserId, Email = x.Email, FullName = x.FullName})
users.CurrentPage.Select(x=> new UserDto {UserId = x.UserId, Email = x.Email, FullName = x.Name})
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion samples/Sample.AspNetCore/Sample.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<UserSecretsId>ed89c14f-ea75-4b37-83e7-078870067ffc</UserSecretsId>
<nullable>enable</nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<nullable>enable</nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>096fd757-814d-4d2a-a02e-ef4f19cdc656</UserSecretsId>
Expand All @@ -15,7 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
56 changes: 17 additions & 39 deletions src/Auth0Net.DependencyInjection/Auth0Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Auth0.AuthenticationApi;
using Auth0.ManagementApi;
using Auth0Net.DependencyInjection.Cache;
using Auth0Net.DependencyInjection.Factory;
using Auth0Net.DependencyInjection.HttpClient;
using Auth0Net.DependencyInjection.Injectables;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using IHttpClientFactory = System.Net.Http.IHttpClientFactory;

namespace Auth0Net.DependencyInjection;

Expand All @@ -14,8 +13,6 @@ namespace Auth0Net.DependencyInjection;
/// </summary>
public static class Auth0Extensions
{

private const string ManagementHttpClientKey = nameof(ManagementHttpClientKey);

/// <summary>
/// Adds an <see cref="AuthenticationApiClient" /> integrated with <see cref="IHttpClientBuilder" />.
Expand All @@ -26,7 +23,7 @@ public static class Auth0Extensions
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="domain">The root domain for your Auth0 tenant.</param>
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="HttpClientAuthenticationConnection"/>.</returns>
public static IHttpClientBuilder AddAuth0AuthenticationClientCore(this IServiceCollection services, string domain)
public static IHttpClientBuilder AddAuth0AuthenticationClient(this IServiceCollection services, string domain)
{
if (services.Any(x => x.ServiceType == typeof(IAuthenticationApiClient)))
throw new InvalidOperationException("AuthenticationApiClient has already been registered!");
Expand Down Expand Up @@ -105,28 +102,18 @@ private static IHttpClientBuilder AddAuth0AuthenticationClientInternal(this ISer
;

}




/// <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(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>
public static IHttpClientBuilder AddAuth0ManagementClient(this IServiceCollection services)
/// <returns>An <see cref="IHttpClientBuilder" /> that can be used to configure the <see cref="ManagementClient"/>.</returns>
public static IHttpClientBuilder AddAuth0ManagementClient(this IServiceCollection services, Action<Auth0ManagementClientConfiguration, IServiceProvider>? config = null)
{
services.AddSingleton<IManagementApiClient, InjectableManagementApiClient>();

services.AddSingleton<IManagementConnection, HttpClientManagementConnection>(x =>
{
var factory = x.GetRequiredService<IHttpClientFactory>();
return new HttpClientManagementConnection(factory.CreateClient(ManagementHttpClientKey));
});

return services.AddHttpClient(ManagementHttpClientKey)
var httpClientBuilder = services.AddHttpClient(ManagementClientFactory.Auth0ManagementApiClient)
#if NET8_0
.ConfigurePrimaryHttpMessageHandler(() =>
new SocketsHttpHandler()
Expand All @@ -136,6 +123,17 @@ public static IHttpClientBuilder AddAuth0ManagementClient(this IServiceCollectio
.SetHandlerLifetime(Timeout.InfiniteTimeSpan)
#endif
;

var optionsBuilder = services.AddOptions<Auth0ManagementClientConfiguration>();

if(config != null)
optionsBuilder.Configure(config);

services.AddSingleton<ManagementClientFactory>();

services.AddSingleton<IManagementApiClient>(sp => sp.GetRequiredService<ManagementClientFactory>().Create());

return httpClientBuilder;
}

/// <summary>
Expand All @@ -155,24 +153,4 @@ public static IHttpClientBuilder AddAccessToken(this IHttpClientBuilder builder,
return builder.AddHttpMessageHandler(provider =>
new Auth0TokenHandler(provider.GetRequiredService<IAuth0TokenCache>(), c));
}

/// <summary>
/// Adds a <see cref="System.Net.Http.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(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 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)
{
var c = new Auth0ManagementTokenConfiguration();
config?.Invoke(c);

return builder.AddHttpMessageHandler(p =>
new Auth0ManagementTokenHandler(
p.GetRequiredService<IAuth0TokenCache>(),
p.GetRequiredService<IOptions<Auth0Configuration>>(), c));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net8.0;net9.0;net10.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>5.2.1</Version>
<Version>6.0.0</Version>
<Authors>Hawxy</Authors>
<Description>Dependency Injection, HttpClientFactory &amp; ASP.NET Core extensions for Auth0.NET</Description>
<LangVersion>latest</LangVersion>
Expand All @@ -22,37 +22,31 @@

<ItemGroup>
<PackageReference Include="Auth0.AuthenticationApi" Version="7.45.1" />
<PackageReference Include="Auth0.ManagementApi" Version="7.46.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.39" PrivateAssets="All" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.4.0" />
<PackageReference Include="Auth0.ManagementApi" Version="8.0.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="2.0.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.5" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.6.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.1" />
<PackageReference Include="Polyfill" Version="9.1.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.11" />
<PackageReference Include="Polyfill" Version="9.23.0" PrivateAssets="all" />
</ItemGroup>

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

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

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

<ItemGroup>
Expand Down
49 changes: 0 additions & 49 deletions src/Auth0Net.DependencyInjection/Auth0ResilienceExtensions.cs

This file was deleted.

29 changes: 24 additions & 5 deletions src/Auth0Net.DependencyInjection/Cache/Auth0Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace Auth0Net.DependencyInjection.Cache;
using ZiggyCreatures.Caching.Fusion;

namespace Auth0Net.DependencyInjection.Cache;

/// <summary>
/// Configuration for the Auth0 Clients and Auth0 Token Cache.
/// </summary>
public sealed class Auth0Configuration
{
/// <summary>
/// The default or custom root domain for your Auth0 tenant.
/// The default or custom root domain for your Auth0 tenant.
/// </summary>
public string Domain { get; set; } = null!;
/// <summary>
Expand All @@ -17,16 +19,33 @@ public sealed class Auth0Configuration
/// The Client Secret of the Auth0 Machine-to-Machine application.
/// </summary>
public string? ClientSecret { get; set; }

/// <summary>
/// This package uses FusionCache internally for token caching.
/// If you have an existing FusionCache configuration you'd like to use (ie one setup with a distributed cache), pass the name of it here.
/// For the default FusionCache instance registered with ".AddFusionCache()", use <see cref="FusionCacheOptions.DefaultCacheName"/>
/// </summary>
public string? FusionCacheInstance { get; set; }

}

/// <summary>
/// Configuration for the Auth0 Management API.
/// </summary>
public sealed class Auth0ManagementTokenConfiguration
public sealed class Auth0ManagementClientConfiguration
{
/// <summary>
/// This option will replace the use of <see cref="Auth0Configuration.Domain"/> to compute the audience for the Management API token.
/// Useful when using custom domains.
/// Sets the audience for the Management API token. This is your default Auth0 domain within your tenant and should be set if you're using a custom domain.
/// </summary>
public string? Audience { get; set; }

/// <summary>
/// Sets the max number of retries for the management client. Default is 2.
/// </summary>
public int? MaxRetries { get; set; }

/// <summary>
/// Sets the timeout for the management client. Default is 30 seconds.
/// </summary>
public TimeSpan? Timeout { get; set; }
}
Loading
Loading