auth

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package auth holds what the admin HTTP layer and the GraphQL resolvers share: argon2id password hashing, admin permissions and the signed-in principal on a request context.

Index

Constants

View Source
const (
	PermRead     = "admin:read"
	PermWrite    = "admin:write"
	PermPublish  = "admin:publish"
	PermAccounts = "admin:accounts"
	PermAll      = "admin:*"
)

Admin permissions. PermAll grants every permission.

Variables

View Source
var (
	ErrForbidden     = errors.New("admin: permission denied")
	ErrUnauthorized  = errors.New("admin: not signed in")
	ErrWeakPassword  = errors.New("admin: password must be at least 12 characters")
	ErrBadPermission = errors.New("admin: unknown permission")
	ErrBadUsername   = errors.New("admin: username must match [a-z0-9][a-z0-9_.-]{0,63}")
)

Errors.

View Source
var ErrOutOfScope = errors.New("admin: outside the token's datasources")

ErrOutOfScope: the principal is limited to other datasources.

Functions

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns a PHC-encoded argon2id hash.

func ValidatePassword

func ValidatePassword(password string) error

ValidatePassword enforces the minimum password length.

func ValidatePermissions

func ValidatePermissions(perms []string) error

ValidatePermissions rejects unknown or duplicate permissions.

func ValidateUsername

func ValidateUsername(name string) error

ValidateUsername checks the account name charset.

func VerifyPassword

func VerifyPassword(hash, password string) bool

VerifyPassword compares password with a PHC argon2id hash in constant time. An empty hash (unknown account) still spends one hash computation.

func WithPrincipal

func WithPrincipal(ctx context.Context, p Principal) context.Context

WithPrincipal attaches the signed-in administrator to ctx.

Types

type Principal

type Principal struct {
	Username    string
	Permissions []string
	// Datasources limits the principal to these datasources (a token's
	// scope); nil does not limit it.
	Datasources []string
}

Principal is the signed-in administrator of a request.

func PrincipalFrom

func PrincipalFrom(ctx context.Context) (Principal, bool)

PrincipalFrom returns the signed-in administrator.

func Require

func Require(ctx context.Context, perm string) (Principal, error)

Require returns the principal when it holds perm.

func RequireDatasource added in v0.1.13

func RequireDatasource(ctx context.Context, perm, datasource string) (Principal, error)

RequireDatasource returns the principal when it holds perm and may act on datasource.

func RequireUnlimited added in v0.1.13

func RequireUnlimited(ctx context.Context, perm string) (Principal, error)

RequireUnlimited returns the principal when it holds perm and is not limited to some datasources: what it reads spans every datasource.

func (Principal) Allows added in v0.1.13

func (p Principal) Allows(datasource string) bool

Allows reports whether the principal may act on datasource.

func (Principal) Has

func (p Principal) Has(perm string) bool

Has reports whether the principal holds perm (directly or via admin:*).

func (Principal) Limited added in v0.1.13

func (p Principal) Limited() bool

Limited reports whether the principal is limited to some datasources.

Jump to

Keyboard shortcuts

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