pub struct HmacSigner { /* private fields */ }
Expand description
A struct that holds the HMAC signer logic.
The HmacSigner
struct is used for signing and verifying data using HMAC signatures.
Implementations§
Trait Implementations§
Source§impl Clone for HmacSigner
impl Clone for HmacSigner
Source§fn clone(&self) -> HmacSigner
fn clone(&self) -> HmacSigner
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for HmacSigner
impl Debug for HmacSigner
Source§impl SignerLogic for HmacSigner
impl SignerLogic for HmacSigner
Source§fn unsign<T: for<'de> Deserialize<'de> + Data>(
&self,
token: &str,
) -> Result<T, Error>
fn unsign<T: for<'de> Deserialize<'de> + Data>( &self, token: &str, ) -> Result<T, Error>
Verifies the token and returns the deserialised data.
Before verifying the payload, the input token is split into two parts: the encoded payload and the signature.
If the token does not contain two parts, an InvalidInput
error is returned.
Afterwards, if the encoded payload is empty, an InvalidToken
error is returned even if the signature is valid.
The signature is then decoded using the provided encoder. If the decoding fails, an InvalidSignature
error is returned.
The encoded payload and the signature are then verified via HMAC. If the verification fails, an InvalidToken
error is returned.
If the encoded payload is valid, the payload is decoded and deserialised using serde.
If the payload’s expiration time is not provided, the deserialized data is returned.
Otherwise, the expiration time is checked against the current time. If the expiration time is earlier than the current time, a TokenExpired
error is returned.
Sample Usage:
use hmac_serialiser_rs::{HmacSigner, KeyInfo, Encoder, algorithm::Algorithm, errors::Error, SignerLogic, Data};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct UserData {
username: String,
}
impl Data for UserData {
fn get_exp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
None
}
}
let key_info = KeyInfo {
key: b"your_secret_key".to_vec(),
salt: b"your_salt".to_vec(),
info: vec![], // empty info
};
// Initialize the HMAC signer
let signer = HmacSigner::new(key_info, Algorithm::SHA256, Encoder::UrlSafe);
let result: Result<UserData, Error> = signer.unsign(&"token.signature");
// or
let result = signer.unsign::<UserData>(&"token.signature");
Source§fn sign<T: Serialize + Data>(&self, data: &T) -> String
fn sign<T: Serialize + Data>(&self, data: &T) -> String
Signs the data and returns the token which can be sent to the client.
Sample Usage:
use hmac_serialiser_rs::{HmacSigner, KeyInfo, Encoder, algorithm::Algorithm, errors::Error, SignerLogic, Data};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct UserData {
username: String,
}
impl Data for UserData {
fn get_exp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
None
}
}
let key_info = KeyInfo {
key: b"your_secret_key".to_vec(),
salt: b"your_salt".to_vec(),
info: b"auth-context".to_vec(),
};
// Initialize the HMAC signer
let signer = HmacSigner::new(key_info, Algorithm::SHA256, Encoder::UrlSafe);
let user = UserData { username: "user123".to_string() };
let result: String = signer.sign(&user);