Skip to main content

Overview

The signature middleware verifies request signatures using HMAC or other algorithms, ensuring requests haven’t been tampered with and come from authorized sources. Use it when you need:
  • Webhook signature verification
  • API request authentication
  • Tamper detection

Installation

Quick Start

Configuration

Options

Examples

Basic HMAC Verification

GitHub Webhook

Stripe Webhook

With Timestamp Validation

Per-Client Secrets

Custom Signature Format

API Reference

Functions

Signature Algorithms

Creating Signatures (Client Side)

Technical Details

Implementation Architecture

The signature middleware implements HMAC-based request verification using Go’s crypto packages:
  • Algorithm Support: SHA-1 (legacy), SHA-256 (default), SHA-512
  • Encoding Formats: Hexadecimal (default) and Base64
  • Signature Verification: Constant-time comparison using hmac.Equal() to prevent timing attacks

Core Components

Middleware Flow

  1. Path & Method Filtering: Checks if request should skip validation based on configured paths and methods
  2. Signature Extraction: Retrieves signature from configured header and validates prefix if specified
  3. Payload Reading: Uses default body reader or custom PayloadGetter function
  4. HMAC Verification: Computes expected signature and performs constant-time comparison
  5. Context Storage: Stores verification result in request context for downstream handlers

Key Functions

  • New(secret): Creates middleware with default options (SHA-256, hex encoding, X-Signature header)
  • WithOptions(opts): Creates middleware with custom configuration
  • Sign(secret, algo, encoding, payload): Generates HMAC signature for given payload
  • GetInfo(c): Retrieves signature verification information from context
  • IsValid(c): Quick check if signature was valid

Provider-Specific Helpers

The middleware includes pre-configured functions for popular webhook providers:
  • GitHub: X-Hub-Signature-256 header with sha256= prefix
  • Stripe: Stripe-Signature header (simplified implementation)
  • Slack: Custom payload format combining version, timestamp, and body
  • Twilio: SHA-1 with Base64 encoding, URL + sorted form parameters
  • AWS: Signature Version 4 (simplified implementation)

Security Features

  • Constant-Time Comparison: Uses hmac.Equal() to prevent timing attacks
  • Body Preservation: Re-wraps request body after reading for downstream handlers
  • Configurable Skip Rules: Allows excluding specific paths and HTTP methods
  • Custom Error Handling: Supports custom error responses via ErrorHandler

Best Practices

  • Use SHA-256 or stronger algorithms
  • Include timestamp to prevent replay attacks
  • Use secure random secrets (32+ bytes)
  • Rotate secrets periodically
  • Log signature failures for security monitoring

Testing

The signature middleware includes comprehensive test coverage for all features and edge cases:
  • keyauth - API key authentication
  • jwt - JWT authentication
  • hmac - HMAC utilities