An open API service indexing awesome lists of open source software.

https://github.com/danhtran94/authzed-codegen

Type-Safety Code Generation tools for AuthZed. (Golang)
https://github.com/danhtran94/authzed-codegen

acl authzed fga generator permission rbac

Last synced: 18 days ago
JSON representation

Type-Safety Code Generation tools for AuthZed. (Golang)

Awesome Lists containing this project

README

        

# `authzed-codegen`

This repository contains Type-Safe Code Generation tools for AuthZed.

[Authzed](https://authzed.com/) is a powerful authorization engine that allows you to define and manage your authorization policies. This code generation tool helps you generate type-safe code for your AuthZed schemas, making it easier to work with your authorization policies in a type-safe manner.

TLDR; `zed` schema => `go` code generation.

```hcl
// ... other definitions

definition menusvc/order {
relation creator: menusvc/user | menusvc/customer
relation company: menusvc/company
permission write = creator + creator->manage + company->manage
}

// ... other definitions
```

```golang
// order-flow.go
// usage generated stubs example
// ...

order := menusvc.Order("order-id")
user := menusvc.User("user-id")

order.CreateCreatorRelations(ctx, menusvc.OrderCreatorObjects{
User: []menusvc.User{user},
// Customer: []menusvc.Customer{},
})

order.
CheckWrite(ctx, acl.CheckOrderWriteInputs{
User: []menusvc.User{user},
// Company: []menusvc.Company{},
})

// ...
```

```golang
// order.gen.go
// Code generated by authzed-codegen. DO NOT EDIT.

package menusvc

const TypeOrder authz.Type = "menusvc/order"
type RelationOrder authz.Relation
type PermissionOrder authz.Permission

// Many payload types like `OrderCreatorObjects`, `OrderCreatorRelation`, etc.
// ...

type Order authz.ID

const OrderCreator RelationOrder = "creator"
const OrderCompany RelationOrder = "company"

const OrderWrite PermissionOrder = "write"

// Many other utility methods like `Order.CreateCreatorRelations`, `Order.CheckWrite`, etc.
// ...
```

## Installation

To install the dependencies, run:

```sh
go install -v github.com/danhtran94/authzed-codegen/cmd/authzed-codegen@latest
```

## Usage

To generate code, run:

```sh
authzed-codegen --output example/authzed example/schema.zed
```

## Features (check the `example` folder for more details)

- Create relationships and permissions stubs.
- Read relationships.
- Lookup permissions and relationships.
- Generate code for multiple languages (Go, ~~TypeScript, etc.~~).
- SpiceDB engine client.

## TODOs

- [ ] Support all schema features.

## Contributing

Contributions are welcome! Please open an issue or submit a pull request.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.