Skip to content

Commit

Permalink
feat(config): update auth opts & allow no storeId for certain requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Jun 17, 2022
1 parent cc86268 commit ced4960
Show file tree
Hide file tree
Showing 57 changed files with 1,063 additions and 303 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
fossa:
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@v2
with:
dotnet-version: 6.0.x
- name: Restore dependencies
Expand All @@ -30,9 +30,9 @@ jobs:
snyk:
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@v2
with:
dotnet-version: 6.0.x
- name: Restore dependencies
Expand All @@ -48,10 +48,10 @@ 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@v2
with:
dotnet-version: 6.0.x

Expand All @@ -75,10 +75,10 @@ jobs:
needs: [test, fossa, snyk]

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

- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
source-url: https://api.nuget.org/v3/index.json
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:
needs: publish

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

- uses: Roang-zero1/github-create-release-action@5cf058ddffa6fa04e5cda07c98570c757dc4a0e1
with:
Expand Down
2 changes: 2 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ docs/WriteAssertionsRequest.md
docs/WriteAuthorizationModelResponse.md
docs/WriteRequest.md
src/OpenFga.Sdk.Test/Api/OpenFgaApiTests.cs
src/OpenFga.Sdk.Test/Models/ModelTests.cs
src/OpenFga.Sdk.Test/OpenFga.Sdk.Test.csproj
src/OpenFga.Sdk/Api/OpenFgaApi.cs
src/OpenFga.Sdk/Client/ApiClient.cs
Expand All @@ -76,6 +77,7 @@ src/OpenFga.Sdk/Client/OAuth2Client.cs
src/OpenFga.Sdk/Client/RequestBuilder.cs
src/OpenFga.Sdk/Client/Utils.cs
src/OpenFga.Sdk/Configuration/Configuration.cs
src/OpenFga.Sdk/Configuration/Credentials.cs
src/OpenFga.Sdk/Exceptions/ApiAuthenticationError.cs
src/OpenFga.Sdk/Exceptions/ApiError.cs
src/OpenFga.Sdk/Exceptions/ApiException.cs
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@

### [0.0.1](https://github.com/openfga/dotnet-sdk/releases/tag/v0.0.1) (2022-06-09)

Initial Release
Initial OpenFGA .NET SDK release
- Support for [OpenFGA](https://github.com/openfga/openfga) API
- CRUD stores
- Create, read & list authorization models
- Writing and Reading Tuples
- Checking authorization
- Using Expand to understand why access was granted
149 changes: 125 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# .NET SDK for OpenFGA

[![Nuget](https://img.shields.io/nuget/v/OpenFga.Sdk?label=OpenFga.Sdk&style=flat-square)](https://www.nuget.org/packages/OpenFga.Sdk)
[![Release](https://img.shields.io/github/v/release/openfga/dotnet-sdk?sort=semver&color=green)](https://github.com/openfga/dotnet-sdk/releases)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](./LICENSE)
[![FOSSA Status](https://app.fossa.com/api/projects/custom%2B4989%2Fgithub.com%2Fopenfga%2Fdotnet-sdk.svg?type=shield)](https://app.fossa.com/reports/4b3a7481-7918-4779-b4ff-b42e5c273517)
[![Discord Server](https://img.shields.io/discord/759188666072825867?color=7289da&logo=discord "Discord Server")](https://discord.com/channels/759188666072825867/930524706854031421)
[![Twitter](https://img.shields.io/twitter/follow/openfga?color=%23179CF0&logo=twitter&style=flat-square "@openfga on Twitter")](https://twitter.com/openfga)

This is an autogenerated SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).

Warning: This SDK comes with no SLAs and is not production-ready!

## Table of Contents

- [About OpenFGA](#about)
Expand Down Expand Up @@ -38,14 +42,14 @@ OpenFGA is designed to make it easy for application builders to model their perm

It allows in-memory data storage for quick development, as well as pluggable database modules - with initial support for PostgreSQL.

It offers an [HTTP API](https://openfga.dev/docs/api) and has SDKs for programming languages including [Node.js/JavaScript](https://github.com/openfga/js-sdk), [GoLang](https://github.com/openfga/go-sdk) and [.NET](https://github.com/openfga/dotnet-sdk).
It offers an [HTTP API](https://openfga.dev/api) and has SDKs for programming languages including [Node.js/JavaScript](https://github.com/openfga/js-sdk), [GoLang](https://github.com/openfga/go-sdk) and [.NET](https://github.com/openfga/dotnet-sdk).

More SDKs and integrations such as Rego are planned for the future.

## Resources

- [OpenFGA Documentation](https://openfga.dev/docs)
- [OpenFGA API Documentation](https://openfga.dev/docs/api)
- [OpenFGA API Documentation](https://openfga.dev/api)
- [Twitter](https://twitter.com/openfga)
- [OpenFGA Discord Community](https://discord.gg/8naAwJfWN6)
- [Zanzibar Academy](https://zanzibar.academy)
Expand Down Expand Up @@ -77,6 +81,10 @@ Search for and install `OpenFga.Sdk` in each of their respective package manager

### Initializing the API Client

[Learn how to initialize your SDK](https://openfga.dev/docs/getting-started/setup-sdk-client)

Without an API Token

```csharp
using OpenFga.Sdk.Api;
using OpenFga.Sdk.Client;
Expand All @@ -87,18 +95,46 @@ namespace Example {
public class Example {
public static void Main() {
try {
var configuration = new Configuration(storeId, environment) {
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

```csharp
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.openfga.example instead of https://api.openfga.example)
StoreId = Environment.GetEnvironmentVariable("OPENFGA_STORE_ID"),
ApiTokenIssuer = Environment.GetEnvironmentVariable("OPENFGA_API_TOKEN_ISSUER"), // optional, required if client id is passed
ApiAudience = Environment.GetEnvironmentVariable("OPENFGA_API_AUDIENCE"), // optional, required if client id is passed
ClientId = Environment.GetEnvironmentVariable("OPENFGA_CLIENT_ID"), // optional (pass in if your provider requires authentication via client credential flow)
ClientSecret = Environment.GetEnvironmentVariable("OPENFGA_CLIENT_SECRET"), // optional, required if client id is passed
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 = openFgaApi.ReadAuthorizationModels();
Debug.WriteLine(response.AuthorizationModelIds);
var response = await openFgaApi.ReadAuthorizationModels();
} catch (ApiException e) {
Debug.Print("Status Code: "+ e.ErrorCode);
}
Expand All @@ -110,17 +146,82 @@ namespace Example {

### Get your Store ID and optional credentials

You need your store id to call the OpenFGA API. You may also configure your credentials if your token provider requires it.
You need your store id to call the OpenFGA API (unless it is to create a store or list all stores). You may also configure your credentials if your service requires it.

### Calling the API

#### List Stores

[API Documentation](https://openfga.dev/api/docs/api#/Stores/ListStores)

```csharp
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](https://openfga.dev/api/docs/api#/Stores/CreateStore)

```csharp
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](https://openfga.dev/api/docs/api#/Stores/GetStore)

> Requires a client initialized with a storeId
```csharp
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](https://openfga.dev/api/docs/api#/Stores/DeleteStore)

> Requires a client initialized with a storeId
```csharp
var store = await openFgaApi.DeleteStore();
```

#### Write Authorization Model

[API Documentation](https://openfga.dev/docs/api#/Authorization%20Models/WriteAuthorizationModel)
[API Documentation](https://openfga.dev/api#/Authorization%20Models/WriteAuthorizationModel)

> Note: To learn how to build your authorization model, check the Docs at https://openfga.dev/docs.
> Learn more about [the OpenFGA configuration language](https://openfga.dev/docs/modeling/configuration-language).
> Learn more about [the OpenFGA configuration language](https://openfga.dev/docs/configuration-language).
```csharp
var relations = new Dictionary<string, Userset>()
Expand All @@ -140,7 +241,7 @@ var response = await openFgaApi.WriteAuthorizationModel(body);

#### Read a Single Authorization Model

[API Documentation](https://openfga.dev/docs/api#/Authorization%20Models/ReadAuthorizationModel)
[API Documentation](https://openfga.dev/api#/Authorization%20Models/ReadAuthorizationModel)

```csharp
string authorizationModelId = "1uHxCSuTP0VKPYSnkq1pbb1jeZw"; // Assuming `1uHxCSuTP0VKPYSnkq1pbb1jeZw` is an id of an existing model
Expand All @@ -152,7 +253,7 @@ var response = await openFgaApi.ReadAuthorizationModel(authorizationModelId);

#### Read Authorization Model IDs

[API Documentation](https://openfga.dev/docs/api#/Authorization%20Models/ReadAuthorizationModels)
[API Documentation](https://openfga.dev/api#/Authorization%20Models/ReadAuthorizationModels)

```csharp
var response = await openFgaApi.ReadAuthorizationModels();
Expand All @@ -162,7 +263,7 @@ var response = await openFgaApi.ReadAuthorizationModels();

#### Check

[API Documentation](https://openfga.dev/docs/api#/Tuples/Check)
[API Documentation](https://openfga.dev/api#/Relationship%20Queries/Check)

```csharp
var body =
Expand All @@ -173,7 +274,7 @@ var response = await openFgaApi.Check(body);

#### Write Tuples

[API Documentation](https://openfga.dev/docs/api#/Tuples/Write)
[API Documentation](https://openfga.dev/api#/Relationship%20Tuples/Write)

```csharp
var body = new WriteRequest(new TupleKeys(new List<TupleKey>
Expand All @@ -183,7 +284,7 @@ var response = await openFgaApi.Write(body);

#### Delete Tuples

[API Documentation](https://openfga.dev/docs/api#/Tuples/Write)
[API Documentation](https://openfga.dev/api#/Relationship%20Tuples/Write)

```csharp
var body = new WriteRequest(new TupleKeys(new List<TupleKey> { }),
Expand All @@ -193,7 +294,7 @@ var response = await openFgaApi.Write(body);

#### Expand

[API Documentation](https://openfga.dev/docs/api#/Debugging/Expand)
[API Documentation](https://openfga.dev/api#/Relationship%20Queries/Expand)

```csharp
var body = new ExpandRequest(new TupleKey("document:project-roadmap", "editor"));
Expand All @@ -204,7 +305,7 @@ var response = await openFgaApi.Expand(body);

#### Read

[API Documentation](https://openfga.dev/docs/api#/Tuples/Read)
[API Documentation](https://openfga.dev/api#/Relationship%20Tuples/Read)

```csharp
// Find if a relationship tuple stating that a certain user is an admin on a certain workspace
Expand Down Expand Up @@ -237,7 +338,7 @@ var response = await openFgaApi.Read(body);

#### Read Changes (Watch)

[API Documentation](https://openfga.dev/docs/api#/Tuples/ReadChanges)
[API Documentation](https://openfga.dev/api#/Relationship%20Tuples/ReadChanges)

```csharp
var type = 'workspace';
Expand Down
Loading

0 comments on commit ced4960

Please sign in to comment.