authorization

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package authorization provides authorization utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyScope

func ApplyScope(filters map[string]any, scope Scope) map[string]any

ApplyScope merges scope filters into an existing filter map.

func MaskFields

func MaskFields(input map[string]any, denied []string) map[string]any

MaskFields returns a new map without the denied keys.

func ProjectFields

func ProjectFields(input map[string]any, allowed []string) map[string]any

ProjectFields returns a new map with only the allowed keys.

func Require

func Require(ctx context.Context, auth ports.Authorizer, subject any, action string, resource any) error

Require calls the provided authorizer or returns an error when missing.

func RequireOwner

func RequireOwner(subjectID string, resource Owner) error

RequireOwner enforces ownership using an Owner interface.

func RequireOwnerID

func RequireOwnerID(subjectID, ownerID string) error

RequireOwnerID enforces resource ownership (BOLA).

func RequireTenant

func RequireTenant(tenantID string, resource TenantOwned) error

RequireTenant enforces tenant scoping.

func TenantIDFromContext

func TenantIDFromContext(ctx context.Context) (string, bool)

TenantIDFromContext returns the tenant identifier from context scope.

func WithScope

func WithScope(ctx context.Context, scope Scope) context.Context

WithScope stores the authorization scope in context.

Types

type AllowlistAuthorizer

type AllowlistAuthorizer struct {
	// contains filtered or unexported fields
}

AllowlistAuthorizer enforces explicit allow rules for actions. Missing rules default to forbidden.

func NewAllowlistAuthorizer

func NewAllowlistAuthorizer() *AllowlistAuthorizer

NewAllowlistAuthorizer creates an allowlist-based authorizer.

func (*AllowlistAuthorizer) Allow

func (a *AllowlistAuthorizer) Allow(action string, auth ports.Authorizer) error

Allow registers an authorizer for a specific action.

func (*AllowlistAuthorizer) AllowAny

func (a *AllowlistAuthorizer) AllowAny(action string) error

AllowAny permits the specified action without additional checks.

func (*AllowlistAuthorizer) AllowFunc

func (a *AllowlistAuthorizer) AllowFunc(action string, fn ports.AuthorizerFunc) error

AllowFunc registers an authorizer function for a specific action.

func (*AllowlistAuthorizer) Can

func (a *AllowlistAuthorizer) Can(ctx context.Context, subject any, action string, resource any) error

Can evaluates the allowlist entry for the given action.

type Owner

type Owner interface {
	OwnerID() string
}

Owner exposes ownership information for BOLA checks.

type PolicyAuthorizer

type PolicyAuthorizer struct {
	// contains filtered or unexported fields
}

PolicyAuthorizer adapts a policy engine to the Authorizer interface.

func NewPolicyAuthorizer

func NewPolicyAuthorizer(engine ports.PolicyEngine, opts PolicyAuthorizerOptions) *PolicyAuthorizer

NewPolicyAuthorizer creates an authorizer backed by a policy engine.

func (*PolicyAuthorizer) Can

func (p *PolicyAuthorizer) Can(ctx context.Context, subject any, action string, resource any) error

Can checks whether the subject can perform the action on the resource.

type PolicyAuthorizerOptions

type PolicyAuthorizerOptions struct {
	ContextProvider PolicyContextProvider
	DenyOnError     bool
}

PolicyAuthorizerOptions configures a policy-backed authorizer.

type PolicyContextProvider

type PolicyContextProvider func(ctx context.Context) any

PolicyContextProvider supplies contextual attributes for policy evaluation.

type Scope

type Scope struct {
	TenantID string
	UserID   string
}

Scope captures tenant/user scoping hints for repositories.

func ScopeFromContext

func ScopeFromContext(ctx context.Context) (Scope, bool)

ScopeFromContext retrieves the authorization scope from context.

func (Scope) Filters

func (s Scope) Filters() map[string]any

Filters builds a simple filter map for repository queries.

type TenantOwned

type TenantOwned interface {
	TenantID() string
}

TenantOwned exposes tenant ownership for scoping checks.

Jump to

Keyboard shortcuts

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