auth

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 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.

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
}

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 (Principal) Has

func (p Principal) Has(perm string) bool

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

Jump to

Keyboard shortcuts

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