Skip to content

Commit

Permalink
Remove dependency on openssl in tests (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Darren Baldwin <[email protected]>
  • Loading branch information
hazelmeow and DarrenBaldwin07 authored May 14, 2024
1 parent 06582f7 commit 15beca5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ features = ["json", "multipart"]
[dev-dependencies]
base64 = "0.22.1"
clerk-rs = { path = "../clerk-rs" }
openssl = "0.10.64"
rand = "0.8.5"
rsa = "0.9.6"
tokio = { version = "1.27.0", features = ["full"] }

[features]
Expand Down
18 changes: 9 additions & 9 deletions src/validators/authorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::prelude::*;
use jsonwebtoken::{encode, errors::ErrorKind, Algorithm, EncodingKey, Header};
use openssl::{pkey::Private, rsa::Rsa};
use rsa::{pkcs1::EncodeRsaPrivateKey, traits::PublicKeyParts, RsaPrivateKey};
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, serde::Serialize)]
Expand All @@ -152,21 +152,21 @@ mod tests {
}

struct Helper {
private_key: Rsa<Private>,
private_key: RsaPrivateKey,
}

impl Helper {
pub fn new() -> Self {
let mut rng = rand::thread_rng();

Self {
private_key: Rsa::generate(2048).unwrap(),
private_key: RsaPrivateKey::new(&mut rng, 2048).unwrap(),
}
}

pub fn generate_jwt_token(&self, kid: Option<&str>, current_time: Option<usize>, expired: bool) -> String {
let encoding_key =
EncodingKey::from_rsa_pem(self.private_key.private_key_to_pem().unwrap().as_slice()).expect("Failed to load encoding key");

Rsa::generate(2048).unwrap();
let pem = self.private_key.to_pkcs1_pem(rsa::pkcs8::LineEnding::LF).unwrap();
let encoding_key = EncodingKey::from_rsa_pem(pem.as_bytes()).expect("Failed to load encoding key");

let current_time = current_time.unwrap_or(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as usize);

Expand Down Expand Up @@ -196,8 +196,8 @@ mod tests {
}

pub fn get_modulus_and_public_exponent(&self) -> (String, String) {
let encoded_modulus = URL_SAFE_NO_PAD.encode(self.private_key.n().to_vec().as_slice());
let encoded_exponent = URL_SAFE_NO_PAD.encode(self.private_key.e().to_vec().as_slice());
let encoded_modulus = URL_SAFE_NO_PAD.encode(self.private_key.n().to_bytes_be().as_slice());
let encoded_exponent = URL_SAFE_NO_PAD.encode(self.private_key.e().to_bytes_be().as_slice());
(encoded_modulus, encoded_exponent)
}
}
Expand Down

0 comments on commit 15beca5

Please sign in to comment.