ctxutil

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ctxutil provides utility functions for storing and retrieving request-scoped values in context.Context.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoActor = errors.New("ctxutil: no actor in context")

ErrNoActor reports a context that has neither an Actor nor a Claims.UserID outside the reserved "system:" prefix.

Functions

func Actor added in v0.8.0

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

Actor returns the actor set by WithActor. Claims are not consulted. An empty value counts as absent.

func CausationID added in v0.8.0

func CausationID(ctx context.Context) (uuid.UUID, bool)

CausationID returns the causation EventID set by WithCausationID. uuid.Nil counts as absent.

func CorrelationID added in v0.8.0

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

CorrelationID returns the correlation ID set by WithCorrelationID. The request ID is not consulted. An empty value counts as absent.

func Permissions

func Permissions(ctx context.Context) ([]string, bool)

Permissions returns the permissions from the context claims.

func RequestID

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

RequestID returns the request ID from the context.

func ResolveActor added in v0.8.0

func ResolveActor(ctx context.Context) (string, error)

ResolveActor returns the Actor set by WithActor, falling back to Claims.UserID. It returns ErrNoActor when both are empty, or when the user ID carries the reserved "system:" prefix, so users cannot pose as library components.

func Roles

func Roles(ctx context.Context) ([]string, bool)

Roles returns the roles from the context claims.

func TenantID

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

TenantID returns the tenant ID set by WithTenantID, falling back to Claims.TenantID. Empty values count as absent.

func UserID

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

UserID returns the user ID from the context claims.

func WithActor added in v0.8.0

func WithActor(ctx context.Context, actor string) context.Context

WithActor returns a new context with the actor set: the user or component (system:<component>:<name>) on whose behalf work runs.

func WithCausationID added in v0.8.0

func WithCausationID(ctx context.Context, eventID uuid.UUID) context.Context

WithCausationID returns a new context with the causation ID set: the EventID of the event that caused the work running in this context.

func WithClaims

func WithClaims(ctx context.Context, claims Claims) context.Context

WithClaims returns a new context with the claims set.

func WithCorrelationID added in v0.8.0

func WithCorrelationID(ctx context.Context, id string) context.Context

WithCorrelationID returns a new context with the correlation ID set, shared by everything one request, scheduled run or inbox message caused.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID returns a new context with the request ID set.

func WithTenantID added in v0.8.0

func WithTenantID(ctx context.Context, id string) context.Context

WithTenantID returns a new context with the tenant ID set explicitly. An explicit tenant ID takes precedence over Claims.TenantID.

Types

type Claims

type Claims struct {
	UserID      string
	TenantID    string
	Roles       []string
	Permissions []string
}

Claims holds JWT-related identity information.

func GetClaims

func GetClaims(ctx context.Context) (Claims, bool)

GetClaims returns the claims from the context.

Jump to

Keyboard shortcuts

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