Documentation
¶
Overview ¶
pkg/auth/jwt.go
Index ¶
- Constants
- Variables
- func Can(role, action, resource string) bool
- func CanSeeDrafts(role, resource string) bool
- func GenerateJWT(username, role string) (string, error)
- func HasPermission(role, action string) bool
- func InitializeJWT(secret string)
- func IsBuiltinRole(name string) bool
- func IsSingletonResource(key string) (string, bool)
- func RoleFromContext(ctx context.Context) string
- func SetRoleLookup(f RoleLookup)
- func SingletonResource(name string) string
- func WithRole(ctx context.Context, role string) context.Context
- type Claims
- type Permissions
- type RoleLookup
Constants ¶
const ( ActionCreate = "create" ActionRead = "read" ActionUpdate = "update" ActionDelete = "delete" ActionPublish = "publish" )
Content actions checked against a role.
const ( RoleAdmin = "admin" RoleEditor = "editor" RoleViewer = "viewer" )
Built-in roles. Their permissions are fixed; custom roles are stored.
const ( // ResourceMedia is the media library. ResourceMedia = "media" // AnyType stands for every content type a role has no specific entry for. AnyType = "*" )
Resources other than content types.
const TokenTTL = 72 * time.Hour
TokenTTL is how long an issued token stays valid.
Variables ¶
var ( ContentActions = []string{ActionRead, ActionCreate, ActionUpdate, ActionDelete, ActionPublish} SingletonActions = []string{ActionRead, ActionUpdate} MediaActions = []string{ActionRead, ActionCreate, ActionUpdate, ActionDelete} )
ContentActions are the actions on collection items; singletons use read and update; the media library read, create, update and delete.
var ErrInvalidToken = errors.New("invalid token")
ErrInvalidToken is returned for any token that fails parsing or validation.
Functions ¶
func Can ¶ added in v0.5.0
Can reports whether role may perform action on resource: a collection name, SingletonResource(name) or ResourceMedia.
func CanSeeDrafts ¶ added in v0.5.0
CanSeeDrafts reports whether role sees draft content of a type: roles that can edit or publish it.
func GenerateJWT ¶
func HasPermission ¶
HasPermission reports whether role may perform action on at least one content type. Prefer Can, which checks one type.
func InitializeJWT ¶
func InitializeJWT(secret string)
func IsBuiltinRole ¶ added in v0.5.0
IsBuiltinRole reports whether name is admin, editor or viewer.
func IsSingletonResource ¶ added in v0.5.0
IsSingletonResource reports whether a key names a singleton, and which.
func RoleFromContext ¶
RoleFromContext returns the role stored by WithRole, or "".
func SetRoleLookup ¶ added in v0.5.0
func SetRoleLookup(f RoleLookup)
SetRoleLookup installs how custom roles are found (the storage layer does it at startup).
func SingletonResource ¶ added in v0.5.0
SingletonResource is the permission key of a singleton.
Types ¶
type Claims ¶
type Claims struct {
Username string `json:"username"`
Role string `json:"role"`
jwt.RegisteredClaims
}
type Permissions ¶ added in v0.5.0
type Permissions struct {
Content map[string][]string `json:"content"`
Media []string `json:"media"`
}
Permissions is what a role may do. Content maps a collection name, "singleton:<name>" or "*" (every other type) to actions; Media lists the actions on the media library.
func PermissionsOf ¶ added in v0.5.0
func PermissionsOf(role string) Permissions
PermissionsOf returns a role's permissions: fixed for built-in roles, stored for custom ones, none for unknown roles.
func (Permissions) Allows ¶ added in v0.5.0
func (p Permissions) Allows(action, resource string) bool
Allows reports whether the permissions grant action on resource.
func (Permissions) AllowsAny ¶ added in v0.5.0
func (p Permissions) AllowsAny(action string) bool
AllowsAny reports whether action is granted on at least one content type.
type RoleLookup ¶ added in v0.5.0
type RoleLookup func(role string) (Permissions, bool)
RoleLookup returns a custom role's permissions (false: no such role).