middleware

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: EUPL-1.2 Imports: 5 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware for automatic STMF decryption.

Index

Constants

View Source
const (
	// FormDataKey is the context key for the decrypted FormData
	FormDataKey contextKey = "stmf_form_data"

	// MetadataKey is the context key for the form metadata
	MetadataKey contextKey = "stmf_metadata"
)

Variables

This section is empty.

Functions

func GetFormData

func GetFormData(r *http.Request) *stmf.FormData

GetFormData retrieves the decrypted FormData from the request context

func GetMetadata

func GetMetadata(r *http.Request) map[string]string

GetMetadata retrieves the form metadata from the request context

func HasSTMFPayload

func HasSTMFPayload(r *http.Request, fieldName string) bool

HasSTMFPayload checks if the request contains a STMF payload

func Middleware

func Middleware(cfg Config) func(http.Handler) http.Handler

Middleware creates an HTTP middleware that decrypts STMF payloads. It looks for the STMF payload in the configured field name, decrypts it, and populates r.Form with the decrypted fields.

func Simple

func Simple(privateKey []byte) func(http.Handler) http.Handler

Simple creates a simple middleware with just a private key

func SimpleBase64

func SimpleBase64(privateKeyB64 string) (func(http.Handler) http.Handler, error)

SimpleBase64 creates a simple middleware with a base64-encoded private key

Types

type Config

type Config struct {
	// PrivateKey is the server's X25519 private key (32 bytes)
	PrivateKey []byte

	// FieldName is the form field name containing the STMF payload
	// Defaults to "_stmf_payload" if empty
	FieldName string

	// OnError is called when decryption fails
	// If nil, returns 400 Bad Request
	OnError func(w http.ResponseWriter, r *http.Request, err error)

	// OnMissingPayload is called when the STMF field is not present
	// If nil, the request passes through unchanged
	OnMissingPayload func(w http.ResponseWriter, r *http.Request)

	// PopulateForm controls whether decrypted fields are added to r.Form
	// Defaults to true
	PopulateForm *bool
}

Config holds the middleware configuration

func DefaultConfig

func DefaultConfig(privateKey []byte) Config

DefaultConfig returns a Config with default values

Jump to

Keyboard shortcuts

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