pub struct HmacSigner { /* private fields */ }Expand description
A struct that holds the HMAC signer logic.
The HmacSigner struct is used for signing and verifying the payload using HMAC signatures.
Implementations§
Source§impl HmacSigner
impl HmacSigner
Source§impl HmacSigner
impl HmacSigner
Sourcepub fn unsign<T: for<'de> Deserialize<'de> + Payload>(
&self,
token: &str,
) -> Result<T, Error>
pub fn unsign<T: for<'de> Deserialize<'de> + Payload>( &self, token: &str, ) -> Result<T, Error>
Verifies the token and returns the deserialised payload.
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 payload 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::{HmacSigner, KeyInfo, Encoder, algorithm::Algorithm, Error, Payload};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct UserData {
username: String,
}
impl Payload 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");Sourcepub fn sign<T: Serialize + Payload>(&self, payload: &T) -> String
pub fn sign<T: Serialize + Payload>(&self, payload: &T) -> String
Signs the payload and returns the token which can be sent to the client.
Sample Usage:
use hmac_serialiser::{HmacSigner, KeyInfo, Encoder, algorithm::Algorithm, Error, Payload};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct UserData {
username: String,
}
impl Payload 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);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 more