From cc1074b008f143e779157f0213016ccb18e7fd64 Mon Sep 17 00:00:00 2001 From: Sergey Minaev Date: Wed, 17 Jan 2024 13:27:02 +0500 Subject: [PATCH] Move mocking salts functionality under feature option. Signed-off-by: Sergey Minaev --- Cargo.toml | 5 ++++- src/utils.rs | 27 ++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2bfcb1c..f563338 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,11 +9,14 @@ authors = ["Sergey Minaev "] repository = "https://github.com/openwallet-foundation-labs/sd-jwt-rust" homepage = "https://github.com/openwallet-foundation-labs/sd-jwt-rust" +[features] +mock_salts = ["lazy_static"] + [dependencies] base64 = "0.21" hmac = "0.12" jsonwebtoken = "9.2" -lazy_static = "1.4" +lazy_static = { version = "1.4", optional = true } log = "0.4" rand = "0.8" serde = { version = "1.0.193", features = ["derive"] } diff --git a/src/utils.rs b/src/utils.rs index e6df228..4cd14fc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,14 +5,16 @@ use crate::error::Error::DeserializationError; use base64::engine::general_purpose; use base64::Engine; use error::Result; +#[cfg(feature = "mock_salts")] use lazy_static::lazy_static; use rand::prelude::ThreadRng; use rand::RngCore; use serde_json::Value; use sha2::Digest; -use std::collections::HashMap; -use std::sync::Mutex; +#[cfg(feature = "mock_salts")] +use std::{collections::HashMap, sync::Mutex}; +#[cfg(feature = "mock_salts")] lazy_static! { pub static ref SALTS: Mutex> = Mutex::new(HashMap::new()); } @@ -35,17 +37,20 @@ pub(crate) fn base64url_decode(b64data: &str) -> Result> { .map_err(|e| Error::DeserializationError(e.to_string())) } -pub(crate) fn generate_salt(key_for_predefined_salt: Option) -> String { - let map = SALTS.lock().unwrap(); +pub(crate) fn generate_salt(_key_for_predefined_salt: Option) -> String { - if let Some(salt) = key_for_predefined_salt.and_then(|key| map.get(&key)) { - //FIXME better mock approach - salt.clone() - } else { - let mut buf = [0u8; 16]; - ThreadRng::default().fill_bytes(&mut buf); - base64url_encode(&buf) + #[cfg(feature = "mock_salts")] + { + let map = SALTS.lock().unwrap(); + if let Some(salt) = _key_for_predefined_salt.and_then(|key| map.get(&key)) { + //FIXME better mock approach + return salt.clone() + } } + + let mut buf = [0u8; 16]; + ThreadRng::default().fill_bytes(&mut buf); + base64url_encode(&buf) } pub(crate) fn jwt_payload_decode(b64data: &str) -> Result> {