OpenFga.Sdk 0.1.0

.NET 6.0
NuGet\Install-Package OpenFga.Sdk -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package OpenFga.Sdk --version 0.1.0
<PackageReference Include="OpenFga.Sdk" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenFga.Sdk --version 0.1.0
#r "nuget: OpenFga.Sdk, 0.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenFga.Sdk as a Cake Addin
#addin nuget:?package=OpenFga.Sdk&version=0.1.0

// Install OpenFga.Sdk as a Cake Tool
#tool nuget:?package=OpenFga.Sdk&version=0.1.0

.NET SDK for OpenFGA

Nuget Release License FOSSA Status Discord Server Twitter

This is an autogenerated SDK for OpenFGA. It provides a wrapper around the OpenFGA API definition.

Table of Contents

About

OpenFGA is an open source Fine-Grained Authorization solution inspired by Google's Zanzibar paper. It was created by the FGA team at Auth0 based on Auth0 Fine-Grained Authorization (FGA), available under a permissive license (Apache-2) and welcomes community contributions.

OpenFGA is designed to make it easy for application builders to model their permission layer, and to add and integrate fine-grained authorization into their applications. OpenFGA’s design is optimized for reliability and low latency at a high scale.

Resources

Installation

The OpenFGA .NET SDK is available on NuGet.

You can install it using:

dotnet add package OpenFga.Sdk
Install-Package OpenFga.Sdk

Search for and install OpenFga.Sdk in each of their respective package manager UIs.

Getting Started

Initializing the API Client

Learn how to initialize your SDK

Without an API Token

using OpenFga.Sdk.Api;
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Configuration;
using OpenFga.Sdk.Model;

namespace Example {
    public class Example {
        public static void Main() {
            try {
                var configuration = new Configuration() {
                    ApiScheme = Environment.GetEnvironmentVariable("OPENFGA_API_SCHEME"), // optional, defaults to "https"
                    ApiHost = Environment.GetEnvironmentVariable("OPENFGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
                    StoreId = Environment.GetEnvironmentVariable("OPENFGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
                };
                var openFgaApi = new OpenFgaApi(configuration);
                var response = openFgaApi.ReadAuthorizationModels();
            } catch (ApiException e) {
                 Debug.Print("Status Code: "+ e.ErrorCode);
            }
        }
    }
}

With an API Token

using OpenFga.Sdk.Api;
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Configuration;
using OpenFga.Sdk.Model;

namespace Example {
    public class Example {
        public static async void Main() {
            try {
                var configuration = new Configuration() {
                    ApiScheme = Environment.GetEnvironmentVariable("OPENFGA_API_SCHEME"), // optional, defaults to "https"
                    ApiHost = Environment.GetEnvironmentVariable("OPENFGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
                    StoreId = Environment.GetEnvironmentVariable("OPENFGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
                    Credentials = new Credentials() {
                        Method = CredentialsMethod.ApiToken,
                        Config = new CredentialsConfig() {
                            ApiToken = Environment.GetEnvironmentVariable("OPENFGA_API_TOKEN"),  // will be passed as the "Authorization: Bearer ${ApiToken}" request header
                        }
                    }
                };
                var openFgaApi = new OpenFgaApi(configuration);
                var response = await openFgaApi.ReadAuthorizationModels();
            } catch (ApiException e) {
                 Debug.Print("Status Code: "+ e.ErrorCode);
            }
        }
    }
}

Get your Store ID

You need your store id to call the OpenFGA API (unless it is to call the CreateStore or ListStores methods).

If your server is configured with authentication enabled, you also need to have your credentials ready.

Calling the API

List Stores

API Documentation

var configuration = new Configuration() {
    ApiScheme = Environment.GetEnvironmentVariable("OPENFGA_API_SCHEME"),
    ApiHost = Environment.GetEnvironmentVariable("OPENFGA_API_HOST"),
};
var openFgaApi = new OpenFgaApi(configuration);
var response = await openFgaApi.ListStores();

// stores = [{ "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }]
Create Store

API Documentation

var configuration = new Configuration() {
    ApiScheme = Environment.GetEnvironmentVariable("OPENFGA_API_SCHEME"),
    ApiHost = Environment.GetEnvironmentVariable("OPENFGA_API_HOST"),
};
var openFgaApi = new OpenFgaApi(configuration);
var store = await openFgaApi.CreateStore(new CreateStoreRequest(){Name = "FGA Demo"})

// store.Id = "01FQH7V8BEG3GPQW93KTRFR8JB"

// store store.Id in database

// update the storeId of the current instance
openFgaApi.StoreId = storeId;

// continue calling the API normally
Get Store

API Documentation

Requires a client initialized with a storeId

var configuration = new Configuration() {
    ApiScheme = Environment.GetEnvironmentVariable("OPENFGA_API_SCHEME"),
    ApiHost = Environment.GetEnvironmentVariable("OPENFGA_API_HOST"),
    StoreId = Environment.GetEnvironmentVariable("OPENFGA_STORE_ID"),
};
var openFgaApi = new OpenFgaApi(configuration);
var store = await openFgaApi.GetStore();

// store = { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" }
Delete Store

API Documentation

Requires a client initialized with a storeId

var store = await openFgaApi.DeleteStore();
Write Authorization Model

API Documentation

Note: To learn how to build your authorization model, check the Docs at https://openfga.dev/docs.

Learn more about the OpenFGA configuration language.

var relations = new Dictionary<string, Userset>()
{
    {"writer", new Userset(_this: new object())},
    {
        "reader",
        new Userset(union: new Usersets(new List<Userset>()
            {new(new object(), new ObjectRelation("", "writer"))}))
    }
};
var body = new WriteAuthorizationModelRequest(new List<TypeDefinition>() {new("repo", relations)});
var response = await openFgaApi.WriteAuthorizationModel(body);

// response.AuthorizationModelId = 1uHxCSuTP0VKPYSnkq1pbb1jeZw
Read a Single Authorization Model

API Documentation

string authorizationModelId = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"; // Assuming `1uHxCSuTP0VKPYSnkq1pbb1jeZw` is an id of an existing model
var response = await openFgaApi.ReadAuthorizationModel(authorizationModelId);

// response.AuthorizationModel.Id = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"
// response.AuthorizationModel.TypeDefinitions = [{ "type": "repo", "relations": { ... } }]
Read Authorization Model IDs

API Documentation

var response = await openFgaApi.ReadAuthorizationModels();

// response.AuthorizationModelIds = ["1uHxCSuTP0VKPYSnkq1pbb1jeZw", "GtQpMohWezFmIbyXxVEocOCxxgq"];
Check

API Documentation

var body =
    new CheckRequest(tupleKey: new TupleKey("document:project-roadmap", "editor", "user:81684243-9356-4421-8fbf-a4f8d36aa31b"));
var response = await openFgaApi.Check(body);
// response.Allowed = true
Write Tuples

API Documentation

var body = new WriteRequest(new TupleKeys(new List<TupleKey>
    {new("document:project-roadmap", "editor", "user:81684243-9356-4421-8fbf-a4f8d36aa31b")}));
var response = await openFgaApi.Write(body);
Delete Tuples

API Documentation

var body = new WriteRequest(new TupleKeys(new List<TupleKey> { }),
    new TupleKeys(new List<TupleKey> {new("document:project-roadmap", "editor", "user:81684243-9356-4421-8fbf-a4f8d36aa31b")}));
var response = await openFgaApi.Write(body);
Expand

API Documentation

var body = new ExpandRequest(new TupleKey("document:project-roadmap", "editor"));
var response = await openFgaApi.Expand(body);

// response.Tree.Root = {"name":"workspace:675bcac4-ad38-4fb1-a19a-94a5648c91d6#admin","leaf":{"users":{"users":["user:81684243-9356-4421-8fbf-a4f8d36aa31b","user:f52a4f7a-054d-47ff-bb6e-3ac81269988f"]}}}
Read Changes

API Documentation

// Find if a relationship tuple stating that a certain user is an admin on a certain workspace
var body = new ReadRequest(new TupleKey(
    _object: "workspace:675bcac4-ad38-4fb1-a19a-94a5648c91d6",
    relation: "admin",
    user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b"));

// Find all relationship tuples where a certain user has a relationship as any relation to a certain workspace
var body = new ReadRequest(new TupleKey(
    _object: "workspace:675bcac4-ad38-4fb1-a19a-94a5648c91d6",
    user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b"));

// Find all relationship tuples where a certain user is an admin on any workspace
var body = new ReadRequest(new TupleKey(
    _object: "workspace:",
    relation: "admin",
    user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b"));

// Find all relationship tuples where any user has a relationship as any relation with a particular workspace
var body = new ReadRequest(new TupleKey(
    _object: "workspace:675bcac4-ad38-4fb1-a19a-94a5648c91d6"));

var body = new ReadRequest(new TupleKey("document:project-roadmap", "editor", "user:81684243-9356-4421-8fbf-a4f8d36aa31b"));
var response = await openFgaApi.Read(body);

// In all the above situations, the response will be of the form:
// response = {"tuples":[{"key":{"user":"...","relation":"...","object":"..."},"timestamp":"..."}]}
Read Changes (Watch)

API Documentation

var type = 'workspace';
var pageSize = 25;
var continuationToken = 'eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==';

var response = await openFgaApi.ReadChanges(type, pageSize, continuationToken);

// response.continuation_token = ...
// response.changes = [
//   { tuple_key: { user, relation, object }, operation: "write", timestamp: ... },
//   { tuple_key: { user, relation, object }, operation: "delete", timestamp: ... }
// ]
List Objects

API Documentation

var body = new ListObjectsRequest{
    AuthorizationModelId = "01GAHCE4YVKPQEKZQHT2R89MQV",
    User = "anne",
    Relation = "can_read",
    Type = "document",
    ContextualTuples = new ContextualTupleKeys() {
        TupleKeys = new List<TupleKey> {
            new("folder:product", "editor", "user:81684243-9356-4421-8fbf-a4f8d36aa31b"),
            new("document:roadmap", "parent", "folder:product")
        }
    }
};
var response = await openFgaApi.ListObjects(body);

// response.ObjectIds = ["roadmap"]

API Endpoints

Method HTTP request Description
Check POST /stores/{store_id}/check Check whether a user is authorized to access an object
CreateStore POST /stores Create a store
DeleteStore DELETE /stores/{store_id} Delete a store
Expand POST /stores/{store_id}/expand Expand all relationships in userset tree format, and following userset rewrite rules. Useful to reason about and debug a certain relationship
GetStore GET /stores/{store_id} Get a store
ListObjects POST /stores/{store_id}/list-objects ListObjects lists all of the object ids for objects of the provided type that the given user has a specific relation with.
ListStores GET /stores Get all stores
Read POST /stores/{store_id}/read Get tuples from the store that matches a query, without following userset rewrite rules
ReadAssertions GET /stores/{store_id}/assertions/{authorization_model_id} Read assertions for an authorization model ID
ReadAuthorizationModel GET /stores/{store_id}/authorization-models/{id} Return a particular version of an authorization model
ReadAuthorizationModels GET /stores/{store_id}/authorization-models Return all the authorization models for a particular store
ReadChanges GET /stores/{store_id}/changes Return a list of all the tuple changes
Write POST /stores/{store_id}/write Add or delete tuples from the store
WriteAssertions PUT /stores/{store_id}/assertions/{authorization_model_id} Upsert assertions for an authorization model ID
WriteAuthorizationModel POST /stores/{store_id}/authorization-models Create a new authorization model

Models

Contributing

Issues

If you have found a bug or if you have a feature request, please report them on the sdk-generator repo issues section. Please do not report security vulnerabilities on the public GitHub issue tracker.

Pull Requests

All changes made to this repo will be overwritten on the next generation, so we kindly ask that you send all pull requests related to the SDKs to the sdk-generator repo instead.

Author

OpenFGA

License

This project is licensed under the Apache-2.0 license. See the LICENSE file for more info.

The code in this repo was auto generated by OpenAPI Generator from a template based on the csharp-netcore template, licensed under the Apache License 2.0.

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OpenFga.Sdk:

Package Downloads
Fga.Net.DependencyInjection

Auth0 Fine Grained Authorization for .NET. This package includes DI collection extensions for the FGA Client.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.0 35 9/30/2022
0.0.3 77 9/9/2022
0.0.2 94 8/16/2022
0.0.1 107 6/17/2022