Skip to main content

KeyRotationStore

Struct KeyRotationStore 

Source
pub struct KeyRotationStore { /* private fields */ }
Expand description

Zero-downtime key rotation store.

Holds up to max_keys Ed25519 key pairs simultaneously. The latest added key is used for signing; all retained keys can verify tokens. During rotation:

  1. Call rotate(private_pem, public_pem) to add the new key.
  2. New tokens are signed with the new key immediately.
  3. Old tokens remain verifiable until their natural expiry.
  4. Call prune() to remove the oldest key once all old tokens have expired.

§Example

let mut store = KeyRotationStore::new(3);
store.add_key("v1", PRIVATE_PEM, PUBLIC_PEM)?;

// Later, on rotation:
store.rotate("v2", NEW_PRIVATE_PEM, NEW_PUBLIC_PEM)?;

Implementations§

Source§

impl KeyRotationStore

Source

pub fn new(max_keys: usize) -> Self

Create a new store. max_keys caps how many key versions are retained simultaneously (minimum 1, maximum 16).

Source

pub fn add_key( &self, kid: impl Into<String>, private_pem: &[u8], public_pem: &[u8], ) -> Result<()>

Load the initial key pair. kid is a human-readable version tag.

Source

pub fn rotate( &self, kid: impl Into<String>, private_pem: &[u8], public_pem: &[u8], ) -> Result<()>

Convenience alias — same as add_key but semantically signals rotation.

Source

pub fn prune_oldest(&self)

Drop the oldest key version (call after old tokens have expired).

Source

pub fn sign( &self, subject: Uuid, ttl_seconds: i64, extra: Value, ) -> Result<String>

Sign a JWT with the current (newest) key.

Source

pub fn verify(&self, token: &str) -> Result<Claims>

Verify a JWT against all retained key versions (newest first).

Source

pub fn key_count(&self) -> usize

Number of currently retained key versions.

Trait Implementations§

Source§

impl Clone for KeyRotationStore

Source§

fn clone(&self) -> Self

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

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more