From d25507f54e91c32e10f85166c0035ee0b5f138a6 Mon Sep 17 00:00:00 2001 From: JT Date: Tue, 9 Apr 2024 20:07:28 +0800 Subject: [PATCH] v1.2 (#15) --- Package.Build.props | 2 +- README.md | 2 + build/_build.csproj | 4 +- build/_build.csproj.DotSettings | 5 +- .../FineGrainedAuthorizationHandler.cs | 3 +- .../Auth0FgaConnectionBuilder.cs | 46 +++++++++++++++---- .../Fga.Net.DependencyInjection.csproj | 2 +- tests/Fga.Net.Tests/Fga.Net.Tests.csproj | 6 +-- tests/Fga.Net.Tests/Unit/ExtensionTests.cs | 3 +- 9 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Package.Build.props b/Package.Build.props index ed84296..d6bf39f 100644 --- a/Package.Build.props +++ b/Package.Build.props @@ -1,6 +1,6 @@ - 1.1.0 + 1.2.0 Hawxy true Apache-2.0 diff --git a/README.md b/README.md index e15a21e..066d1e1 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ builder.Services.AddOpenFgaClient(config => { config.ConfigureAuth0Fga(x => { + // Change to EU/AUS depending on where your store is located + x.SetEnvironment(FgaEnvironment.US); x.WithAuthentication(builder.Configuration["Auth0Fga:ClientId"]!, builder.Configuration["Auth0Fga:ClientSecret"]!); }); diff --git a/build/_build.csproj b/build/_build.csproj index 1b97ce9..c72030f 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 CS0649;CS0169 .. @@ -11,7 +11,7 @@ - + diff --git a/build/_build.csproj.DotSettings b/build/_build.csproj.DotSettings index 7bc2848..337271d 100644 --- a/build/_build.csproj.DotSettings +++ b/build/_build.csproj.DotSettings @@ -16,6 +16,8 @@ False <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> True True True @@ -24,4 +26,5 @@ True True True - True + True + True diff --git a/src/Fga.Net.AspNetCore/Authorization/FineGrainedAuthorizationHandler.cs b/src/Fga.Net.AspNetCore/Authorization/FineGrainedAuthorizationHandler.cs index 4c69eb9..e7b228a 100644 --- a/src/Fga.Net.AspNetCore/Authorization/FineGrainedAuthorizationHandler.cs +++ b/src/Fga.Net.AspNetCore/Authorization/FineGrainedAuthorizationHandler.cs @@ -42,7 +42,8 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext var endpoint = httpContext.GetEndpoint(); if (endpoint is null) - return; + throw new InvalidOperationException($"{nameof(FineGrainedAuthorizationHandler)} was unable to resolve the current endpoint. This handler is only compatible with endpoint routing."); + var attributes = endpoint.Metadata.GetOrderedMetadata(); // The user is enforcing the fga policy but there's no attributes here. if (attributes.Count == 0) diff --git a/src/Fga.Net/Configuration/Auth0FgaConnectionBuilder.cs b/src/Fga.Net/Configuration/Auth0FgaConnectionBuilder.cs index 475297a..6f81c66 100644 --- a/src/Fga.Net/Configuration/Auth0FgaConnectionBuilder.cs +++ b/src/Fga.Net/Configuration/Auth0FgaConnectionBuilder.cs @@ -24,12 +24,20 @@ namespace Fga.Net.DependencyInjection.Configuration; /// /// Available environments for Auth0 FGA /// -public enum Auth0Environment +public enum FgaEnvironment { /// - /// US Environment - `fga.us.auth0.com` + /// US Environment - `api.us1.fga.dev` /// - Us + US, + /// + /// AU Environment - `api.au1.fga.dev` + /// + AU, + /// + /// EU Environment - `api.eu1.fga.dev` + /// + EU } internal sealed record Auth0FgaEnvironment(string ApiHost, string ApiTokenIssuer, string ApiAudience); @@ -40,33 +48,53 @@ internal sealed record Auth0FgaEnvironment(string ApiHost, string ApiTokenIssuer /// public sealed class Auth0FgaConnectionBuilder { - private readonly IReadOnlyDictionary _fgaEnvironments = - new Dictionary() + private const string FgaIssuer = "fga.us.auth0.com"; + + private readonly IReadOnlyDictionary _fgaEnvironments = + new Dictionary() { { - Auth0Environment.Us, - new Auth0FgaEnvironment("https://api.us1.fga.dev", "fga.us.auth0.com", "https://api.us1.fga.dev/") + FgaEnvironment.US, + new Auth0FgaEnvironment("https://api.us1.fga.dev", FgaIssuer, "https://api.us1.fga.dev/") + }, + { + FgaEnvironment.EU, + new Auth0FgaEnvironment("https://api.eu1.fga.dev", FgaIssuer, "https://api.eu1.fga.dev/") + }, + { + FgaEnvironment.AU, + new Auth0FgaEnvironment("https://api.au1.fga.dev", FgaIssuer, "https://api.au1.fga.dev/") } }; - private readonly Auth0Environment _environment = Auth0Environment.Us; + private FgaEnvironment _environment = FgaEnvironment.US; private string _clientId = null!; private string _clientSecret = null!; + /// + /// Set the region/environment that your Auth0 FGA store lives in. Defaults to if not set. + /// + /// An Auth0 FGA region + public Auth0FgaConnectionBuilder SetEnvironment(FgaEnvironment environment) + { + _environment = environment; + return this; + } /// /// Configure authentication for Auth0 FGA /// /// Client Id from your Auth0 FGA Account /// Client Secret from your Auth0 FGA Account - public void WithAuthentication(string clientId, string clientSecret) + public Auth0FgaConnectionBuilder WithAuthentication(string clientId, string clientSecret) { ArgumentNullException.ThrowIfNull(clientId); ArgumentNullException.ThrowIfNull(clientSecret); _clientId = clientId; _clientSecret = clientSecret; + return this; } internal FgaConnectionConfiguration Build() diff --git a/src/Fga.Net/Fga.Net.DependencyInjection.csproj b/src/Fga.Net/Fga.Net.DependencyInjection.csproj index 6024c47..2c35f73 100644 --- a/src/Fga.Net/Fga.Net.DependencyInjection.csproj +++ b/src/Fga.Net/Fga.Net.DependencyInjection.csproj @@ -13,7 +13,7 @@ - + diff --git a/tests/Fga.Net.Tests/Fga.Net.Tests.csproj b/tests/Fga.Net.Tests/Fga.Net.Tests.csproj index 51d591d..33319d8 100644 --- a/tests/Fga.Net.Tests/Fga.Net.Tests.csproj +++ b/tests/Fga.Net.Tests/Fga.Net.Tests.csproj @@ -10,10 +10,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/Fga.Net.Tests/Unit/ExtensionTests.cs b/tests/Fga.Net.Tests/Unit/ExtensionTests.cs index 4dab971..ef92875 100644 --- a/tests/Fga.Net.Tests/Unit/ExtensionTests.cs +++ b/tests/Fga.Net.Tests/Unit/ExtensionTests.cs @@ -113,7 +113,8 @@ public void AspNetCoreServiceExtensions_RegisterCorrectly(ExtensionScenario scen new ExtensionScenario("Auth0 FGA", config => config.ConfigureAuth0Fga(x => { - x.WithAuthentication(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); + x.SetEnvironment(FgaEnvironment.AU) + .WithAuthentication(Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); })), new ExtensionScenario("OpenFGA - No Credentials", config => config.ConfigureOpenFga(x =>