authorization

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotServiceActor = apperror.Authorization("This operation requires a service actor.")
	ErrNotAdminActor   = apperror.Authorization("This operation requires an admin actor.")
)

Module provides the TokenAuthorizer as the Authorizer contract.

Functions

func ContextWithActor

func ContextWithActor(ctx context.Context, a *Actor) context.Context

ContextWithActor returns a new context with the given Actor attached.

func ContextWithUser

func ContextWithUser[U any](ctx context.Context, u *U) context.Context

ContextWithUser returns a new context carrying the service's user entity. The auth middleware sets it after authentication; handlers read it with UserFromContext.

func UserFromContext

func UserFromContext[U any](ctx context.Context) *U

UserFromContext returns the service's user entity from the context, or nil if none is present (e.g. service-account requests, or unauthenticated ones).

Types

type Actor

type Actor struct {
	ID          uuid.UUID
	Type        ActorType
	ClientID    string
	Scopes      []string
	Permissions []string
	IsAdmin     bool
	Locale      string
}

Actor is the authenticated caller's security context passed into use cases. Populated by the interface layer (HTTP, Console, gRPC, etc.) after authentication.

Type distinguishes human users from automated service callers.

Scopes define what the credential is permitted to do. A standard interactive login carries broad default scopes (full access to own resources). A restricted credential carries only explicitly granted scopes.

Permissions define what the actor itself is permitted to do, regardless of the credential. Populated for user actors from the identity provider. Always empty for service actors.

IsAdmin is true when the actor holds the service-wide admin realm role. Locale is the user's preferred locale from the JWT locale claim (e.g. "en-US"). Empty for service actors.

func ActorFromContext

func ActorFromContext(ctx context.Context) *Actor

ActorFromContext retrieves the authenticated Actor from the context, or returns nil if not present.

type ActorType

type ActorType string

ActorType represents the type of the authenticated actor.

const (
	ActorTypeUser    ActorType = "user"
	ActorTypeService ActorType = "service"
)

type Authorizer

type Authorizer interface {
	Authorize(actor *Actor, permissions ...string) error
}

Authorizer handles application layer authorization: it checks whether the given actor holds all the required permissions.

type TokenAuthorizer

type TokenAuthorizer struct{}

TokenAuthorizer authorizes tokens based on their scopes and permissions.

Permissions are fully-qualified, prefixed with the owning service: "assets:assets.write", "notifications:preferences.read". A scope or permission entry matches when it equals the permission itself, its admin-prefixed form ("admin:<permission>"), or a service-level wildcard ("<service>:*" / "admin:<service>:*").

func NewTokenAuthorizer

func NewTokenAuthorizer() *TokenAuthorizer

func (*TokenAuthorizer) Authorize

func (a *TokenAuthorizer) Authorize(actor *Actor, permissions ...string) error

Jump to

Keyboard shortcuts

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