From 7f03008ebfda215c3dd61f8cb3276cef1642bb7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pomp=C3=A9ry?= Date: Wed, 30 Oct 2024 09:23:40 +0100 Subject: [PATCH] feat: statically read key material from file in testing --- demo-api/keypair.pem | 28 ++++++++++++++++++++++++++++ demo-api/src/auth.rs | 10 ++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 demo-api/keypair.pem diff --git a/demo-api/keypair.pem b/demo-api/keypair.pem new file mode 100644 index 0000000..7729fb5 --- /dev/null +++ b/demo-api/keypair.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCzd8UDP0UCMFmt +DsICAYJhxSNsAOFhc0B2jUm+97ZMTevGgWAnSYk1mmxtd+nXmDPGkn+MTDmVABGe +FwPKIno8t0GoN38PZ07E7QqGOsF7r1w960/0QKuPFYgUqQF/DX2mPkyp7epnmsh8 +g1ZDoR85MleVkyiSB8cBbrK6uMgH0zUqpfkygX+PSwsuwKAVeXY8bnsADFTAhAb5 +KVQQhuPoS8uIdA/edSxVy2BcS256UjI+4wUuVLRaySYIvhDf1QFRig1QPTHmJqAC +2wDM2SW09g3D2x5WNOtNTdFVU/HVTqlHYoR8Bz6aLYBNos0mSAIr2ASp2G3Bc8Dn +QvuLmGSJAgMBAAECggEAAdcCHy3Gf3afLrviX4qkK2wt2Ck8pkb8E9MQPNvVg7v/ +RSxVn+V8OX9VKfyjGjWaX0hodBfidb130MT34ho/IlD3JHo4LGBg4Xo0I8rlqabT +NWPMkKqaj6eW+u4+II8y+PG1z76riucec89VYrFbzHZwiS0mavPXNZOs06b6DwF2 +AbgL2OAZ/pXBx2k7NRlKU8jUE23R5oaXltpxq+TCesAfDek8LWfTZhtIEatGqXkX +G4aDeF1+klLzxbzJTeN2TbyNl263sECueho9hwTZdTptryp3thQLHv85WyrnZYHO +xjDdRhB3+QoZJXcWtZPoCNbr1dfHEwPcv5LRo/8BcQKBgQDzBPRJBUF/l7TH6jnT +UXVGUHhXvE+/ludUdj6O7UmT2Hr8ep1JWGzttLB3cFxgJmXlLkjbQZSbXSFuZYsg +dvApSiVknlKGXXUK6pw8RBSi4mv2hOZ2Xm3Ekhyv7BCrive7IzaocccFCsL/AEQO +9lv3DjQm826yptCjCGYHy9CGWQKBgQC9Dc/0UIOiKhJu1LE9IxUfjo0r4eany9Xm ++HM271nwFnH19CcK3RhGRosaF6rqzDrzn4j4xXiJdj1sN8aR2W6zeF+g14UtMEAj +9yf/7DbHeOY8jN1dggo7DY1MghK3oZBPWbMvFSkcdNw3pTOe7W/h3x8bq8vsbicv +CoMju7lpsQKBgQDohYr++wpqfMs6pP/jE4O0XXZn7AQZ+tcoV2MP+jZ2QaGbRe6M +qRDAeCyIe3fHf7XrDNglAaCg7wD30izGSUcNGqagmWO4sF82rLgEadec6iwG2a5w +/3TjqjHMmYBxwiu6YL0BeqvERUHCH4z/C/V+c5au1ijKwxXJiFWni/nfMQKBgBeV +FH6mWHUvCFOY5m745FamYoe5yxrdO1ZvYXfVzYM8WYTTqtcZE0EY5ZsgnqphRW1E +VkKp50pGXI0WPAfPdpaeilc26x7i6O1TlELBRWgGdbnR3H8gjIQnQZvskcckOSsc +xlQ2MIWjMiEi01icx5sGSQwblvlsGCkvB4K6lPtRAoGARWm8FApojUdtLPaQ83XA +UCWemmkRqUHFUBjK0jQ3LBcXer3J+7bxDZqqfVWU/FYXxrwpAjirnF7fpNUptKag +ouI5GwgiWLvXjjJ1DgPNt2B5QR2kAYOzuHJLhsJ1nEMs/CbkixD6fb8yg47u2kGZ +Kyk/dD16PG1exaDjoyHBDj0= +-----END PRIVATE KEY----- diff --git a/demo-api/src/auth.rs b/demo-api/src/auth.rs index dd6ab93..3017501 100644 --- a/demo-api/src/auth.rs +++ b/demo-api/src/auth.rs @@ -9,7 +9,6 @@ #![allow(clippy::blocks_in_conditions)] use std::collections::HashSet; -use std::env; use jsonwebtoken::errors::Result; use jsonwebtoken::{Algorithm, TokenData}; @@ -22,6 +21,7 @@ use rocket::request::{self, FromRequest, Request}; use rocket::response::status; use rocket::serde::{Deserialize, Serialize}; +use base64::{engine::general_purpose::STANDARD, Engine as _}; use rocket_okapi::okapi::map; use rocket_okapi::okapi::openapi3::{ Object, SecurityRequirement, SecurityScheme, SecuritySchemeData, @@ -33,7 +33,6 @@ use rocket_okapi::{ use rsa::pkcs8::DecodePrivateKey; use rsa::pkcs8::EncodePublicKey; use rsa::{pkcs8::LineEnding, RsaPrivateKey, RsaPublicKey}; -use base64::{engine::general_purpose::STANDARD, Engine as _}; #[derive(Clone)] pub struct KeyPair { @@ -156,9 +155,12 @@ impl<'r> FromRequest<'r> for UserToken { } pub fn load_keys() -> KeyPair { - let priv_key = env::var("PRIV_KEY").expect("PRIV_KEY must be set"); + #[cfg(test)] + let priv_key = include_str!("../keypair.pem"); + #[cfg(not(test))] + let priv_key: &str = &std::env::var("PRIV_KEY").expect("PRIV_KEY must be set"); - let private_key = RsaPrivateKey::from_pkcs8_pem(&priv_key) + let private_key = RsaPrivateKey::from_pkcs8_pem(priv_key) .unwrap_or_else(|err| panic!("Could not deserialize private key: {}", err)); let public_key = RsaPublicKey::from(&private_key);