Skip to content

Commit

Permalink
v0.8 Alpha (#7)
Browse files Browse the repository at this point in the history
* Support client v0.1.2

* Support client v0.2.0

* Update extension naming and replace docs

* Update github actions
  • Loading branch information
Hawxy authored Jan 3, 2023
1 parent 1a04b04 commit 4d03308
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '7.0.x'

- name: Run build & test
run: ./build.sh Test
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/nuget-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '7.0.x'

- name: Run Nuget Pack
run: ./build.sh NugetPack
Expand Down
2 changes: 1 addition & 1 deletion Package.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.7.0-alpha</Version>
<Version>0.8.0-alpha</Version>
<Authors>Hawxy</Authors>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ Ensure you have a Store ID, Client ID, and Client Secret ready from [How to get
1. Add your `StoreId`, `ClientId` and `ClientSecret` to your application configuration, ideally via the [dotnet secrets manager](https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows#enable-secret-storage).
2. Add the following code to your ASP.NET Core services configuration:
```cs
builder.Services.AddOpenFga(x =>
builder.Services.AddOpenFgaClient(x =>
{
x.WithAuth0FgaDefaults(builder.Configuration["Auth0Fga:ClientId"], builder.Configuration["Auth0Fga:ClientSecret"]);

x.StoreId = builder.Configuration["Auth0Fga:StoreId"];
});

builder.Services.AddOpenFgaMiddleware();
```

The `WithAuth0FgaDefaults` extension will configure the relevant OpenFGA client settings to work with Auth0 FGA's US environment.
Expand All @@ -47,17 +49,19 @@ OpenFGA configuration is very similar to the [SDK Setup Guide](https://openfga.d
1. Add the FGA `ApiScheme`, `ApiHost` & `StoreId` to your application configuration.
2. Add the following code to your ASP.NET Core configuration:
```cs
builder.Services.AddOpenFga(x =>
builder.Services.AddOpenFgaClient(x =>
{
x.ApiScheme = builder.Configuration["Fga:ApiScheme"];
x.ApiHost = builder.Configuration["Fga:ApiHost"];
x.StoreId = builder.Configuration["Fga:StoreId"];
});

builder.Services.AddOpenFgaMiddleware();
```

### Authorization Policy Setup

We'll need to setup our authorization middleware like so:
We'll need to setup our authorization policy like so:

```cs
builder.Services.AddAuthorization(options =>
Expand Down Expand Up @@ -105,12 +109,11 @@ If you want to use the built-in attributes, you need to configure how the user's
The example below uses the Name, which should be suitable for most people (given the claim is mapped correctly).

```cs
builder.Services.AddOpenFga(x =>
{
//...
}, config =>
builder.Services.AddOpenFgaMiddleware(config =>
{
config.UserIdentityResolver = principal => principal.Identity!.Name!;
//If you're using DSL v1.1 it requires the user type to be included
//config.UserIdentityResolver = principal => $"user:{principal.Identity!.Name!}";
});
```

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="6.2.1" />
<PackageReference Include="Nuke.Common" Version="6.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 10 additions & 8 deletions samples/Fga.Example.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,27 @@


// Auth0 FGA
builder.Services.AddOpenFga(clientConfig =>
builder.Services.AddOpenFgaClient(clientConfig =>
{
clientConfig.WithAuth0FgaDefaults(builder.Configuration["Auth0Fga:ClientId"], builder.Configuration["Auth0Fga:ClientSecret"]);
clientConfig.WithAuth0FgaDefaults(builder.Configuration["Auth0Fga:ClientId"],
builder.Configuration["Auth0Fga:ClientSecret"]);
clientConfig.StoreId = builder.Configuration["Auth0Fga:StoreId"];
}, middlewareConfig =>
{
middlewareConfig.UserIdentityResolver = principal => principal.Identity!.Name!;
});
});

// OpenFGA
/*builder.Services.AddOpenFga(x =>
/*builder.Services.AddOpenFgaClient(x =>
{
x.ApiScheme = builder.Configuration["Fga:ApiScheme"];
x.ApiHost = builder.Configuration["Fga:ApiHost"];
x.StoreId = builder.Configuration["Fga:StoreId"];
});*/

builder.Services.AddOpenFgaMiddleware(middlewareConfig =>
{
middlewareConfig.UserIdentityResolver = principal => principal.Identity!.Name!;
});

builder.Services.AddAuthorization(options =>
{
options.AddPolicy(FgaAuthorizationDefaults.PolicyKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public override ValueTask<string> GetRelation(HttpContext context)
}

/// <inheritdoc />
public override ValueTask<string> GetObject(HttpContext context)
public override async ValueTask<string> GetObject(HttpContext context)
{
context.Request.EnableBuffering();
using var document = JsonDocument.Parse(context.Request.Body);
using var document = await JsonDocument.ParseAsync(context.Request.Body, cancellationToken: context.RequestAborted);
if (document.RootElement.TryGetProperty(_property, out var element))
{
context.Request.Body.Position = 0;
return ValueTask.FromResult(FormatObject(_type,element.GetString()!));
return FormatObject(_type,element.GetString()!);
}

throw new FgaMiddlewareException($"Unable to resolve JSON property {_property}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
}
}, httpContext.RequestAborted);

if (!result.Allowed)
if (result.Allowed is false)
{
_logger.CheckFailureDebug(user, relation, @object);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/Controllers/FgaControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public async Task<bool> Check(string user, string relation, string @object, Canc
Object = @object
}
}, ct);
return checkRes.Allowed;
return checkRes.Allowed.HasValue && checkRes.Allowed.Value;
}
}
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/Fga.Net.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
16 changes: 16 additions & 0 deletions src/Fga.Net.AspNetCore/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class ServiceCollectionExtensions
/// <param name="clientConfig">The delegate for the <see cref="FgaClientConfiguration"/> that will be used to configure the <see cref="OpenFgaApi"/></param>
/// <param name="middlewareConfig">The delegate for the <see cref="FgaAspNetCoreConfiguration"/> that will be used to configure the underlying middleware</param>
/// <returns>The service collection</returns>
[Obsolete("Replace with AddOpenFgaClient & AddOpenFgaMiddleware")]
public static IServiceCollection AddOpenFga(this IServiceCollection collection, Action<FgaClientConfiguration> clientConfig, Action<FgaAspNetCoreConfiguration>? middlewareConfig = null)
{
ArgumentNullException.ThrowIfNull(clientConfig);
Expand All @@ -47,6 +48,21 @@ public static IServiceCollection AddOpenFga(this IServiceCollection collection,
return collection;
}

/// <summary>
/// Adds and configures an <see cref="FineGrainedAuthorizationHandler"/>
/// </summary>
/// <param name="collection">The service collection</param>
/// <param name="middlewareConfig">The delegate for the <see cref="FgaAspNetCoreConfiguration"/> that will be used to configure the underlying middleware</param>
/// <returns>The service collection</returns>
public static IServiceCollection AddOpenFgaMiddleware(this IServiceCollection collection, Action<FgaAspNetCoreConfiguration>? middlewareConfig = null)
{
if (middlewareConfig != null)
collection.Configure(middlewareConfig);
collection.AddScoped<IFgaCheckDecorator, FgaCheckDecorator>();
collection.AddScoped<IAuthorizationHandler, FineGrainedAuthorizationHandler>();
return collection;
}

/// <summary>
/// Adds a <see cref="FineGrainedAuthorizationRequirement"/> to the given policy.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Fga.Net/Fga.Net.DependencyInjection.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="OpenFga.Sdk" Version="0.1.0" />
<PackageReference Include="OpenFga.Sdk" Version="0.2.0" />
</ItemGroup>

<Import Project="../../Package.Build.props" />
Expand Down
8 changes: 5 additions & 3 deletions tests/Fga.Net.Tests/Client/EndpointTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alba;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -34,15 +35,15 @@ private async Task GetEndpoints_Return_200()
var modelResponse = await client.ReadAuthorizationModel(modelId);

Assert.NotNull(modelResponse);
Assert.NotNull(modelResponse.AuthorizationModel.Id);
Assert.NotNull(modelResponse.AuthorizationModel?.Id);

var assertions = await client.ReadAssertions(modelId);

Assert.NotNull(assertions);
Assert.True(assertions.Assertions?.Count > 0);
var assertion = assertions.Assertions!.First().TupleKey;

Assert.NotEmpty(assertion.Object!);
Assert.NotEmpty(assertion!.Object!);
Assert.NotEmpty(assertion.Relation!);
Assert.NotEmpty(assertion.User!);

Expand All @@ -58,6 +59,7 @@ private async Task GetEndpoints_Return_200()
var watch = await client.ReadChanges();
Assert.NotNull(watch);


}

}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fga.Net.Tests/Fga.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alba" Version="6.1.0" />
<PackageReference Include="HttpContextMoq" Version="1.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="Alba" Version="7.2.1" />
<PackageReference Include="HttpContextMoq" Version="1.4.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down
6 changes: 4 additions & 2 deletions tests/Fga.Net.Tests/Unit/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public void AspNetCoreServiceExtensions_RegisterCorrectly()
{
var collection = new ServiceCollection();

collection.AddOpenFga(x =>
collection.AddOpenFgaClient(x =>
{
x.StoreId = Guid.NewGuid().ToString();
x.WithAuth0FgaDefaults(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}, x =>
});

collection.AddOpenFgaMiddleware(x =>
{
x.UserIdentityResolver = principal => principal.Identity!.Name!;
});
Expand Down

0 comments on commit 4d03308

Please sign in to comment.