Struct HmacSigner

Source
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§

Source§

impl HmacSigner

Source

pub fn new(key_info: KeyInfo, algo: Algorithm, encoder: Encoder) -> Self

Trait Implementations§

Source§

impl Clone for HmacSigner

Source§

fn clone(&self) -> HmacSigner

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HmacSigner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl SignerLogic for HmacSigner

Source§

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

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);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.