Struct MultiSerializer

Source
pub struct MultiSerializer<PrimarySerializer> { /* private fields */ }
Expand description

The MultiSerializer provides the ability to sign values with a given serializer, but also try a series of fallback serializers. This is useful if you are rotating keys, and want to sign things using a new key, but allow an old serializer to unsign values.

§Exmaple

use itsdangerous::*;

let primary = serializer_with_signer(default_builder("new key").build(), URLSafeEncoding);
let fallback = serializer_with_signer(default_builder("old key").build(), URLSafeEncoding);

let signed_with_new_key = primary.sign(&"Signed with new key".to_owned()).unwrap();
let signed_with_old_key = fallback.sign(&"Signed with old key".to_owned()).unwrap();

let multi = MultiSerializer::new(primary).add_fallback(fallback);

assert_eq!(multi.unsign::<String>(&signed_with_new_key).unwrap(), "Signed with new key");
assert_eq!(multi.unsign::<String>(&signed_with_old_key).unwrap(), "Signed with old key");

Implementations§

Source§

impl<PrimarySerializer> MultiSerializer<PrimarySerializer>
where PrimarySerializer: Serializer,

Source

pub fn new(primary_serializer: PrimarySerializer) -> Self

Constructs a new MultiSerializer with a given Serializer as the primary serializer. The primary serializer is the one that will be used to sign values, and the first serializer that will be attempted while trying to unsign.

§Remarks

If the primary serializer is used to unsign a value, no dynamic dispatch takes place. That is to say, the MultiSerializer is its fastest when only the primary serializer is required to unsign a value, and when signing a value, it is a zero-cost abstraction.

Source

pub fn add_fallback<FallbackSerializer>( self, fallback_serializer: FallbackSerializer, ) -> Self
where FallbackSerializer: UnsignToString + 'static,

Adds a Serializer to as a fallback, that will be attempted to be used to unsign a value if the primary serializer fails to unsign a value.

§Remarks

Fallback serializers are attempted in the order they are added. For optimal performance when using fallbacks, add them in the order they will probably be used. Meaning, if you have a 2 fallbacks, consider adding the one you expect to be sucecessful before the other.

Trait Implementations§

Source§

impl<PrimarySerializer> Serializer for MultiSerializer<PrimarySerializer>
where PrimarySerializer: Serializer,

Source§

fn sign<T: Serialize>(&self, value: &T) -> Result<String>

Source§

fn unsign<'a, T: DeserializeOwned>( &'a self, value: &'a str, ) -> Result<T, BadSignature<'a>>

Auto Trait Implementations§

§

impl<PrimarySerializer> Freeze for MultiSerializer<PrimarySerializer>
where PrimarySerializer: Freeze,

§

impl<PrimarySerializer> !RefUnwindSafe for MultiSerializer<PrimarySerializer>

§

impl<PrimarySerializer> !Send for MultiSerializer<PrimarySerializer>

§

impl<PrimarySerializer> !Sync for MultiSerializer<PrimarySerializer>

§

impl<PrimarySerializer> Unpin for MultiSerializer<PrimarySerializer>
where PrimarySerializer: Unpin,

§

impl<PrimarySerializer> !UnwindSafe for MultiSerializer<PrimarySerializer>

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.