Skip to content

Commit

Permalink
Generate private key with build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed Nov 6, 2023
1 parent 17b6775 commit 515a43a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ db/*
.envrc
static/dist/
node_modules/
keys/*.pem
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ thiserror = "1.0"
validator = { version = "0.16", features = [ "derive" ] }
jsonwebtoken = "9.1"
openssl = "0.10"

[build-dependencies]
openssl = "0.10"
2 changes: 1 addition & 1 deletion Rocket.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ maximum_pending_users = 25

[debug]
secret_key = "1vwCFFPSdQya895gNiO556SzmfShG6MokstgttLvwjw="
ec_private_key = "keys/replace_me.pem"
ec_private_key = "keys/jwt_key.pem"
bcrypt_cost = 4
seed_database = true

Expand Down
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::fs::File;
use std::io::Write;
use std::path::Path;

use openssl::ec::{EcGroup, EcKey};
use openssl::nid::Nid;
use openssl::pkey::PKey;

fn main() {
let path = Path::new("keys/jwt_key.pem");
if !path.exists() {
let group = EcGroup::from_curve_name(Nid::SECP384R1).unwrap();
let pkey = PKey::from_ec_key(EcKey::generate(&group).unwrap()).unwrap();
let mut f = File::create(path).unwrap();
let pem = pkey.private_key_to_pem_pkcs8().unwrap();
f.write_all(&pem).unwrap();
}
}
Empty file added keys/.gitkeep
Empty file.
6 changes: 0 additions & 6 deletions keys/replace_me.pem

This file was deleted.

2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn config() -> Config {
email_confirmation_token_seconds: 300,
secure_token_length: 64,
bcrypt_cost: BCRYPT_COST,
ec_private_key: "keys/replace_me.pem".to_string(),
ec_private_key: "keys/jwt_key.pem".to_string(),
base_url: "example.com".to_string(),
mail_queue_size: 10,
mail_queue_wait_seconds: 0,
Expand Down

0 comments on commit 515a43a

Please sign in to comment.