Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@openfga/syntax-transformer

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@openfga/syntax-transformer

[![npm](https://img.shields.io/npm/v/@openfga/syntax-transformer.svg?style=flat)](https://www.npmjs.com/package/@openfga/syntax-transformer) [![Release](https://img.shields.io/github/v/release/openfga/syntax-transformer?sort=semver&color=green)](https://g

Source
npmnpm
Version
0.1.4
Version published
Weekly downloads
45K
-38.52%
Maintainers
2
Weekly downloads
 
Created
Source

OpenFGA Syntax Transformer

npm Release License FOSSA Status Discord Server Twitter

The OpenFGA API accepts a JSON syntax for the configuration of the authorization model. The OpenFGA docs showcase an alternate friendlier syntax that can be used to build an OpenFGA authorization model.

This module transforms between the JSON syntax accepted by the OpenFGA API and the friendlier syntax you see throughout the documentation.

Table of Contents

About OpenFGA

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.

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 and a gRPC API. It has SDKs for Node.js/JavaScript, GoLang, Python and .NET. Look in our Community section for third-party SDKs and tools.

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

Resources

Installation

npm install --save @openfga/syntax-transformer // OR yarn add @openfga/syntax-transformer

Usage

The syntax transformer has grown to encompass a lot more functionality than previously intended, mostly components used by the FGA Playground. Please note that all functionality except: friendlySyntaxToApiSyntax and apiSyntaxToFriendlySyntax is undocumented and should be considered tentative and may be removed at any moment. If you depend on them, please reach out so that we can discuss out future plans for that functionality and make sure our plans are taking your use-case into consideration.

From the Friendly Syntax to the JSON Syntax

const { friendlySyntaxToApiSyntax } = require("@openfga/syntax-transformer");

const apiSyntax = friendlySyntaxToApiSyntax(
`model
  schema 1.1
type user
type document
  relations
    define blocked: [user]
    define editor: [user] but not blocked
`);

From the JSON Syntax to the Friendly Syntax

const { apiSyntaxToFriendlySyntax } = require("@openfga/syntax-transformer");

const friendlySyntax = apiSyntaxToFriendlySyntax({
  "schema_version": "1.1",
  "type_definitions": [{
    "type": "user",
    "relations": {}
  }, {
    "type": "document",
    "relations": {
      "blocked": { "this": {} },
      "editor": {
        "difference": {
          "base": { "this": {} },
          "subtract": {
            "computedUserset": {
              "object": "",
              "relation": "blocked"
            }
          }
        }
      }
    },
    "metadata": {
      "relations": {
        "blocked": {
          "directly_related_user_types": [{ "type": "user" }]
        },
        "editor": {
          "directly_related_user_types": [{ "type": "user" }]
        }
      }
    }
  }]
}
);

CLI

This transformer comes with a basic CLI that can be used to transform between the Friendly and API JSON syntaxes

Transform from OpenFGA API's JSON syntax to the friendly OpenFGA DSL

 npx @openfga/syntax-transformer transform --from=json --inputFile=test.json

Transform from OpenFGA's friendly DSL to the OpenFGA API's JSON syntax

npx @openfga/syntax-transformer transform --from=dsl --inputFile=test.openfga

Configuration Syntaxes

Schema 1.1

The two below Syntaxes are equivalent. Find out more on OpenFGA's configuration language here.

Friendly Syntax (DSL)

model
  schema 1.1
type user
type folder
  relations
    define editor: [user]
type document
  relations
    define parent: [folder]
    define editor: [user] or editor from parent

JSON Syntax

{
  "schema_version": "1.1",
  "type_definitions": [{
    "type": "user",
    "relations": {}
  }, {
    "type": "folder",
    "relations": {
      "editor": { "this": {} }
    },
    "metadata": {
      "relations": {
        "editor": {
          "directly_related_user_types": [{ "type": "user" }]
        }
      }
    }
  }, {
    "type": "document",
    "relations": {
      "parent": { "this": {} },
      "editor": {
        "union": {
          "child": [{
            "this": {}
          }, {
            "tupleToUserset": {
              "tupleset": {
                "object": "",
                "relation": "parent"
              },
              "computedUserset": {
                "object": "",
                "relation": "editor"
              }
            }
          }]
        }
      }
    },
    "metadata": {
      "relations": {
        "parent": {
          "directly_related_user_types": [{ "type": "folder" }]
        },
        "editor": {
          "directly_related_user_types": [{ "type": "user" }]
        }
      }
    }
  }]
}

Schema 1.0

The two below Syntaxes are equivalent. Find out more on OpenFGA's configuration language here.

Friendly Syntax (DSL)

type user
type folder
  relations
    define editor as self
type document
  relations
    define parent as self
    define editor as self or editor from parent

JSON Syntax

{
  "schema_version": "1.0",
  "type_definitions": [{
    "type": "user",
    "relations": {}
  }, {
    "type": "folder",
    "relations": {
      "editor": { "this": {} }
    }
  }, {
    "type": "document",
    "relations": {
      "parent": { "this": {} },
      "editor": {
        "union": {
          "child": [{
            "this": {}
          }, {
            "tupleToUserset": {
              "tupleset": {
                "object": "",
                "relation": "parent"
              },
              "computedUserset": {
                "object": "",
                "relation": "editor"
              }
            }
          }]
        }
      }
    }
  }]
}

Community Parsers

RepoLicenseMaintainersLanguageSchema v1.0Schema v1.1Package ManagersOther Links
syntax-transformerApache-2.0@openfgaTypescriptYesYesnpm:@openfga/syntax-transformer
openfga-dsl-parserApache-2.0@maxmindlin - @dblclikRustYesNocrates:openfga-dsl-parserpypi:openfga-dsl-parser-pythonWASM - Python
openfga-rsApache-2.0@iammathewRustYesNo
openfga-dsl-parserApache-2.0@craigpastroANTLR & GoYesPartial (requires self). Supports nestingGitHub release (latest SemVer)

Community Wrapper

RepoLicenseMaintainersLanguageSchema v1.0Schema v1.1Package ManagersOther Links
fga-transformer-cliMIT@ozee-ioJavascriptYesYesnpm:@openfga/syntax-transformer

Contributing

Take a look at our Contributing Guide

Author

OpenFGA team

License

Apache-2.0

Keywords

openfga

FAQs

Package last updated on 11 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts