Skip to content

Commit

Permalink
Move mocking salts functionality under feature option. (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Minaev <[email protected]>
  • Loading branch information
jovfer authored Jan 17, 2024
1 parent d2f8fc1 commit 84ef9d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ authors = ["Sergey Minaev <[email protected]>"]
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"] }
Expand Down
27 changes: 16 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<String, String>> = Mutex::new(HashMap::new());
}
Expand All @@ -35,17 +37,20 @@ pub(crate) fn base64url_decode(b64data: &str) -> Result<Vec<u8>> {
.map_err(|e| Error::DeserializationError(e.to_string()))
}

pub(crate) fn generate_salt(key_for_predefined_salt: Option<String>) -> String {
let map = SALTS.lock().unwrap();
pub(crate) fn generate_salt(_key_for_predefined_salt: Option<String>) -> 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<serde_json::Map<String, Value>> {
Expand Down

0 comments on commit 84ef9d3

Please sign in to comment.