Documentation ¶
Overview ¶
Package can provides primitives for authorization. Authorization is the way it restricts what resource a given role and permissions. Simple primitives for RBAC (Role Based Access Control) are the building blocks for can. This package was inspired by https://github.com/CanCanCommunity/cancancan
Index ¶
- func Can(ctx context.Context, role Role, permission string, ability Ability, ...) bool
- func Compare[T Comparable](i, j T) func() bool
- func PermissionFromPath(r *http.Request) string
- type Ability
- type CanFn
- type Comparable
- type DiskPermission
- type DiskRole
- type DiskRoles
- type Permission
- type Role
- type Roles
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Can ¶
func Can(ctx context.Context, role Role, permission string, ability Ability, compare func() bool) bool
Can is the heart and soul of the can package. It can take a custom compare function to do various authorization checking
ctx - a standard ctx to pass to authorization. Useful for passing additional request specific data and canceling the can function call if it was signal to a remote authorization service.
role - a role structure that contains the role and permissions to check authorization on.
permission - defines the permission to check of a given object.
ability - defines the ability to check of a given object.
compare - a simple function to check request specific data. Things like if a user can update their own comments or the like.
returns a true or false if the role or permission is allowed.
func Compare ¶
func Compare[T Comparable](i, j T) func() bool
Compare is a helper function to easily satisfies the compare function in the main Can function
func PermissionFromPath ¶
PermissionFromPath uses the request path to build a permission that can be used to check authorization in the Can function. Uses the chi router context to build the permission.
r - a standard http request
returns - a string representation of a permission
Types ¶
type Ability ¶
type Ability int64
Ability provides typed constants for general resource control.
const ( // Read is for access to a given resource Read Ability = iota // Create is for creating a given resource Create // Update is for updating a given resource Update // Delete is for deleting a given resource Delete // All is read/create/update/delete for a give resource All // Skip is for skipping authorization lookups on a given resource. // Useful if for options style results and when authorization might be // handled later in a request chain. Skip // None is useful for signaling no access to given resource. Also useful for // error states None )
func BuildFromMethod ¶
BuildFromMethod uses standard Rest conventions to build a permission and ability from the request. Useful for implementing authorization middleware
method - a string representation of an HTTP verb. GET/POST/PUT, etc
returns - an ability
func StringToAbility ¶
StringToAbility converts a string to an ability type
s is a string to convert
returns an ability or -1 if the string is incorrect
type CanFn ¶
type CanFn func(ctx context.Context, role *Role, compare func() bool, permission string, ability Ability) bool
CanFn is a type for the implementing custom authorization functions.
type Comparable ¶
type Comparable interface { constraints.Ordered | bool }
type DiskPermission ¶
type DiskRole ¶
type DiskRole map[string]DiskPermission
diskRole is the private struct that represents how the roles are encoded in yaml to disk
type Permission ¶
type Permission struct { Abilities map[Ability]struct{} `json:"abilities" db:"abilities" yaml:"abilities"` Resource string `json:"resource" db:"resource" yaml:"resource"` }
Permission provides typed structure for general permissions or access to a given resource. This struct is easily embedded in other types to extend the permissions (see examples).
type Role ¶
type Role map[string]Permission
Role provides typed structure for general roles that enumerates a set of permissions. This struct is easily embedded in other types to extend the role (see examples).
type Roles ¶
func Config ¶
Config takes a per parsed config file and return a map of Roles. Useful if the config file is a different format than yaml or if the config file is parsed elsewhere. c - a set of disk roles
returns - a map of Roles
func OpenFile ¶
OpenFile takes a yaml file and returns a map of Roles filename - yaml encoded file for parsing
returns - a map of Roles and an error
func (Roles) UnmarshalYAML ¶
UnmarshalYAML implement the yaml Unmarshaler interface