hyperstack-auth
Authentication and authorization utilities for Hyperstack, providing JWT token handling, claims validation, and Ed25519-based key management for secure WebSocket and HTTP authentication.
Overview
hyperstack-auth provides a robust, production-ready authentication system designed specifically for Hyperstack deployments. It uses Ed25519 (EdDSA) for asymmetric cryptographic signing, offering superior security compared to traditional HMAC-based approaches.
Key Features
- Ed25519 Signatures: Asymmetric signing using the EdDSA algorithm for enhanced security
- JWT Token Support: Full JWT implementation with customizable session claims
- Key Rotation: JWKS (JSON Web Key Set) support for seamless key rotation
- Origin Binding: Optional defense-in-depth with origin validation
- IP Binding: Client IP validation for high-security scenarios
- Resource Limits: Built-in metering and rate limiting support
- Token Revocation: Support for token revocation lists
- Security Auditing: Structured audit logging for authentication events
- Multi-key Verification: Verify tokens against multiple signing keys
Installation
Add to your Cargo.toml:
[]
= "0.2"
Feature Flags
jwks(default): Enables JWKS fetching from URLs viareqwest
[]
= { = "0.2", = false } # Without JWKS
Usage
Basic Token Signing and Verification
use ;
// Generate a new Ed25519 key pair
let signing_key = generate;
let verifying_key = signing_key.verifying_key;
// Create a signer with your issuer
let signer = new;
// Build session claims
let claims = builder
.with_ttl // 5 minute session
.with_scope
.with_metering_key
.build;
// Sign the token
let token = signer.sign.unwrap;
// Verify the token
let verifier = new;
let auth_context = verifier.verify.unwrap;
println!;
Key Management
use ;
use Path;
// Generate and save keys to files
let = generate_and_save_keys.unwrap;
// Load keys from files
let signing_key = signing_key_from_file.unwrap;
let verifying_key = verifying_key_from_file.unwrap;
// Load from environment variables (base64-encoded)
set_var;
let signing_key = signing_key_from_env.unwrap;
Origin and IP Binding
Add defense-in-depth by binding tokens to specific origins or client IPs:
use ;
let signing_key = generate;
// Create origin-bound token
let claims = builder
.with_origin
.with_client_ip
.build;
let signer = new;
let token = signer.sign.unwrap;
// Verify with origin validation
let verifier = new
.with_origin_validation
.with_client_ip_validation;
// Must provide matching origin and IP
let context = verifier.verify.unwrap;
Resource Limits and Metering
use ;
let limits = Limits ;
let claims = builder
.with_limits
.with_key_class // or KeyClass::Secret
.with_plan
.build;
JWKS Key Rotation
use JwksVerifier;
// Fetch JWKS from a URL
let jwks = fetch_jwks
.await
.unwrap;
// Create verifier with JWKS support
let verifier = new;
// Tokens signed with any key in the JWKS can be verified
let context = verifier.verify.unwrap;
Multi-key Verification
Verify tokens against multiple keys (useful for zero-downtime rotation):
use ;
let verifier = builder
.with_key
.with_key
.build;
let context = verifier.verify.unwrap;
Security Audit Logging
use ;
// Log authentication events
let logger = new;
logger.log_event.await;
Security Considerations
- Key Storage: Store signing keys securely (e.g., in environment variables, AWS KMS, or HashiCorp Vault)
- Token TTL: Use short-lived tokens (default: 5 minutes) with refresh mechanisms
- Origin Binding: Enable origin validation for browser-based clients
- Rate Limiting: Implement rate limiting using the built-in metering support
License
This project is licensed under the terms specified in the LICENSE file.
See Also
- KEY_ROTATION_GUIDE.md - Guide for key rotation procedures
- SECURITY_AUDIT_LOGGING.md - Security audit logging documentation