Enum biscuit::jwe::Compact
[−]
[src]
pub enum Compact<T, H> { Decrypted { header: Header<H>, payload: T, }, Encrypted(Compact), }
Compact representation of a JWE, or an encrypted JWT
This representation contains a payload of type T
with custom headers provided by type H
.
Examples
Encrypting a JWS/JWT
See the example code in the biscuit::JWE
type alias.
Encrypting a string payload with A256GCMKW and A256GCM
extern crate biscuit; use std::str; use biscuit::Empty; use biscuit::jwk::{JWK}; use biscuit::jwe; use biscuit::jwa::{KeyManagementAlgorithm, ContentEncryptionAlgorithm}; let payload = "The true sign of intelligence is not knowledge but imagination."; // You would usually have your own AES key for this, but we will use a zeroed key as an example let key: JWK<Empty> = JWK::new_octect_key(&vec![0; 256/8], Default::default()); // Construct the JWE let jwe = jwe::Compact::new_decrypted(From::from(jwe::RegisteredHeader { cek_algorithm: KeyManagementAlgorithm::A256GCMKW, enc_algorithm: ContentEncryptionAlgorithm::A256GCM, ..Default::default() }), payload.as_bytes().to_vec()); // Encrypt let encrypted_jwe = jwe.encrypt(&key).unwrap(); // Decrypt let decrypted_jwe = encrypted_jwe.decrypt(&key, KeyManagementAlgorithm::A256GCMKW, ContentEncryptionAlgorithm::A256GCM) .unwrap(); let decrypted_payload: &Vec<u8> = decrypted_jwe.payload().unwrap(); let decrypted_str = str::from_utf8(&*decrypted_payload).unwrap(); assert_eq!(decrypted_str, payload);
Variants
Decrypted
Decrypted form of the JWE. This variant cannot be serialized or deserialized and will return an error.
Fields of Decrypted
header: Header<H> | Embedded header |
payload: T | Payload, usually a signed/unsigned JWT |
Encrypted(Compact)
Encrypted JWT. Use this form to send to your clients
Methods
impl<T: CompactPart, H: Serialize + DeserializeOwned + Clone> Compact<T, H>
[src]
fn new_decrypted(header: Header<H>, payload: T) -> Self
Create a new encrypted JWE
fn new_encrypted(token: &str) -> Self
Create a new encrypted JWE
fn into_encrypted<K: Serialize + DeserializeOwned>(
self,
key: &JWK<K>
) -> Result<Self, Error>
self,
key: &JWK<K>
) -> Result<Self, Error>
Consumes self and encrypt it. If the token is already encrypted, this is a no-op.
fn encrypt<K: Serialize + DeserializeOwned>(
&self,
key: &JWK<K>
) -> Result<Self, Error>
&self,
key: &JWK<K>
) -> Result<Self, Error>
Encrypt an Decrypted JWE
fn into_decrypted<K: Serialize + DeserializeOwned>(
self,
key: &JWK<K>,
cek_alg: KeyManagementAlgorithm,
enc_alg: ContentEncryptionAlgorithm
) -> Result<Self, Error>
self,
key: &JWK<K>,
cek_alg: KeyManagementAlgorithm,
enc_alg: ContentEncryptionAlgorithm
) -> Result<Self, Error>
Consumes self and decrypt it. If the token is already decrypted, this is a no-op.
fn decrypt<K: Serialize + DeserializeOwned>(
&self,
key: &JWK<K>,
cek_alg: KeyManagementAlgorithm,
enc_alg: ContentEncryptionAlgorithm
) -> Result<Self, Error>
&self,
key: &JWK<K>,
cek_alg: KeyManagementAlgorithm,
enc_alg: ContentEncryptionAlgorithm
) -> Result<Self, Error>
Decrypt an encrypted JWE. Provide the expected algorithms to mitigate an attacker modifying the fields
fn encrypted(&self) -> Result<&Compact, Error>
Convenience method to get a reference to the encrypted payload
fn encrypted_mut(&mut self) -> Result<&mut Compact, Error>
Convenience method to get a mutable reference to the encrypted payload
fn payload(&self) -> Result<&T, Error>
Convenience method to get a reference to the payload from an Decrypted JWE
fn payload_mut(&mut self) -> Result<&mut T, Error>
Convenience method to get a mutable reference to the payload from an Decrypted JWE
fn header(&self) -> Result<&Header<H>, Error>
Convenience method to get a reference to the header from an Decrypted JWE
fn header_mut(&mut self) -> Result<&mut Header<H>, Error>
Convenience method to get a reference to the header from an Decrypted JWE
fn unwrap_decrypted(self) -> (Header<H>, T)
Consumes self, and move the payload and header out and return them as a tuple
Panics
Panics if the JWE is not decrypted
fn unwrap_encrypted(self) -> Compact
Consumes self, and move the encrypted Compact serialization out and return it
Panics
Panics if the JWE is not encrypted
Trait Implementations
impl<T: Clone, H: Clone> Clone for Compact<T, H>
[src]
fn clone(&self) -> Compact<T, H>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more