Skip to content

Commit

Permalink
test(crypto): add tests for jwe
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantJoshi00 committed Oct 19, 2023
1 parent 69979a0 commit 8744683
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ thiserror = "1.0.49"
error-stack = "0.4.1"
futures-util = "0.3.28"

[dev-dependencies]
rand = "0.8.5"



Expand Down
115 changes: 113 additions & 2 deletions src/crypto/jw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ fn jws_sign_payload(
Ok(jws::serialize_compact(payload, &src_header, &signer)?)
}

fn encrypt_jwe(payload: &[u8], public_key: impl AsRef<[u8]>) -> Result<String, error::CryptoError> {
pub fn encrypt_jwe(
payload: &[u8],
public_key: impl AsRef<[u8]>,
) -> Result<String, error::CryptoError> {
let alg = jwe::RSA_OAEP_256;
let enc = "A256GCM";
let mut src_header = jwe::JweHeader::new();
src_header.set_content_type(enc);
src_header.set_content_encryption(enc);
src_header.set_token_type("JWT");
let encrypter = alg.encrypter_from_pem(public_key)?;

Expand All @@ -146,3 +149,111 @@ pub fn verify_sign(jws_body: String, key: impl AsRef<[u8]>) -> Result<String, er
let resp = String::from_utf8(dst_payload)?;
Ok(resp)
}

#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used)]

use super::*;

// Keys used for tests
// Can be generated using the following commands:
// `openssl genrsa -out private_key.pem 2048`
// `openssl rsa -in private_key.pem -pubout -out public_key.pem`
const ENCRYPTION_KEY: &str = "\
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwa6siKaSYqD1o4J3AbHq
Km8oVTvep7GoN/C45qY60C7DO72H1O7Ujt6ZsSiK83EyI0CaUg3ORPS3ayobFNmu
zR366ckK8GIf3BG7sVI6u/9751z4OvBHZMM9JFWa7Bx/RC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c+QuxBi+lexs89ppuJIAgMvLUiJT67SBHguP
l4+oL9U78KGh7PfJpMKH+Pk5yc1xucAevk0wWmr5Tz2vKRDavFTPV+MCgYEA61qg
Y7yN0cDtxtqlZdMC8BJPFCQ1+s3uB0wetAY3BEKjfYc2O/4sMbixXzt5PkZqZy96
QBUBxhcM/rIolpM3nrgN7h1nmJdk9ljCTjWoTJ6fDk8BUh8+0GrVhTbe7xZ+bFUN
UioIqvfapr/q/k7Ah2mCBE04wTZFry9fndrH2ssCgYEAh1T2Cj6oiAX6UEgxe2h3
z4oxgz6efAO3AavSPFFQ81Zi+VqHflpA/3TQlSerfxXwj4LV5mcFkzbjfy9eKXE7
/bjCm41tQ3vWyNEjQKYr1qcO/aniRBtThHWsVa6eObX6fOGN+p4E+txfeX693j3A
6q/8QSGxUERGAmRFgMIbTq0CgYAmuTeQkXKII4U75be3BDwEgg6u0rJq/L0ASF74
4djlg41g1wFuZ4if+bJ9Z8ywGWfiaGZl6s7q59oEgg25kKljHQd1uTLVYXuEKOB3
e86gJK0o7ojaGTf9lMZi779IeVv9uRTDAxWAA93e987TXuPAo/R3frkq2SIoC9Rg
paGidwKBgBqYd/iOJWsUZ8cWEhSE1Huu5rDEpjra8JPXHqQdILirxt1iCA5aEQou
BdDGaDr8sepJbGtjwTyiG8gEaX1DD+KsF2+dQRQdQfcYC40n8fKkvpFwrKjDj1ac
VuY3OeNxi+dC2r7HppP3O/MJ4gX/RJJfSrcaGP8/Ke1W5+jE97Qy
-----END RSA PRIVATE KEY-----
";

#[test]
fn test_jwe() {
let jwt = encrypt_jwe("request_payload".as_bytes(), ENCRYPTION_KEY).unwrap();
let alg = jwe::RSA_OAEP_256;
let payload = decrypt_jwe(&jwt, DECRYPTION_KEY, alg).unwrap();
assert_eq!("request_payload".to_string(), payload)
}

#[test]
fn test_jws() {
let jwt = jws_sign_payload("jws payload".as_bytes(), SIGNING_KEY).unwrap();
let payload = verify_sign(jwt, SIGNATURE_VERIFICATION_KEY).unwrap();
assert_eq!("jws payload".to_string(), payload)
}
}

0 comments on commit 8744683

Please sign in to comment.