authctx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package authctx provides type-safe context propagation for authentication claims.

It uses Go generics so each project can store and retrieve its own claims type without gokit knowing about the specific fields.

Usage:

// Store claims (typically in middleware)
ctx = authctx.Set(ctx, myClaims)

// Retrieve claims (in handlers)
claims, ok := authctx.Get[*MyClaims](ctx)
claims := authctx.MustGet[*MyClaims](ctx) // panics if missing

Index

Constants

This section is empty.

Variables

View Source
var ErrNoClaims = errors.New("authctx: no claims in context")

ErrNoClaims is returned when claims are not found in the context.

Functions

func Get

func Get[T any](ctx context.Context) (T, bool)

Get retrieves typed authentication claims from the context. Returns the claims and true if found and of the correct type, or zero value and false otherwise.

func GetOrError

func GetOrError[T any](ctx context.Context) (T, error)

GetOrError retrieves typed claims from the context. Returns ErrNoClaims if claims are missing or of the wrong type.

func MustGet

func MustGet[T any](ctx context.Context) T

MustGet retrieves typed authentication claims from the context. Panics if claims are missing or of the wrong type. Use in handlers where authentication middleware guarantees claims exist.

func Set

func Set(ctx context.Context, claims any) context.Context

Set stores authentication claims in the context. The claims can be any type — the project defines its own claims struct.

Types

This section is empty.

Jump to

Keyboard shortcuts

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