pub struct StateEncryption { /* private fields */ }Expand description
State encryption using ChaCha20-Poly1305 AEAD
Provides authenticated encryption for OAuth state parameters. Uses a fixed encryption key for the deployment lifetime. Each encryption uses a random nonce for security.
§Security Properties
- Confidentiality: State values are encrypted with ChaCha20
- Authenticity: Authentication tag prevents tampering detection
- Replay Prevention: Random nonce in each encryption
- Key Isolation: Separate from signing keys, used only for state
Implementations§
Source§impl StateEncryption
impl StateEncryption
Sourcepub fn encrypt(&self, state: &str) -> Result<EncryptedState>
pub fn encrypt(&self, state: &str) -> Result<EncryptedState>
Encrypt a state value
Generates a random 96-bit nonce and encrypts the state using ChaCha20-Poly1305. The authentication tag is appended to the ciphertext.
§Arguments
state- The plaintext state value to encrypt
§Returns
EncryptedState containing ciphertext and nonce
§Errors
Returns error if encryption fails (should be rare)
Sourcepub fn decrypt(&self, encrypted: &EncryptedState) -> Result<String>
pub fn decrypt(&self, encrypted: &EncryptedState) -> Result<String>
Decrypt and verify a state value
Uses the nonce from EncryptedState to decrypt the ciphertext. Authentication tag verification is automatic - tampering is detected.
§Arguments
encrypted- The encrypted state to decrypt
§Returns
The decrypted plaintext state value
§Errors
Returns error if:
- Authentication tag verification fails (tampering detected)
- Decryption fails
- Result is not valid UTF-8
Sourcepub fn encrypt_to_bytes(&self, state: &str) -> Result<Vec<u8>>
pub fn encrypt_to_bytes(&self, state: &str) -> Result<Vec<u8>>
Encrypt state and serialize to bytes.
§Errors
Returns AuthError::Internal if AEAD encryption fails (essentially never).
Sourcepub fn decrypt_from_bytes(&self, bytes: &[u8]) -> Result<String>
pub fn decrypt_from_bytes(&self, bytes: &[u8]) -> Result<String>
Decrypt state from serialized bytes.
§Errors
Returns AuthError::InvalidState if bytes is too short, if AEAD
authentication fails (tampered or wrong key), or if decrypted bytes are
not valid UTF-8.
Auto Trait Implementations§
impl Freeze for StateEncryption
impl RefUnwindSafe for StateEncryption
impl Send for StateEncryption
impl Sync for StateEncryption
impl Unpin for StateEncryption
impl UnsafeUnpin for StateEncryption
impl UnwindSafe for StateEncryption
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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