docs.rs failed to build nonce-auth-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build:
nonce-auth-0.6.3
Nonce Auth
A Rust-based secure nonce authentication library that provides one-time token (nonce) generation, signing, and verification functionality to effectively prevent replay attacks.
Features
- 🔐 HMAC-SHA256 Signing - Cryptographic signing of requests using shared secrets
- ⏰ Timestamp Window Validation - Prevents replay attacks from expired requests
- 🔑 One-time Nonce - Ensures each nonce can only be used once
- 💾 SQLite Persistence - Automatic nonce storage and cleanup management
- 🎯 Context Isolation - Support for nonce isolation across different business scenarios
- 🚀 Async Support - Fully asynchronous API design
- 🛡️ Security Protection - Constant-time comparison to prevent timing attacks
- 📦 Client-Server Separation - Clean separation of client and server responsibilities
Architecture
Client-Server Separation Design
The library provides two independent managers for clear separation of responsibilities:
NonceClient - Client-side Manager
- Responsible for generating signed requests
- No database dependencies required
- Lightweight design suitable for client-side use
NonceServer - Server-side Manager
- Responsible for verifying signed requests
- Manages nonce storage and cleanup
- Includes timestamp validation and replay attack prevention
NonceManager - Unified Manager (Backward Compatible)
- Contains both client and server functionality
- Suitable for monolithic applications or simple scenarios
Parameter Explanation
default_ttl: Nonce time-to-live, representing the duration from generation to expiration, defaults to 5 minutestime_window: Timestamp validation window, defaults to 1 minute
Both work together to prevent replay attacks.
Important Notes
- The server uses local SQLite for nonce persistence, please consider using with connection sticky policies
Quick Start
Add Dependencies
[]
= "0.1.0"
= { = "1", = ["full"] }
= { = "1.0", = ["derive"] }
= "1.0"
= "0.3"
Complete Client-Server Example
JavaScript Client
// client.js
import crypto from 'crypto';
// Usage example
;
Rust Server
// server.rs
use NonceServer;
use ;
use Duration;
use Filter;
async
async
Simple Usage (Client-Server Separation)
use Duration;
use ;
async
Example Authentication Flow Sequence Diagram
sequenceDiagram
participant JSClient as JavaScript Client
participant RustServer as Rust Server
participant DB as SQLite Database
Note over JSClient, DB: Complete Authentication Flow
JSClient->>JSClient: 1. Generate random secret
JSClient->>JSClient: 2. Generate UUID nonce
JSClient->>JSClient: 3. Create timestamp
JSClient->>JSClient: 4. Sign (timestamp + nonce) with HMAC-SHA256
JSClient->>RustServer: 5. POST /api/protected<br/>{data, auth: {timestamp, nonce, signature, secret}}
RustServer->>RustServer: 6. Create NonceServer with client's secret
RustServer->>RustServer: 7. Verify timestamp within window
alt Timestamp out of window
RustServer-->>JSClient: 401 Timestamp expired
end
RustServer->>RustServer: 8. Verify HMAC signature
alt Invalid signature
RustServer-->>JSClient: 401 Invalid signature
end
RustServer->>DB: 9. Check if nonce exists in context
alt Nonce already used
RustServer-->>JSClient: 401 Duplicate nonce
end
RustServer->>DB: 10. Store nonce with context
RustServer->>RustServer: 11. Process business logic
RustServer-->>JSClient: 200 Success response
Note over RustServer, DB: Background cleanup
RustServer->>DB: Cleanup expired nonces as needed
API Documentation
NonceClient
Constructor
secret: Secret key used for signing
Methods
Create Signed Request
Generates a complete request containing timestamp, nonce, and signature.
Sign
Generates HMAC-SHA256 signature for given timestamp and nonce.
NonceServer
Constructor
secret: Secret key used for verificationdefault_ttl: Default nonce expiration time (default: 5 minutes)time_window: Allowed time window for timestamp validation (default: 1 minute)
Methods
Verify Signed Request
pub async
Verifies request integrity including time window, nonce validity, and signature correctness.
Initialize Database
pub async
Creates necessary database tables and indexes.
SignedRequest
Error Types
Typical Use Cases
1. API Authentication
- Client first obtains authentication token
- Uses token to access protected APIs
- Each token can only be used once
2. Form Submission Protection
- Generate nonce when rendering form
- Verify nonce when submitting
- Prevents duplicate form submissions
3. Microservice Authentication
- Service A generates nonce for requests
- Service B verifies requests from Service A
- Ensures request uniqueness and authenticity
4. Session-based Authentication
- Client generates random secret per session
- Server verifies requests using client's secret
- Provides stateless authentication
Security Features
Replay Attack Prevention
- Time Window Limitation: Only accepts requests within specified time window
- One-time Nonce: Each nonce is deleted after verification, ensuring no reuse
- Context Isolation: Nonces from different business scenarios are isolated
Timing Attack Prevention
- Uses constant-time comparison algorithms for signature verification
Cryptographic Strength
- Uses HMAC-SHA256 algorithm to ensure signature integrity and authenticity
- Supports custom secret key lengths
Performance Optimization
- Automatic background cleanup of expired nonce records
- Database index optimization for query performance
- Asynchronous design supports high-concurrency scenarios
Dependencies
hmac- HMAC signingsha2- SHA256 hashingturbosql- SQLite ORMuuid- UUID generationserde- Serialization supporttokio- Async runtimethiserror- Error handling
License
MIT OR Apache-2.0
Contributing
Issues and Pull Requests are welcome!