Skip to content

Commit

Permalink
v2.1 (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: JT <[email protected]>
  • Loading branch information
Hawxy and Hawxy authored Feb 17, 2025
1 parent 1a0801f commit d6e7858
Show file tree
Hide file tree
Showing 29 changed files with 81 additions and 60 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

## Getting Started

This package is compatible with the OSS OpenFGA as well as the managed Auth0 FGA service.
This package is compatible with the OSS OpenFGA as well as the managed Okta FGA service.

Please ensure you have a basic understanding of how FGA works before continuing: [OpenFGA Docs](https://openfga.dev/) or [Auth0 FGA Docs](https://docs.fga.dev/)
Please ensure you have a basic understanding of how FGA works before continuing: [OpenFGA Docs](https://openfga.dev/) or [Okta FGA Docs](https://docs.fga.dev/)

## ASP.NET Core Setup

This tutorial assumes you have authentication setup within your project, such as [JWT bearer authentication via Auth0](https://auth0.com/docs/quickstart/backend/aspnet-core-webapi/01-authorization).

Install `Fga.Net.AspNetCore` from Nuget before continuing.

### Auth0 FGA
### Okta FGA

Ensure you have a Store ID, Client ID, and Client Secret ready from [How to get your API keys](https://docs.fga.dev/integration/getting-your-api-keys).

Expand All @@ -29,20 +29,20 @@ Ensure you have a Store ID, Client ID, and Client Secret ready from [How to get
```cs
builder.Services.AddOpenFgaClient(config =>
{
config.ConfigureAuth0Fga(x =>
config.ConfigureOktaFga(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"]!);
x.WithAuthentication(builder.Configuration["OktaFga:ClientId"]!, builder.Configuration["OktaFga:ClientSecret"]!);
});

config.SetStoreId(builder.Configuration["Auth0Fga:StoreId"]!);
config.SetStoreId(builder.Configuration["OktaFga:StoreId"]!);
});

builder.Services.AddOpenFgaMiddleware();
```

The `ConfigureAuth0Fga` extension will configure the client to work with the Auth0 US environment. An environment selector will be added as additional regions come online.
The `ConfigureOktaFga` extension will configure the client to work with the Okta US environment. An environment selector will be added as additional regions come online.

### OpenFGA

Expand Down Expand Up @@ -176,7 +176,7 @@ An additional pre-made attribute that allows all tuple values to be hardcoded st

### Contextual Tuples

All attributes supports specifying contextual tuples as part of a check. Inherit & override `GetContextualTuple` to provide the relevant logic in your own attribute.
All attributes supports specifying contextual tuples as part of a check. Inherit & override `GetContextualTuples` to provide the relevant logic in your own attribute.

## Client Injection

Expand Down Expand Up @@ -206,21 +206,21 @@ services.PostConfigureFgaClient(config =>
To get started:

1. Install `Fga.Net.DependencyInjection`
2. Add your `StoreId`, `ClientId` and `ClientSecret` Auth0 FGA configuration **OR** `ApiUrl` & `StoreId` OpenFGA configuration 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 your `StoreId`, `ClientId` and `ClientSecret` Okta FGA configuration **OR** `ApiUrl` & `StoreId` OpenFGA configuration 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).
3. Register the authorization client:

```cs
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Auth0 FGA
// Okta FGA
services.AddOpenFgaClient(config =>
{
config.ConfigureAuth0Fga(x =>
config.ConfigureOktaFga(x =>
{
x.WithAuthentication(context.Configuration["Auth0Fga:ClientId"], context.Configuration["Auth0Fga:ClientSecret"]);
x.WithAuthentication(context.Configuration["OktaFga:ClientId"], context.Configuration["OktaFga:ClientSecret"]);
});
config.SetStoreId(context.Configuration["Auth0Fga:StoreId"]);
config.SetStoreId(context.Configuration["OktaFga:StoreId"]);
});

// OpenFGA
Expand Down Expand Up @@ -273,4 +273,4 @@ See the [OpenFGA.Sdk docs](https://openfga.dev/docs/getting-started/setup-sdk-cl

## Disclaimer

I am not affiliated with nor represent Auth0 or OpenFGA. All support queries regarding the underlying service should go to the [Auth0 Labs Discord](https://discord.gg/8naAwJfWN6).
I am not affiliated with nor represent Okta or OpenFGA. All support queries regarding the underlying service should go to the respective support channels.
10 changes: 9 additions & 1 deletion src/Fga.Net.AspNetCore/Authorization/Attributes/FgaAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,7 +52,15 @@ public abstract class FgaAttribute : Attribute
/// </summary>
/// <param name="context">The context of the current request</param>
/// <returns>The list of contextual tuples, or null if none were provided</returns>
[Obsolete("Replace with GetContextualTuples")]
public virtual ValueTask<List<ClientTupleKey>?> GetContextualTuple(HttpContext context) => new((List<ClientTupleKey>?)null);

/// <summary>
/// Contextual tuple(s) to apply the check generated from this attribute.
/// </summary>
/// <param name="context">The context of the current request</param>
/// <returns>The list of contextual tuples, or null if none were provided</returns>
public virtual ValueTask<List<ClientTupleKey>?> GetContextualTuples(HttpContext context) => new((List<ClientTupleKey>?)null);

/// <summary>
/// Concats the type and identifier into the object format
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy
Copyright 2021-2025 Hawxy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/Authorization/FgaCheckDecorator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,7 +62,9 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
user = await attribute.GetUser(httpContext);
relation = await attribute.GetRelation(httpContext);
@object = await attribute.GetObject(httpContext);
contextualTuples = await attribute.GetContextualTuple(httpContext);
#pragma warning disable CS0618 // Type or member is obsolete
contextualTuples = await attribute.GetContextualTuples(httpContext) ?? await attribute.GetContextualTuple(httpContext);
#pragma warning restore CS0618 // Type or member is obsolete
}
catch (FgaMiddlewareException ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/Authorization/Log.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/Authorization/Validation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions src/Fga.Net.AspNetCore/Fga.Net.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<PropertyGroup>
<PackageId>Fga.Net.AspNetCore</PackageId>
<Description>Auth0 Fine Grained Authorization for ASP.NET Core. This package includes ASP.NET Core authorization extensions.</Description>
<PackageTags>fga,auth0,sandcastle,asp.net</PackageTags>
<Description>OpenFGA/Okta FGA for ASP.NET Core. This package includes ASP.NET Core authorization extensions.</Description>
<PackageTags>fga,auth0,okta,sandcastle,asp.net</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/FgaAspNetCoreConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net.AspNetCore/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net/Configuration/FgaClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ limitations under the License.
namespace Fga.Net.DependencyInjection.Configuration;

/// <summary>
/// Available environments for Auth0 FGA
/// Available environments for Okta FGA
/// </summary>
public enum FgaEnvironment
{
Expand All @@ -40,30 +40,30 @@ public enum FgaEnvironment
EU
}

internal sealed record Auth0FgaEnvironment(string ApiHost, string ApiTokenIssuer, string ApiAudience);
internal sealed record OktaFgaEnvironment(string ApiHost, string ApiTokenIssuer, string ApiAudience);


/// <summary>
/// Configuration for Auth0 FGA environments
/// Configuration for Okta FGA environments
/// </summary>
public sealed class Auth0FgaConnectionBuilder
public sealed class OktaFgaConnectionBuilder
{
private const string FgaIssuer = "auth.fga.dev";

private readonly Dictionary<FgaEnvironment, Auth0FgaEnvironment> _fgaEnvironments =
private readonly Dictionary<FgaEnvironment, OktaFgaEnvironment> _fgaEnvironments =
new()
{
{
FgaEnvironment.US,
new Auth0FgaEnvironment("https://api.us1.fga.dev", FgaIssuer, "https://api.us1.fga.dev/")
new OktaFgaEnvironment("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/")
new OktaFgaEnvironment("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/")
new OktaFgaEnvironment("https://api.au1.fga.dev", FgaIssuer, "https://api.au1.fga.dev/")
}
};

Expand All @@ -76,7 +76,7 @@ public sealed class Auth0FgaConnectionBuilder
/// Set the region/environment that your Auth0 FGA store lives in. Defaults to <see cref="FgaEnvironment.US"/> if not set.
/// </summary>
/// <param name="environment">An Auth0 FGA region</param>
public Auth0FgaConnectionBuilder SetEnvironment(FgaEnvironment environment)
public OktaFgaConnectionBuilder SetEnvironment(FgaEnvironment environment)
{
_environment = environment;
return this;
Expand All @@ -87,7 +87,7 @@ public Auth0FgaConnectionBuilder SetEnvironment(FgaEnvironment environment)
/// </summary>
/// <param name="clientId">Client Id from your Auth0 FGA Account</param>
/// <param name="clientSecret">Client Secret from your Auth0 FGA Account</param>
public Auth0FgaConnectionBuilder WithAuthentication(string clientId, string clientSecret)
public OktaFgaConnectionBuilder WithAuthentication(string clientId, string clientSecret)
{
ArgumentNullException.ThrowIfNull(clientId);
ArgumentNullException.ThrowIfNull(clientSecret);
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net/Configuration/OpenFgaConnectionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region License
/*
Copyright 2021-2024 Hawxy (JT)
Copyright 2021-2025 Hawxy (JT)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/Fga.Net/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace Fga.Net.DependencyInjection;

internal static class Constants
{
public const string FgaHttpClient = "FgaHttpClient";
public const string FgaHttpClient = nameof(FgaHttpClient);
}
4 changes: 2 additions & 2 deletions src/Fga.Net/Fga.Net.DependencyInjection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<PropertyGroup>
<PackageId>Fga.Net.DependencyInjection</PackageId>
<Description>Auth0 Fine Grained Authorization for .NET. This package includes DI collection extensions for the FGA Client.</Description>
<PackageTags>fga,auth0,sandcastle</PackageTags>
<Description>OpenFGA/Okta FGA for .NET. This package includes DI collection extensions for the FGA Client.</Description>
<PackageTags>fga,auth0,okta,sandcastle</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit d6e7858

Please sign in to comment.