request

package
v0.0.0-...-4391913 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package request reads what a handler needs out of an API Gateway request that typed binding does not cover: the claims API Gateway's authorizer verified (the JWT authorizer of an HTTP API, payload format 2.0, or the Cognito user pool authorizer of a REST API, payload format 1.0), the caller's IP behind trusted proxies, comma-separated id lists.

Path, query, header and body values are bound and validated by the typed operations of the Event Handler router (github.com/alessandrolattao/powertools-lambda-go/eventhandler), whose context the claim functions read:

userID, err := request.UserID(ctx)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClaimsNotFound is returned when the request carries no JWT claims.
	ErrClaimsNotFound = errors.New("claims not found in JWT")

	// ErrClaimNotFound is returned when a specific claim is missing from the JWT.
	ErrClaimNotFound = errors.New("not found in JWT claims")

	// ErrUserIDNotFound is returned when the JWT has no userId claim.
	ErrUserIDNotFound = errors.New("userId claim not found in JWT")

	// ErrUserIDInvalid is returned when the userId claim is not a valid UUID.
	ErrUserIDInvalid = errors.New("userId claim is not a valid UUID")
)
View Source
var ErrTooManyCSVIDs = errors.New("too many csv ids")

ErrTooManyCSVIDs is returned when the input carries more than max ids.

Functions

func Claim

func Claim(ctx context.Context, name string) (string, error)

Claim returns one claim of the request ctx belongs to.

func Claims

func Claims(ctx context.Context) (map[string]string, error)

Claims returns the claims API Gateway's authorizer verified for the request ctx belongs to (a typed operation's context, r.Context() in a plain handler, or the context of a streaming handler): the JWT authorizer's behind an HTTP API, the Cognito user pool authorizer's behind a REST API. Every value is a string, as payload format 2.0 delivers them.

func ClientIP

func ClientIP(r *events.APIGatewayV2HTTPRequest, trustedProxies int) string

ClientIP returns the address of the client that called an API Gateway HTTP API (payload format 2.0).

With trustedProxies 0 it is requestContext.http.sourceIp: the peer API Gateway itself saw, which the client cannot forge. The first entry of X-Forwarded-For is never used, since the client writes it.

When trustedProxies proxies of yours sit in front of API Gateway (CloudFront is one), sourceIp is the last of them and each of them appended to X-Forwarded-For the address it saw, so the client is the trustedProxies-th entry counting from the right (after dropping the sourceIp API Gateway may append itself). This holds only if API Gateway cannot be reached around those proxies: a caller that goes straight to it can still choose that entry.

Behind a REST API, Container.ClientIP applies the same rule to requestContext.identity.sourceIp.

func ParseCSVIDs

func ParseCSVIDs[T any](raw string, maxIDs int, parse func(string) (T, error)) ([]T, error)

ParseCSVIDs splits a comma-separated id list, trims each token, skips empties, enforces the cap of maxIDs ids, and parses each surviving token via parse. Empty tokens do not count towards the cap. It does NOT dedup (callers rely on `= ANY($1)`, where dups are harmless) and does NOT decide the empty-result policy: the caller inspects len(result).

func UserID

func UserID(ctx context.Context) (uuid.UUID, error)

UserID returns the internal user id, the custom userId claim, of the request ctx belongs to.

Types

type InvalidCSVIDError

type InvalidCSVIDError struct {
	Value string
	Err   error
}

InvalidCSVIDError carries the offending token, so a caller can put it in the details of its BadRequest, and the parse error behind it.

func (*InvalidCSVIDError) Error

func (e *InvalidCSVIDError) Error() string

Error implements the error interface.

func (*InvalidCSVIDError) Unwrap

func (e *InvalidCSVIDError) Unwrap() error

Unwrap returns the parse error.

Jump to

Keyboard shortcuts

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