From ed88ef724584a27649a055e49a96a4664536df19 Mon Sep 17 00:00:00 2001 From: "baranyildirim@gmail.com" Date: Sun, 10 Mar 2024 06:03:15 +0000 Subject: [PATCH] Add pad option to base64 encode/decode functions --- biscuit-auth/src/token/mod.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/biscuit-auth/src/token/mod.rs b/biscuit-auth/src/token/mod.rs index 8140997f..c364de7e 100644 --- a/biscuit-auth/src/token/mod.rs +++ b/biscuit-auth/src/token/mod.rs @@ -99,12 +99,12 @@ impl Biscuit { } /// deserializes a token and validates the signature using the root public key - pub fn from_base64(slice: T, key_provider: KP) -> Result + pub fn from_base64(slice: T, key_provider: KP, pad: bool) -> Result where T: AsRef<[u8]>, KP: RootKeyProvider, { - Biscuit::from_base64_with_symbols(slice, key_provider, default_symbol_table()) + Biscuit::from_base64_with_symbols(slice, key_provider, default_symbol_table(), pad) } /// serializes the token @@ -113,11 +113,16 @@ impl Biscuit { } /// serializes the token and encode it to a (URL safe) base64 string - pub fn to_base64(&self) -> Result { + pub fn to_base64(&self, pad: bool) -> Result { + let base64_config = if pad { + base64::URL_SAFE + } else { + base64::URL_SAFE_NO_PAD + }; self.container .to_vec() .map_err(error::Token::Format) - .map(|v| base64::encode_config(v, base64::URL_SAFE)) + .map(|v| base64::encode_config(v, base64_config)) } /// serializes the token @@ -301,12 +306,18 @@ impl Biscuit { slice: T, key_provider: KP, symbols: SymbolTable, + pad: bool, ) -> Result where T: AsRef<[u8]>, KP: RootKeyProvider, { - let decoded = base64::decode_config(slice, base64::URL_SAFE)?; + let base64_config = if pad { + base64::URL_SAFE + } else { + base64::URL_SAFE_NO_PAD + }; + let decoded = base64::decode_config(slice, base64_config)?; Biscuit::from_with_symbols(&decoded, key_provider, symbols) }