Skip to main content

EncryptedStorage

Struct EncryptedStorage 

Source
pub struct EncryptedStorage<T> { /* private fields */ }
Expand description

Wrapper for storage backends that adds encryption at rest to the generic key-value (store_kv/get_kv) layer.

Coverage note: this wraps store_kv/get_kv only. The dedicated store_token/store_session methods (and each backend’s own typed columns for them) are NOT touched by this wrapper – they delegate to inner unchanged. Encrypting those would need changes to the AuthStorage trait itself (their fields are typed, not opaque bytes), tracked separately. This wrapper covers everything that already goes through store_kv today: API keys, TOTP secrets, OAuth2 client registries, MFA codes, and most of the other KV-backed subsystems found in the storage audit (docs/STORAGE-AUDIT.md).

Implementations§

Source§

impl<T> EncryptedStorage<T>

Source

pub fn new( storage: T, encryption: StorageEncryption, allow_plaintext_reads: bool, allow_legacy_v0: bool, ) -> Self

allow_plaintext_reads should be true only transiently, while migrating an existing deployment’s plaintext data (see migrate_kv_to_encrypted) – it is what makes Self::get_kv tolerate a value that isn’t a valid envelope instead of erroring. Leaving it true permanently means anyone who can write to the backing store can overwrite an encrypted value with chosen plaintext (or corrupt one) and have it accepted silently, which defeats the point of encrypting at rest. Pass false for normal operation.

allow_legacy_v0 should likewise be true only transiently, while migrating pre-existing format-version-0 data: a v0 envelope has no AAD binding it to its own storage key, so with this true, a writer who knows one record’s v0 envelope can copy it onto a different record’s key and it still decrypts – the exact swap the current format’s AAD exists to prevent. This flag is independent of migrate_kv_to_encrypted’s own, separate accept_legacy_v0 parameter (that one gates whether migration actually rewrites a v0 envelope; this one gates whether this wrapper’s get_kv will read one at all) – set this one true if something needs to read v0 data through this wrapper before, or without ever, running migration.

Source

pub fn into_inner(self) -> T

Trait Implementations§

Source§

impl<T> AuthStorage for EncryptedStorage<T>
where T: AuthStorage + Send + Sync,

Source§

fn get_kv<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Reads back a value stored by Self::store_kv, decrypting it.

A value that parses as one of this module’s envelopes (StorageEncryption::looks_like_envelope) is always decrypted; a decryption failure (wrong key, corrupted, tampered) is a hard Err, never silently returned as-is.

A value that does not parse as an envelope at all is ambiguous: it could be genuine pre-encryption plaintext, or it could be a plaintext value an attacker (or a bug) overwrote the encrypted one with. This method only tolerates that ambiguity – returning the raw bytes as-is – when allow_plaintext_reads is true. With it false (the default via the storage factory once migration is done), a non-envelope value is also a hard Err, so overwriting an encrypted record with plaintext cannot silently succeed.

Source§

fn store_token<'life0, 'life1, 'async_trait>( &'life0 self, token: &'life1 AuthToken, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Store a token.
Source§

fn get_token<'life0, 'life1, 'async_trait>( &'life0 self, token_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<AuthToken>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieve a token by ID.
Source§

fn get_token_by_access_token<'life0, 'life1, 'async_trait>( &'life0 self, access_token: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<AuthToken>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieve a token by access token string.
Source§

fn update_token<'life0, 'life1, 'async_trait>( &'life0 self, token: &'life1 AuthToken, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Update a token.
Source§

fn delete_token<'life0, 'life1, 'async_trait>( &'life0 self, token_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a token.
Source§

fn list_user_tokens<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<AuthToken>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

List all tokens for a user.
Source§

fn store_session<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, session_id: &'life1 str, data: &'life2 SessionData, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store session data.
Source§

fn get_session<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<SessionData>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieve session data.
Source§

fn delete_session<'life0, 'life1, 'async_trait>( &'life0 self, session_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete session data.
Source§

fn list_user_sessions<'life0, 'life1, 'async_trait>( &'life0 self, user_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<SessionData>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

List all sessions for a user.
Source§

fn count_active_sessions<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Count currently active sessions (non-expired)
Source§

fn store_tokens_bulk<'life0, 'life1, 'async_trait>( &'life0 self, tokens: &'life1 [AuthToken], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Bulk store tokens.
Source§

fn delete_tokens_bulk<'life0, 'life1, 'async_trait>( &'life0 self, token_ids: &'life1 [String], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Bulk delete tokens by ID.
Source§

fn store_sessions_bulk<'life0, 'life1, 'async_trait>( &'life0 self, sessions: &'life1 [(String, SessionData)], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Bulk store sessions.
Source§

fn delete_sessions_bulk<'life0, 'life1, 'async_trait>( &'life0 self, session_ids: &'life1 [String], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Bulk delete sessions by ID.
Source§

fn get_kv_ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Duration>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Remaining time-to-live of a key-value entry. Read more
Source§

fn store_kv<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store arbitrary key-value data with expiration.
Source§

fn store_kv_if_absent<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Store value under key only if no live entry exists. Returns whether it was stored. Read more
Source§

fn swap_kv<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, remove: &'life1 [String], key: &'life2 str, value: &'life3 [u8], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Remove keys and store another in ONE atomic step. Read more
Source§

fn supports_atomic_swap(&self) -> bool

Whether swap_kv is implemented (atomically). false by default.
Source§

fn tracks_kv_ttl(&self) -> bool

Whether get_kv_ttl reports the TTLs given to store_kv. false (the default) means a TTL is invisible to callers, so anything that copies entries (migration, backup/restore) cannot preserve expiry. A backend that overrides get_kv_ttl must override this too (return true), otherwise it is treated as TTL-blind.
Source§

fn encrypts_kv_at_rest(&self) -> bool

Whether this storage encrypts KV values at rest itself (i.e. it is, or wraps, an EncryptedStorage). false (the default) means values reach the backend as given. A wrapper around an EncryptedStorage must forward this, or startup will wrongly report its values as unencrypted.
Source§

fn delete_kv<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete arbitrary key-value data.
Source§

fn list_kv_keys<'life0, 'life1, 'async_trait>( &'life0 self, prefix: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

List keys with a specific prefix. Read more
Source§

fn cleanup_expired<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clean up expired data.

Auto Trait Implementations§

§

impl<T> Forget for EncryptedStorage<T>

§

impl<T> Freeze for EncryptedStorage<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for EncryptedStorage<T>
where T: RefUnwindSafe,

§

impl<T> Send for EncryptedStorage<T>
where T: Send,

§

impl<T> Sync for EncryptedStorage<T>
where T: Sync,

§

impl<T> Unpin for EncryptedStorage<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for EncryptedStorage<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for EncryptedStorage<T>
where T: UnwindSafe,

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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