casbin-go-client

module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2025 License: Apache-2.0

README

Go client for Casbin Server

Go Report Card GitHub Actions Go Reference Release Discord Sourcegraph

casbin-go-client is Golang's client for Casbin-Server. Casbin-Server is the Access Control as a Service (ACaaS) solution based on Casbin.

Installation

go mod init your-project
go mod tidy

Quick Start

First, start the casbin-server:

# Install casbin-server
go install github.com/casbin/casbin-server@latest

# Start the server
casbin-server

Then use the client:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/casbin/casbin-go-client/client"
    "google.golang.org/grpc"
)

func main() {
    // Create client with insecure connection
    c, err := client.NewClient(context.Background(), "127.0.0.1:50051", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }

    // Define RBAC model
    modelText := `
        [request_definition]
        r = sub, obj, act

        [policy_definition]
        p = sub, obj, act

        [role_definition]
        g = _, _

        [policy_effect]
        e = some(where (p.eft == allow))

        [matchers]
        m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
    `

    // Create enforcer
    enforcer, err := c.NewEnforcer(context.Background(), client.Config{
        ModelText: modelText,
    })
    if err != nil {
        log.Fatal(err)
    }

    // Add policy
    enforcer.AddPolicy(context.Background(), "alice", "data1", "read")

    // Check permission
    allowed, err := enforcer.Enforce(context.Background(), "alice", "data1", "read")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("alice can read data1: %v\n", allowed) // true
}

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Directories

Path Synopsis
Package client implements a Go client for casbin-server, and contains an implementation of an API similar to the casbin API.
Package client implements a Go client for casbin-server, and contains an implementation of an API similar to the casbin API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL