auth

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

pkg/auth/jwt.go

Index

Constants

View Source
const (
	ActionCreate  = "create"
	ActionRead    = "read"
	ActionUpdate  = "update"
	ActionDelete  = "delete"
	ActionPublish = "publish"
)

Content actions checked against a role.

View Source
const (
	RoleAdmin  = "admin"
	RoleEditor = "editor"
	RoleViewer = "viewer"
	// RolePublic is what anonymous requests (no Authorization header) may
	// do. It is stored like a custom role, grants read actions only, and
	// cannot be given to accounts or tokens.
	RolePublic = "public"
)

Built-in roles. Their permissions are fixed; custom roles are stored.

View Source
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.

View Source
const TokenTTL = 72 * time.Hour

TokenTTL is how long an issued token stays valid.

Variables

ContentActions are the actions on collection items; singletons use read and update; the media library read, create, update and delete.

View Source
var ErrInvalidToken = errors.New("invalid token")

ErrInvalidToken is returned for any token that fails parsing or validation.

View Source
var PublicActions = []string{ActionRead}

PublicActions are the actions the public role may grant.

Functions

func Can added in v0.5.0

func Can(role, action, resource string) bool

Can reports whether role may perform action on resource: a collection name, SingletonResource(name) or ResourceMedia.

func CanSeeDrafts added in v0.5.0

func CanSeeDrafts(role, resource string) bool

CanSeeDrafts reports whether role sees draft content of a type: roles that can edit or publish it.

func DerivedKey added in v0.8.0

func DerivedKey(purpose string) []byte

DerivedKey returns a key for another use of the JWT secret (signing the state of a sign-in with a provider): tokens signed with it are not valid session tokens.

func GenerateJWT

func GenerateJWT(username, role string) (string, error)

func HasPermission

func HasPermission(role, action string) bool

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

func IsBuiltinRole(name string) bool

IsBuiltinRole reports whether name is admin, editor or viewer.

func IsPublicRole added in v0.6.0

func IsPublicRole(name string) bool

IsPublicRole reports whether name is the role of anonymous requests.

func IsSingletonResource added in v0.5.0

func IsSingletonResource(key string) (string, bool)

IsSingletonResource reports whether a key names a singleton, and which.

func RoleFromContext

func RoleFromContext(ctx context.Context) string

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

func SingletonResource(name string) string

SingletonResource is the permission key of a singleton.

func WithRole

func WithRole(ctx context.Context, role string) context.Context

WithRole returns a context carrying the authenticated user's role.

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	Role     string `json:"role"`

	jwt.RegisteredClaims
}

func ParseJWT

func ParseJWT(tokenStr string) (*Claims, error)

ParseJWT validates signature, algorithm (HS256 only) and expiry. It never returns nil claims together with a nil error.

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.

func (Permissions) Empty added in v0.6.0

func (p Permissions) Empty() bool

Empty reports whether the permissions grant nothing.

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).

Jump to

Keyboard shortcuts

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