pub struct AuthData {
pub timestamp: u64,
pub nonce: String,
pub signature: String,
}Expand description
Authentication data for nonce-based request verification.
This structure contains the cryptographic authentication information that is embedded within or sent alongside application requests. It is specifically designed for nonce-based authentication and replay attack prevention, not as a complete request structure.
§Purpose
AuthData represents only the authentication portion of a request:
- It does not contain application payload or business logic data
- It focuses solely on cryptographic verification and replay prevention
- It can be embedded in larger request structures or sent as headers
§Fields
timestamp: Unix timestamp (seconds since epoch) when the auth data was creatednonce: A unique identifier (typically UUID) that prevents request reusesignature: HMAC-SHA256 signature that can include various data fields
§Serialization
This struct implements Serialize and Deserialize for easy JSON/binary
serialization when sending authentication data over the network.
§Example
use nonce_auth::{NonceClient, AuthData};
use hmac::Mac;
let client = NonceClient::new(b"secret");
let auth_data: AuthData = client.create_auth_data(|mac, timestamp, nonce| {
mac.update(timestamp.as_bytes());
mac.update(nonce.as_bytes());
}).unwrap();
// Embed in a larger request structure
#[derive(serde::Serialize)]
struct ApiRequest {
payload: String,
auth: AuthData,
}
let request = ApiRequest {
payload: "application data".to_string(),
auth: auth_data,
};§Security Notes
- The timestamp prevents very old authentication attempts from being replayed
- The nonce ensures each authentication attempt is unique and can only be used once
- The signature proves the authentication data hasn’t been tampered with
- The signature algorithm is flexible and can include additional request data
Fields§
§timestamp: u64Unix timestamp (seconds since epoch) when this authentication data was created.
Used by the server to validate that the authentication attempt is within the acceptable time window and not too old.
nonce: StringA unique nonce value, typically a UUID string.
This value must be unique and is used to prevent the same authentication data from being processed multiple times.
signature: StringHMAC-SHA256 signature that can include various data fields.
The signature algorithm is flexible and can be customized to include timestamp, nonce, payload, HTTP method, path, or any other relevant data. This proves that the authentication data was created by someone who knows the shared secret and that the included data hasn’t been tampered with.