Skip to content

Permify/permify-go

Repository files navigation

Permify logo
Permify Golang Client

GitHub package.json version  Permify Licence  Permify Discord Channel 

Installation

go get github.com/Permify/permify-go/v1

How to use

Import Permify.

import permify "github.com/Permify/permify-go/v1"

Initialize the new Permify client.

import permify "github.com/Permify/permify-go/v1"

// generate new client
client, err = permify.NewClient(
    Config{
	    Endpoint: `localhost:3478`,
    },
    grpc.WithTransportCredentials(insecure.NewCredentials()),
)

Create a new tenant

ct, err := client.Tenancy.Create(context.Background(), &v1.TenantCreateRequest{
    Id:   "t1",
    Name: "tenant 1",
})

Write Schema

sr, err: = client.Schema.Write(context.Background(), &v1.SchemaWriteRequest {
    TenantId: "t1",
    Schema: `
        entity user {}
            
        entity document {
    
        relation viewer @user
        action view = viewer
    }`,
})

Write Relationships

rr, err: = client.Relationship.Write(context.Background(), & v1.RelationshipWriteRequest {
    TenantId: "t1",
    Metadata: & v1.RelationshipWriteRequestMetadata {
        SchemaVersion: sr.SchemaVersion, // sr --> schema write response
    },
    Tuples: [] * v1.Tuple {
        {
            Entity: & v1.Entity {
                Type: "document",
                Id: "1",
            },
            Relation: "viewer",
            Subject: & v1.Subject {
                Type: "user",
                Id: "1",
            },
        }, {
            Entity: & v1.Entity {
                Type: "document",
                Id: "3",
            },
            Relation: "viewer",
            Subject: & v1.Subject {
                Type: "user",
                Id: "1",
            },
        }
    },
})

Check

cr, err: = client.Permission.Check(context.Background(), & v1.PermissionCheckRequest {
    TenantId: "t1",
	Metadata: & v1.PermissionCheckRequestMetadata {
        SnapToken: rr.SnapToken, // rr --> relationship write response
        SchemaVersion: sr.SchemaVersion, // sr --> schema write response
        Depth: 50,
    },
    Entity: & v1.Entity {
        Type: "document",
        Id: "1",
    },
    Permission: "view",
    Subject: & v1.Subject {
        Type: "user",
        Id: "3",
    },

    if (cr.can === PermissionCheckResponse_Result.RESULT_ALLOWED) {
        // RESULT_ALLOWED
    } else {
        // RESULT_DENIED
    }
})

Streaming Calls

str, err: = client.Permission.LookupEntityStream(context.Background(), & v1.PermissionLookupEntityRequest {
    TenantId: "t1",
	Metadata: & v1.PermissionLookupEntityRequestMetadata {
        SnapToken: rr.SnapToken, // rr --> relationship write response
        SchemaVersion: sr.SchemaVersion, // sr --> schema write response
        Depth: 50,
    },
    EntityType: "document",
    Permission: "view",
    Subject: & v1.Subject {
        Type: "user",
        Id: "1",
    },
})

// handle stream response
for {
    res, err: = str.Recv()

    if err == io.EOF {
        break
    }

    // res.EntityId
}

Permify is an open-source authorization service for creating and maintaining fine-grained authorizations across your individual applications and services.

Community & Support

Join our Discord channel for issues, feature requests, feedbacks or anything else. We love to talk about authorization and access control ❤️

permify | Discord permify | Twitter permify | Linkedin