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:
- Call
rotate(private_pem, public_pem)to add the new key. - New tokens are signed with the new key immediately.
- Old tokens remain verifiable until their natural expiry.
- 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
impl KeyRotationStore
Sourcepub fn new(max_keys: usize) -> Self
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).
Sourcepub fn add_key(
&self,
kid: impl Into<String>,
private_pem: &[u8],
public_pem: &[u8],
) -> Result<()>
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.
Sourcepub fn rotate(
&self,
kid: impl Into<String>,
private_pem: &[u8],
public_pem: &[u8],
) -> Result<()>
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.
Sourcepub fn prune_oldest(&self)
pub fn prune_oldest(&self)
Drop the oldest key version (call after old tokens have expired).
Sourcepub fn sign(
&self,
subject: Uuid,
ttl_seconds: i64,
extra: Value,
) -> Result<String>
pub fn sign( &self, subject: Uuid, ttl_seconds: i64, extra: Value, ) -> Result<String>
Sign a JWT with the current (newest) key.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for KeyRotationStore
impl RefUnwindSafe for KeyRotationStore
impl Send for KeyRotationStore
impl Sync for KeyRotationStore
impl Unpin for KeyRotationStore
impl UnsafeUnpin for KeyRotationStore
impl UnwindSafe for KeyRotationStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more