commit
9eaadaa940
6 changed files with 173 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||||
|
CREATE TABLE PROSPECT( |
||||
|
id INTEGER PRIMARY KEY, |
||||
|
name TEXT NOT NULL, |
||||
|
email TEXT UNIQUE, |
||||
|
password TEXT NOT NULL, |
||||
|
approved INTEGER NOT NULL |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE car( |
||||
|
registration_plate TEXT PRIMARY KEY, |
||||
|
cost REAL NOT NULL, |
||||
|
bought_on TEXT NOT NULL |
||||
|
); |
||||
@ -0,0 +1,37 @@ |
|||||
|
use std::error::Error; |
||||
|
use url::Url; |
||||
|
use reqwest::StatusCode; |
||||
|
use serde::{Serialize, Deserialize}; |
||||
|
|
||||
|
const GOOGLE_API_URL: &str = "https://www.google.com/recaptcha/api/siteverify"; |
||||
|
|
||||
|
pub fn validate(secret_key: &String, token: &String) -> bool { |
||||
|
return match send_request(secret_key, token) { |
||||
|
Ok(is_success) => is_success, |
||||
|
Err(_e) => false |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
pub fn send_request(secret_key: &String, token: &String) -> Result<bool, Box<dyn Error>> { |
||||
|
let url = Url::parse_with_params(GOOGLE_API_URL, |
||||
|
&[("secret", secret_key), ("response", token)])?; |
||||
|
|
||||
|
let resp = reqwest::blocking::get(url)?; |
||||
|
let status_code = resp.status(); |
||||
|
|
||||
|
if status_code != StatusCode::OK { |
||||
|
println!("nicht gut"); |
||||
|
Err("status code is not")?; |
||||
|
} |
||||
|
|
||||
|
let response: GoogleCaptchaResponse = serde_json::from_str(resp.text()?.as_str())?; |
||||
|
|
||||
|
Ok(response.success) |
||||
|
} |
||||
|
|
||||
|
|
||||
|
#[serde(rename_all = "camelCase")] |
||||
|
#[derive(Serialize, Deserialize)] |
||||
|
struct GoogleCaptchaResponse { |
||||
|
success: bool |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
use serde::{Serialize, Deserialize}; |
||||
|
|
||||
|
#[serde(rename_all = "camelCase")] |
||||
|
#[derive(Debug, Serialize, Deserialize)] |
||||
|
pub struct FreakProspect { |
||||
|
pub name: String, |
||||
|
pub password: String, |
||||
|
pub confirm_password: String, |
||||
|
pub email: String, |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
use include_dir::{include_dir, Dir}; |
||||
|
use lazy_static::lazy_static; |
||||
|
use crate::common::FreakProspect; |
||||
|
use bcrypt::{DEFAULT_COST, hash}; |
||||
|
use rusqlite::{Result, Connection}; |
||||
|
use rusqlite_migration::Migrations; |
||||
|
|
||||
|
const DB_PATH: &str = "~/freakshells.db"; |
||||
|
const DB_PROSPECT: &str = "prospect"; |
||||
|
|
||||
|
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); |
||||
|
|
||||
|
lazy_static! { |
||||
|
static ref MIGRATIONS: Migrations<'static> = |
||||
|
Migrations::from_directory(&MIGRATIONS_DIR).unwrap(); |
||||
|
} |
||||
|
|
||||
|
fn init_db() -> Connection { |
||||
|
let mut conn = Connection::open("./my_db.db3").unwrap(); |
||||
|
|
||||
|
// Update the database schema, atomically
|
||||
|
MIGRATIONS.to_latest(&mut conn).unwrap(); |
||||
|
|
||||
|
conn |
||||
|
} |
||||
|
|
||||
|
pub fn store_prospect(prospect: FreakProspect) -> Result<()> { |
||||
|
let db = init_db(); |
||||
|
let hashed_password = hash(prospect.password, DEFAULT_COST).unwrap(); |
||||
|
db.execute( |
||||
|
"INSERT INTO PROSPECT (name, email, password, approved) VALUES (?1, ?2, ?3, 0)",
|
||||
|
(prospect.name, prospect.email, hashed_password) |
||||
|
)?; |
||||
|
|
||||
|
Ok(()) |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
use lettre::message::header::ContentType; |
||||
|
use lettre::transport::smtp::authentication::Credentials; |
||||
|
use lettre::{Message, SmtpTransport, Transport}; |
||||
|
|
||||
|
use std::io::Result; |
||||
|
|
||||
|
pub struct FreakMail { |
||||
|
from: String, |
||||
|
to: String, |
||||
|
subject: String, |
||||
|
body: String, |
||||
|
} |
||||
|
|
||||
|
pub fn send_mail(mail: FreakMail) -> Result<()> { |
||||
|
let email = Message::builder() |
||||
|
.from("Administrator FREAKSHELLS <admin@freakshells.net>".parse().unwrap()) |
||||
|
.to("dodiusz@gmail.com".parse().unwrap()) |
||||
|
.subject("Happy new year") |
||||
|
.header(ContentType::TEXT_HTML) |
||||
|
.body(String::from("Be happy!")) |
||||
|
.unwrap(); |
||||
|
|
||||
|
let creds = Credentials::new("freakshellsnet@gmail.com".to_owned(), "kivpysohxzydycnq".to_owned()); |
||||
|
|
||||
|
// Open a remote connection to gmail
|
||||
|
let mailer = SmtpTransport::relay("smtp.gmail.com") |
||||
|
.unwrap() |
||||
|
.credentials(creds) |
||||
|
.build(); |
||||
|
|
||||
|
// Send the email
|
||||
|
mailer.send(&email).unwrap(); |
||||
|
|
||||
|
Ok(()) |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
use rocket::serde::{Serialize, json::Json}; |
||||
|
use rocket::response::status; |
||||
|
|
||||
|
use common::FreakProspect; |
||||
|
|
||||
|
mod captcha; |
||||
|
mod db; |
||||
|
mod common; |
||||
|
mod mail; |
||||
|
|
||||
|
#[macro_use] |
||||
|
extern crate rocket; |
||||
|
|
||||
|
#[derive(Serialize)] |
||||
|
#[serde(crate = "rocket::serde")] |
||||
|
struct GetResponse { |
||||
|
id: i32, |
||||
|
val: String |
||||
|
} |
||||
|
|
||||
|
|
||||
|
#[get("/")] |
||||
|
fn hello() -> &'static str { |
||||
|
let skey = String::from("cze"); |
||||
|
let tok = String::from("de"); |
||||
|
captcha::validate(&skey, &tok); |
||||
|
|
||||
|
db::store_prospect(FreakProspect { |
||||
|
name: String::from("asd"), |
||||
|
email: "test@test".to_string(), |
||||
|
password: "Test123#".to_string(), |
||||
|
confirm_password: "Test123#".to_string(), |
||||
|
}).unwrap(); |
||||
|
|
||||
|
"Hello world!" |
||||
|
} |
||||
|
|
||||
|
#[launch] |
||||
|
fn rocket() -> _ { |
||||
|
rocket::build().mount("/", routes![hello]) |
||||
|
} |
||||
|
|
||||
Loading…
Reference in new issue