permify_go

package module
v4.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2022 License: MIT Imports: 16 Imported by: 0

README

Permify Go Library

GitHub last commit Twitter URL

Use Permify in server-side Go Projects.

Install

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

Usage

To get started, create the Permify client using your Workspace id and API private key:

import permify `github.com/Permify/permify-go/v4`

client := client.New("workspace_id", "private_token")
Create Group

This method creates a group entity in Permify.

response, err = client.Group.Create(group.Create{
    ID:   "group id",
    Name: "role name",
})
Create User

This method creates a user entity in Permify.

response, err := client.User.Create(user.Create{
    ID: "user id",
    Name: "name",
    Photo: "photo",
    RoleNames: []string{"role name"},
    GroupIDs: []string{"group id"},
    Attributes: map[string]interface{}{},
})
Create Resource

This method creates a resource entity in Permify.

response, err := client.Resource.Create(resource.Create{
    ID:      "resource id",
    Type:    "resource type",
	GroupID: "group id",
    Attributes: map[string]interface{}{},
}
Create Role

This method creates a role entity in Permify.

response, err = client.Role.Create(role.Create{
    Name:        "role name",
    Description: "description",
})
Create Rule

This method creates a rule entity in Permify.

Sample Rules
Is the user senior?
user.attributes.tenure > 8
Is the user manager?
"manager" in user.roles[_].guard_name
Is the user admin?
"admin" in user.roles[_].guard_name
Is the user the owner of the resource?
user.id == resource.attributes.owner_id
response, err = client.Rule.Create(rule.Create{
    Name:   "rule name",
    Conditions: []string{"condition"},
})
Create Action

This method creates an action entity in Permify.

response, err = client.Option.Create(option.Create{
    Alias:   "action name",
})
Create Policy

This method creates a policy entity in Permify.

response, err = client.Policy.Create(policy.Create{
    Name:   "policy name",
    ActionAlias: "action alias"
    Description: "policy description",
    AnyOf: true,
    RuleNames: []string{""},
})
IsAuthorized

This method returns a decision about whether the user is authorized for this action with the given parameters.

Parameters
  • PolicyName (mandatory)

Custom Permify Policy name.

  • ActionAlias (mandatory)

Alias of the Action.

  • UserID (mandatory)

Id of the User

  • ResourceID (optional)

Id of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.

  • ResourceType (optional)

Type or name of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.

  • CascadeCreate (optional)

Create policy if not exists.

response, err = client.User.IsAuthorized(user.IsAuthorized{
    PolicyName: "policy name",
    ActionAlias: "action alias",
    UserID: "user id",
    ResourceID: permify.String("resource id"),
    ResourceType: permify.String("resource type"),
    CascadeCreate: true,
})

Permify Documentation

See more

Documentation

Index

Constants

View Source
const (
	APIVersion = "v1"
	Version    = "v3"
)
View Source
const DefaultBatchSize = 250
View Source
const DefaultEndpoint = "https://papi.permify.co"
View Source
const DefaultInterval = 5 * time.Second

Variables

This section is empty.

Functions

func Alias

func Alias(b string) string

func Bool

func Bool(v bool) *bool

func Guard

func Guard(b string) string

func GuardArray

func GuardArray(b []string) (guardArray []string)

func HmacSha1

func HmacSha1(message string, secret string) string

func Pre

func Pre(x interface{}, y ...interface{})

Pre exit running project. @param interface{} @param ...interface{}

func String

func String(v string) *string

func StructToMap

func StructToMap(obj interface{}) (newMap map[string]interface{}, err error)

Types

type API

type API interface {
	Call(request Request, successValue interface{}) error
}

type APIValidationError

type APIValidationError struct {
	Errors map[string]interface{} `json:"errors"`
}

func (APIValidationError) Error

func (e APIValidationError) Error() string

type Action

type Action struct {
	Alias       string    `json:"alias"`
	WorkspaceID string    `json:"workspace_id"`
	CreatedAt   time.Time `json:"created_at"`
}

Action */

type AppInfo

type AppInfo struct {
	Name    string `json:"name,omitempty"`
	Version string `json:"version,omitempty"`
}

type Backend

type Backend struct {
	Configuration
	// contains filtered or unexported fields
}

func GetBackend

func GetBackend(workspaceID, privateKey string) *Backend

func GetBackendWithConfiguration

func GetBackendWithConfiguration(workspaceID, privateKey string, configuration Configuration) (client *Backend, err error)

func (*Backend) Call

func (b *Backend) Call(request Request, successValue interface{}) (err error)

type Configuration

type Configuration struct {
	Endpoint       string
	Interval       time.Duration
	Transport      http.RoundTripper
	DefaultContext *Context
	BatchSize      int

	Logger Logger
	// contains filtered or unexported fields
}

type Context

type Context struct {
	App       AppInfo     `json:"app,omitempty"`
	Library   LibraryInfo `json:"library,omitempty"`
	Locale    string      `json:"locale,omitempty"`
	Timezone  string      `json:"timezone,omitempty"`
	UserAgent string      `json:"userAgent,omitempty"`
}

type Group

type Group struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	WorkspaceID string    `json:"workspace_id"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Group */

type LibraryInfo

type LibraryInfo struct {
	Name    string `json:"name,omitempty"`
	Version string `json:"version,omitempty"`
}

type Logger

type Logger interface {
	Logf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

func StdLogger

func StdLogger(logger *log.Logger) Logger

type MessageResponse

type MessageResponse struct {
	Message string `json:"message"`
}

func (MessageResponse) Error

func (e MessageResponse) Error() string

type Policy

type Policy struct {
	Name        string    `json:"name"`
	GuardName   string    `json:"guard_name"`
	Description string    `json:"description"`
	Color       string    `json:"color"`
	AnyOf       bool      `json:"any_of"`
	Rules       []Rule    `json:"rules"`
	Action      *Action   `json:"action"`
	WorkspaceID string    `json:"workspace_id"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Policy */

type Request

type Request interface {
	GetMethod() string
	GetPath() string
	GetQueries() map[string][]string
	GetBody() map[string]interface{}
}

type Resource

type Resource struct {
	ID          string                 `json:"id"`
	Type        string                 `json:"type"`
	Attributes  map[string]interface{} `json:"attributes"`
	WorkspaceID string                 `json:"workspace_id"`
	Group       *Group                 `json:"group"`
	CreatedAt   time.Time              `json:"created_at"`
	UpdatedAt   time.Time              `json:"updated_at"`
}

Resource */

type ResponseDecoder

type ResponseDecoder interface {
	Decode(resp *http.Response, v interface{}) error
}

type Role

type Role struct {
	Name        string    `json:"name"`
	GuardName   string    `json:"guard_name"`
	Description string    `json:"description"`
	Color       string    `json:"color"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Role */

type Rule

type Rule struct {
	Name        string    `json:"name"`
	GuardName   string    `json:"guard_name"`
	Description string    `json:"description"`
	FailMessage string    `json:"fail_message"`
	Condition   string    `json:"condition"`
	WorkspaceID string    `json:"workspace_id"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Rule */

type User

type User struct {
	ID          string                 `json:"id"`
	Name        string                 `json:"name"`
	Photo       string                 `json:"photo"`
	Attributes  map[string]interface{} `json:"attributes"`
	WorkspaceID string                 `json:"workspace_id"`
	Roles       []Role                 `json:"roles"`
	Groups      []Group                `json:"groups"`
	CreatedAt   time.Time              `json:"created_at"`
	UpdatedAt   time.Time              `json:"updated_at"`
}

User */

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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