diff options
author | Teddy Wing | 2018-11-13 03:14:03 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-13 03:14:03 +0100 |
commit | f8e04f96df65616fff89e7faac707f999176d18b (patch) | |
tree | f7a82fb51601553f9d4115b73b16791f534b9e9d | |
parent | f0a562869567f535441c8ce3cf6e86193abf7996 (diff) | |
download | dome-key-web-f8e04f96df65616fff89e7faac707f999176d18b.tar.bz2 |
license: Generate a license and send it in HTTP response
If the purchaser coming from POST params is found in the database,
generate a license for the purchaser, zip the license, and send a
response containing the zipped data.
zip:
Change the writer input to a mutable reference to enable us to use the
zip data when writing to the response. Otherwise we get a borrow error.
-rw-r--r-- | license-generator/Cargo.lock | 7 | ||||
-rw-r--r-- | license-generator/Cargo.toml | 1 | ||||
-rw-r--r-- | license-generator/src/bin/license.rs | 68 | ||||
-rw-r--r-- | license-generator/src/errors.rs | 1 | ||||
-rw-r--r-- | license-generator/src/zip.rs | 2 |
5 files changed, 72 insertions, 7 deletions
diff --git a/license-generator/Cargo.lock b/license-generator/Cargo.lock index 368e602..2905f0f 100644 --- a/license-generator/Cargo.lock +++ b/license-generator/Cargo.lock @@ -192,6 +192,11 @@ dependencies = [ ] [[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -302,6 +307,7 @@ version = "0.0.1" dependencies = [ "aquatic-prime 0.0.1", "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", + "exitcode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "fastcgi 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mysql 14.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -876,6 +882,7 @@ dependencies = [ "checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb" "checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" +"checksum exitcode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum fastcgi 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4159a0f48bea0281602e508eb070d7d7ba1f6ac2480f9db1a60a39274aea1cc" "checksum flate2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3b0c7353385f92079524de3b7116cf99d73947c08a7472774e9b3b04bff3b901" diff --git a/license-generator/Cargo.toml b/license-generator/Cargo.toml index 069230c..531fd57 100644 --- a/license-generator/Cargo.toml +++ b/license-generator/Cargo.toml @@ -5,6 +5,7 @@ version = "0.0.1" [dependencies] aquatic-prime = { path = "aquatic-prime" } error-chain = "0.12.0" +exitcode = "1.1.2" fastcgi = "1.0.0" log = "0.4.6" mysql = "14.1.1" diff --git a/license-generator/src/bin/license.rs b/license-generator/src/bin/license.rs index 54552f1..777b206 100644 --- a/license-generator/src/bin/license.rs +++ b/license-generator/src/bin/license.rs @@ -1,21 +1,38 @@ ///! FastCGI script that displays a thank-you page with a link to download a ///! custom-generated license. +extern crate aquatic_prime; extern crate fastcgi; +extern crate exitcode; #[macro_use] extern crate log; +#[macro_use] +extern crate serde_derive; + extern crate license_generator; use std::borrow::Cow; -use std::io::{Read, Write}; +use std::io::{Cursor, Read, Write}; + +use aquatic_prime::AquaticPrime; use license_generator::database; use license_generator::errors::*; use license_generator::logger; use license_generator::params; use license_generator::response; +use license_generator::zip; + +#[derive(Serialize)] +struct LicenseData<'a> { + #[serde(rename = "Name")] + name: &'a str, + + #[serde(rename = "Email")] + email: &'a str, +} fn main() -> Result<()> { logger::init()?; @@ -30,6 +47,27 @@ fn main() -> Result<()> { }, }; + // TODO: Change to include_str! + let public_key = match std::str::from_utf8( + include_bytes!("../../private/public_key.txt") + ).chain_err(|| "public key could not be loaded") { + Ok(k) => k, + Err(e) => { + error!("{}", e); + return Err(e); + }, + }; + let private_key = match std::str::from_utf8( + include_bytes!("../../private/private_key.txt") + ).chain_err(|| "private key could not be loaded") { + Ok(k) => k, + Err(e) => { + error!("{}", e); + return Err(e); + }, + }; + let aquatic_prime = AquaticPrime::new(&public_key, &private_key); + fastcgi::run(move |mut req| { let mut params = String::new(); match req.stdin().read_to_string(&mut params) { @@ -72,8 +110,12 @@ fn main() -> Result<()> { let secret = ps.get("secret"); if name.is_some() && email.is_some() && secret.is_some() { + let name = name.unwrap().to_string(); + let email = email.unwrap().to_string(); + let secret = secret.unwrap().to_string(); + let mut tx = cx.start_transaction(false, None, None).unwrap(); - let query_result = tx.prep_exec(" + let row = tx.prep_exec(" SELECT id FROM purchasers WHERE name = ? @@ -82,13 +124,27 @@ fn main() -> Result<()> { AND secret = ?", ( - name.unwrap().to_string(), - email.unwrap().to_string(), - secret.unwrap().to_string(), + &name, + &email, + &secret, ) ).unwrap().next(); - info!("Row: {:?}", query_result); + if row.is_some() { + let license_data = LicenseData { + name: &name, + email: &email, + }; + + let license = aquatic_prime.plist(license_data).unwrap(); + + let mut zip_data = Cursor::new(vec![]); + zip::license(&mut zip_data, license.as_bytes()).unwrap(); + + write!(&mut req.stdout(), "Content-Type: application/zip\n\n") + .unwrap(); + req.stdout().write_all(&zip_data.into_inner()).unwrap(); + } tx.commit().unwrap(); } else { diff --git a/license-generator/src/errors.rs b/license-generator/src/errors.rs index c7aadc6..352f959 100644 --- a/license-generator/src/errors.rs +++ b/license-generator/src/errors.rs @@ -7,6 +7,7 @@ error_chain! { foreign_links { EnvVar(::std::env::VarError); Io(::std::io::Error); + Utf8(::std::string::FromUtf8Error); Log(log::SetLoggerError); MySql(mysql::error::Error); diff --git a/license-generator/src/zip.rs b/license-generator/src/zip.rs index 6c08368..7b3600c 100644 --- a/license-generator/src/zip.rs +++ b/license-generator/src/zip.rs @@ -4,7 +4,7 @@ use zip_lib as zip; use errors::*; -pub fn license<W: Write + Seek>(w: W, plist: &[u8]) -> Result<()> { +pub fn license<W: Write + Seek>(w: &mut W, plist: &[u8]) -> Result<()> { let mut zip = zip::ZipWriter::new(w); zip.start_file( |