Help us shape the future of Go! Take the 2026 Go Developer Survey

scope

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package scope provides context-based scope extraction for audit events and queries.

When running inside Forge, it extracts AppID, TenantID, UserID, and IP from forge.Scope and authsome.User in the context. When running standalone, it gracefully handles missing scope values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyToAggregateQuery

func ApplyToAggregateQuery(ctx context.Context, q *audit.AggregateQuery)

ApplyToAggregateQuery enforces scope on an aggregate query.

func ApplyToCountQuery

func ApplyToCountQuery(ctx context.Context, q *audit.CountQuery)

ApplyToCountQuery enforces scope on a count query.

func ApplyToEvent

func ApplyToEvent(ctx context.Context, event *audit.Event)

ApplyToEvent sets scope fields on an event from context. Fields already set on the event are not overwritten.

func ApplyToQuery

func ApplyToQuery(ctx context.Context, q *audit.Query)

ApplyToQuery enforces scope on a query. For non-platform callers (those with a TenantID in context), the query's TenantID is forcibly set to the caller's tenant — preventing cross-tenant data access.

func ParseTrustedProxies

func ParseTrustedProxies(entries []string) ([]netip.Prefix, error)

ParseTrustedProxies converts CIDR blocks or bare addresses into prefixes for FromRequestWithProxies.

trusted, err := scope.ParseTrustedProxies([]string{"10.0.0.0/8", "192.168.1.1"})

func WithAppID

func WithAppID(ctx context.Context, appID string) context.Context

WithAppID returns a context with the given app ID.

func WithIP

func WithIP(ctx context.Context, ip string) context.Context

WithIP returns a context with the given IP address.

func WithInfo

func WithInfo(ctx context.Context, info Info) context.Context

WithInfo returns a context with all scope info set at once.

func WithRequestID

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

WithRequestID returns a context with the given request correlation id.

func WithTenantID

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

WithTenantID returns a context with the given tenant ID.

func WithUserAgent

func WithUserAgent(ctx context.Context, ua string) context.Context

WithUserAgent returns a context with the given User-Agent string.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a context with the given user ID.

Types

type Info

type Info struct {
	AppID     string
	TenantID  string
	UserID    string
	IP        string
	UserAgent string
	RequestID string
}

Info holds extracted scope information from the context.

func FromContext

func FromContext(ctx context.Context) Info

FromContext extracts scope information from the context. It checks for scope values set via With* functions. Returns zero Info if no scope is present (standalone mode).

func FromRequest

func FromRequest(r *http.Request) Info

FromRequest extracts the client IP from an HTTP request and merges it with any existing scope in the request's context.

Proxy headers are ignored: the IP comes from the connection's peer address. X-Forwarded-For and X-Real-IP are set by whoever is talking to you, so trusting them lets any caller choose the address written into the audit trail. If your service really sits behind a proxy, use FromRequestWithProxies with that proxy's address range.

func FromRequestWithProxies

func FromRequestWithProxies(r *http.Request, trusted []netip.Prefix) Info

FromRequestWithProxies is FromRequest but honours X-Forwarded-For and X-Real-IP when the request's peer is one of the trusted proxies.

With a trusted peer, the client-most entry of X-Forwarded-For is used. Build trusted from your load balancer's ranges with ParseTrustedProxies; passing nil disables header trust entirely.

Jump to

Keyboard shortcuts

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