Documentation
¶
Overview ¶
Package security is the root module for the go-security enterprise security framework.
It is a collection of independently importable sub-packages that together cover the most common application-security concerns of modern HTTP services, aligned with the OWASP Application Security Verification Standard (ASVS v4.0) and the OWASP Top 10 (2021).
Sub-packages ¶
- jwt — JSON Web Token issuance, verification & validation (JWS)
- oauth2 — OAuth 2.1 / OIDC client helpers (auth-code, PKCE, refresh)
- apikey — API key generation, hashing, rotation & verification
- hmac — HMAC-SHA256/384/512 with constant-time comparison
- rbac — Role-Based Access Control engine with role hierarchy
- permission — Permission & ABAC engine with wildcard & deny-by-default
- password — Password hashing (argon2id, bcrypt, scrypt) & policy
- csrf — Double-submit cookie CSRF protection (SameSite aware)
- cors — Configurable CORS middleware (OWASP compliant)
- ratelimit — Token-bucket & sliding-window rate limiters
- headers — OWASP secure HTTP response headers middleware
- ipwhitelist — CIDR-based IP allow/deny lists with trusted-proxy handling
- signature — HMAC/RSA request signing with replay protection
- jwks — Remote JWKS fetcher with caching & auto-refresh
- oidc — OpenID Connect discovery & ID token verification
- session — Session management with pluggable store (memory, Redis)
- totp — TOTP/HOTP 2FA (RFC 6238/4226)
- cookie — AES-GCM encrypted secure cookies
- encrypt — AES-256-GCM encryption with key rotation
Design Principles ¶
- Fail-secure: when in doubt, deny / reject
- Context-first: all I/O operations accept context.Context
- Functional options for configurable APIs
- Immutable after construction where possible
- Constant-time comparisons for all secret comparisons
- No sensitive data in errors or logs
- Accept interfaces, return structs
Quick Start ¶
import (
"github.com/natifdevelopment/go-security/jwt"
"github.com/natifdevelopment/go-security/oidc"
"github.com/natifdevelopment/go-security/session"
"github.com/natifdevelopment/go-security/password"
"github.com/natifdevelopment/go-security/rbac"
)
See each sub-package's doc.go for detailed usage examples.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apikey provides generation, verification and rotation of opaque API keys with constant-time comparison and optional HMAC-SHA256 peppering.
|
Package apikey provides generation, verification and rotation of opaque API keys with constant-time comparison and optional HMAC-SHA256 peppering. |
|
Package cookie provides secure HTTP cookie encoding and decoding with AES-GCM encryption and HMAC authentication.
|
Package cookie provides secure HTTP cookie encoding and decoding with AES-GCM encryption and HMAC authentication. |
|
Package cors provides an OWASP-compliant Cross-Origin Resource Sharing (CORS) middleware for net/http servers.
|
Package cors provides an OWASP-compliant Cross-Origin Resource Sharing (CORS) middleware for net/http servers. |
|
Package csrf implements Cross-Site Request Forgery (CSRF) protection for net/http servers using the Double-Submit Cookie pattern with SameSite cookie awareness and HMAC-signed tokens.
|
Package csrf implements Cross-Site Request Forgery (CSRF) protection for net/http servers using the Double-Submit Cookie pattern with SameSite cookie awareness and HMAC-signed tokens. |
|
Package encrypt provides AES-GCM authenticated encryption for data-at-rest protection, with support for multiple key sizes, key derivation from passphrases (PBKDF2-HMAC-SHA256), and transparent key rotation.
|
Package encrypt provides AES-GCM authenticated encryption for data-at-rest protection, with support for multiple key sizes, key derivation from passphrases (PBKDF2-HMAC-SHA256), and transparent key rotation. |
|
Package headers provides net/http middleware that applies the HTTP response headers recommended by the OWASP Secure Headers Project.
|
Package headers provides net/http middleware that applies the HTTP response headers recommended by the OWASP Secure Headers Project. |
|
Package hmac provides HMAC-SHA256/384/512 primitives with constant-time comparison, suitable for request signing, webhook verification and any scenario requiring message authentication.
|
Package hmac provides HMAC-SHA256/384/512 primitives with constant-time comparison, suitable for request signing, webhook verification and any scenario requiring message authentication. |
|
Package ipwhitelist provides IP-based allowlist and denylist filtering for HTTP services.
|
Package ipwhitelist provides IP-based allowlist and denylist filtering for HTTP services. |
|
Package jwks provides a caching fetcher for JSON Web Key Sets (JWKS) retrieved from a remote OpenID Connect / OAuth 2.1 provider.
|
Package jwks provides a caching fetcher for JSON Web Key Sets (JWKS) retrieved from a remote OpenID Connect / OAuth 2.1 provider. |
|
Package jwt provides JSON Web Token (JWS) issuance and verification with support for HMAC (HS256/384/512), RSA (RS256/384/512) and ECDSA (ES256/384/512) algorithms.
|
Package jwt provides JSON Web Token (JWS) issuance and verification with support for HMAC (HS256/384/512), RSA (RS256/384/512) and ECDSA (ES256/384/512) algorithms. |
|
Package oauth2 provides OAuth 2.1 / OpenID Connect client helpers built on top of golang.org/x/oauth2, adding PKCE (S256), ID-token verification and secure defaults.
|
Package oauth2 provides OAuth 2.1 / OpenID Connect client helpers built on top of golang.org/x/oauth2, adding PKCE (S256), ID-token verification and secure defaults. |
|
Package oidc provides OpenID Connect (OIDC) discovery and ID token verification helpers built on top of github.com/natifdevelopment/go-security/jwt and github.com/natifdevelopment/go-security/jwks.
|
Package oidc provides OpenID Connect (OIDC) discovery and ID token verification helpers built on top of github.com/natifdevelopment/go-security/jwt and github.com/natifdevelopment/go-security/jwks. |
|
Package password provides secure password hashing and policy validation for the go-security framework.
|
Package password provides secure password hashing and policy validation for the go-security framework. |
|
Package permission provides an Attribute-Based Access Control (ABAC) engine with wildcard resource matching, attribute conditions and deny-by-default semantics with explicit-deny-wins resolution.
|
Package permission provides an Attribute-Based Access Control (ABAC) engine with wildcard resource matching, attribute conditions and deny-by-default semantics with explicit-deny-wins resolution. |
|
Package ratelimit provides thread-safe, per-key rate limiters and an net/http middleware for protecting HTTP services from abusive traffic.
|
Package ratelimit provides thread-safe, per-key rate limiters and an net/http middleware for protecting HTTP services from abusive traffic. |
|
Package rbac provides a Role-Based Access Control engine with role hierarchy, dynamic role management and user-to-role assignment.
|
Package rbac provides a Role-Based Access Control engine with role hierarchy, dynamic role management and user-to-role assignment. |
|
Package session provides generic session management with pluggable storage backends (in-memory and Redis).
|
Package session provides generic session management with pluggable storage backends (in-memory and Redis). |
|
Package signature provides request signing and verification with replay protection for HTTP-style requests.
|
Package signature provides request signing and verification with replay protection for HTTP-style requests. |
|
Package totp implements time-based (RFC 6238) and counter-based (RFC 4226) one-time passwords for two-factor authentication (2FA).
|
Package totp implements time-based (RFC 6238) and counter-based (RFC 4226) one-time passwords for two-factor authentication (2FA). |
Click to show internal directories.
Click to hide internal directories.