attribution

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package attribution captures immutable human/system and request provenance. It carries no authenticated model, credentials, permissions or database handle.

Index

Constants

View Source
const (
	MaxRouteMethodBytes = 16
	MaxRouteNameBytes   = 256
)
View Source
const MaxRequestIDBytes = 128
View Source
const MaxUserAgentBytes = 4096

Variables

This section is empty.

Functions

func SanitizeUserAgent

func SanitizeUserAgent(raw string) string

SanitizeUserAgent converts untrusted transport text into valid attribution metadata. Invalid UTF-8 becomes U+FFFD, control characters (including TAB, CR/LF, DEL and C1 controls) are removed, and the result is truncated at MaxUserAgentBytes on a rune boundary. It never fails: a user agent describes a request, it does not authorize or reject it.

func WithContext

func WithContext(ctx context.Context, origin Origin) (context.Context, error)

WithContext attaches a validated value snapshot and preserves the caller's cancellation. It does not install security credentials or authorization state.

func WithRoute

func WithRoute(ctx context.Context, route Route) (context.Context, error)

WithRoute attaches the matched route for the remainder of this request. It grants no authority and does not change the request's Origin.

Types

type GuardName

type GuardName string

GuardName is the serialized name of an authentication strategy, never a credential.

type Origin

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

Origin contains at most one model identity or system identity. Zero is valid anonymous provenance. It is metadata, never an authentication/authorization capability; security adapters must resolve their own concrete subject.

func FromContext

func FromContext(ctx context.Context) Origin

FromContext returns anonymous provenance when none is present. Later context derivation or model mutation cannot change a previously returned snapshot.

func (Origin) Format

func (Origin) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing attribution details.

func (Origin) Guard

func (o Origin) Guard() GuardName

Guard returns the origin's authentication guard name.

func (Origin) MarshalJSON

func (o Origin) MarshalJSON() ([]byte, error)

MarshalJSON validates and encodes the origin as JSON.

func (Origin) Model

func (o Origin) Model() (model.Identity, bool)

Model returns the model identity and whether it is present.

func (Origin) Request

func (o Origin) Request() Request

Request returns the origin's request metadata.

func (Origin) System

func (o Origin) System() SystemID

System returns the origin's system subject identifier.

func (*Origin) UnmarshalJSON

func (o *Origin) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes and validates JSON, replacing the receiver only on success.

func (Origin) Validate

func (o Origin) Validate() error

Validate checks subject exclusivity, semantic identifiers, and request metadata.

func (Origin) WithGuard

func (o Origin) WithGuard(guard GuardName) (Origin, error)

WithGuard captures only a strategy name. A system origin cannot use a guard.

func (Origin) WithIdentity

func (o Origin) WithIdentity(subject model.Identity) (Origin, error)

WithIdentity is the explicit restored-metadata boundary. Supplying a parsed identity grants no authority and does not authenticate the caller.

func (Origin) WithModel

func (o Origin) WithModel(subject model.Identifiable) (Origin, error)

WithModel captures the model's generated stored-key identity immediately. Mutating the original model later cannot change this origin. No lookup or custom read getter runs. A custom metadata adapter must be pure.

func (Origin) WithRequest

func (o Origin) WithRequest(request Request) (Origin, error)

WithRequest returns a copy with the supplied request metadata after validation.

func (Origin) WithSystem

func (o Origin) WithSystem(system SystemID) (Origin, error)

WithSystem replaces the model subject and clears authentication metadata.

type Request

type Request struct {
	ID        RequestID  `json:"id,omitzero"`
	IP        netip.Addr `json:"ip,omitzero"`
	UserAgent string     `json:"user_agent,omitzero"`
}

Request is a value snapshot of request metadata. HTTP adapters determine trusted client IPs before attribution is captured; this package trusts no headers.

func (Request) Validate

func (r Request) Validate() error

Validate checks request identifier and user agent text bounds and rejects scoped IP addresses.

type RequestID

type RequestID string

RequestID correlates work with its originating request. It is not a secret.

type Route

type Route struct {
	Method string
	Name   string
}

Route identifies the matched server endpoint of the current request by its method and declared route name. It is request-local context metadata: Origin serialization, queued jobs, events and outbox records do not carry it. Raw URLs, query strings and path values are deliberately excluded because they can contain credentials or personal data.

func RouteFromContext

func RouteFromContext(ctx context.Context) (Route, bool)

RouteFromContext returns the matched route, or false outside a routed request.

func (Route) Format

func (Route) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing route details.

func (Route) Validate

func (r Route) Validate() error

Validate checks the route's uppercase method and nonempty, bounded name.

type SystemID

type SystemID string

SystemID identifies infrastructure/domain automation, such as a named schedule.

Jump to

Keyboard shortcuts

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