Return JSON, add basic captcha functionality

This commit is contained in:
~erin 2021-11-11 22:26:00 -05:00
parent 1e3c7d5e0b
commit 543be99f67
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
6 changed files with 458 additions and 44 deletions

359
Cargo.lock generated
View File

@ -2,6 +2,29 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "adler32"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom 0.2.3",
"once_cell",
"version_check",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
@ -86,6 +109,12 @@ version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "base64ct"
version = "1.0.1"
@ -130,6 +159,12 @@ version = "3.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c"
[[package]]
name = "bytemuck"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72957246c41db82b8ef88a5486143830adeb8227ef9837740bdec67724cf2c5b"
[[package]]
name = "byteorder"
version = "1.4.3"
@ -142,6 +177,20 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
[[package]]
name = "captcha"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29256038744434f6d0e1328d7c9050f14aa5fc8562ff065b9e9481ac293ba5bc"
dependencies = [
"base64",
"hound",
"image",
"lodepng",
"rand 0.7.3",
"serde_json",
]
[[package]]
name = "cc"
version = "1.0.71"
@ -166,6 +215,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "const_fn"
version = "0.4.8"
@ -192,6 +247,27 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.5"
@ -225,6 +301,16 @@ dependencies = [
"subtle",
]
[[package]]
name = "deflate"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174"
dependencies = [
"adler32",
"byteorder",
]
[[package]]
name = "devise"
version = "0.3.1"
@ -288,6 +374,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "fallible_collections"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaefd4190151d458f16f0793d3452d7f13aeb3701566a4cefc4c37598876cc00"
dependencies = [
"hashbrown",
]
[[package]]
name = "figment"
version = "0.10.6"
@ -302,6 +397,18 @@ dependencies = [
"version_check",
]
[[package]]
name = "flate2"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e6988e897c1c9c485f43b47a529cef42fde0547f9d8d41a7062518f1d8fc53f"
dependencies = [
"cfg-if",
"crc32fast",
"libc",
"miniz_oxide 0.4.4",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -444,6 +551,17 @@ dependencies = [
"version_check",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.3"
@ -452,7 +570,17 @@ checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
name = "gif"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3a7187e78088aead22ceedeee99779455b23fc231fe13ec443f99bb71694e5b"
dependencies = [
"color_quant",
"weezl",
]
[[package]]
@ -485,6 +613,9 @@ name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
dependencies = [
"ahash",
]
[[package]]
name = "hermit-abi"
@ -495,6 +626,12 @@ dependencies = [
"libc",
]
[[package]]
name = "hound"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549"
[[package]]
name = "http"
version = "0.2.5"
@ -553,6 +690,25 @@ dependencies = [
"want",
]
[[package]]
name = "image"
version = "0.23.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"gif",
"jpeg-decoder",
"num-iter",
"num-rational",
"num-traits",
"png",
"scoped_threadpool",
"tiff",
]
[[package]]
name = "indexmap"
version = "1.7.0"
@ -585,6 +741,15 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "jpeg-decoder"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2"
dependencies = [
"rayon",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -606,6 +771,18 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "lodepng"
version = "3.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1f7ef1b146a329001ccc6ad5fc1def8300281dda5176e8336b7fd44e440fb96"
dependencies = [
"fallible_collections",
"flate2",
"libc",
"rgb",
]
[[package]]
name = "log"
version = "0.4.14"
@ -660,6 +837,25 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435"
dependencies = [
"adler32",
]
[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.14"
@ -721,6 +917,28 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2021c8337a54d21aca0d59a92577a029af9431cb59b909b03252b9c164fad59"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
@ -784,7 +1002,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d791538a6dcc1e7cb7fe6f6b58aca40e7f79403c45b2bc274008b5e647af1d8"
dependencies = [
"base64ct",
"rand_core",
"rand_core 0.6.3",
"subtle",
]
@ -823,9 +1041,10 @@ version = "0.1.0"
dependencies = [
"argon2",
"bincode",
"captcha",
"log",
"rand",
"rand_core",
"rand 0.8.4",
"rand_core 0.6.3",
"rocket",
"serde",
"sled",
@ -844,6 +1063,18 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "png"
version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
dependencies = [
"bitflags",
"crc32fast",
"deflate",
"miniz_oxide 0.3.7",
]
[[package]]
name = "ppv-lite86"
version = "0.2.15"
@ -893,6 +1124,19 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.4"
@ -900,9 +1144,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
"rand_hc 0.3.1",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
]
[[package]]
@ -912,7 +1166,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
]
[[package]]
@ -921,7 +1184,16 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
"getrandom 0.2.3",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
@ -930,7 +1202,32 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rayon"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"lazy_static",
"num_cpus",
]
[[package]]
@ -995,6 +1292,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "rgb"
version = "0.8.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a27fa03bb1e3e2941f52d4a555a395a72bf79b0a85fbbaab79447050c97d978c"
dependencies = [
"bytemuck",
]
[[package]]
name = "rocket"
version = "0.5.0-rc.1"
@ -1017,7 +1323,7 @@ dependencies = [
"num_cpus",
"parking_lot",
"pin-project-lite",
"rand",
"rand 0.8.4",
"ref-cast",
"rocket_codegen",
"rocket_http",
@ -1105,6 +1411,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]]
name = "scoped_threadpool"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -1326,7 +1638,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if",
"libc",
"rand",
"rand 0.8.4",
"redox_syscall",
"remove_dir_all",
"winapi",
@ -1341,6 +1653,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "tiff"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437"
dependencies = [
"jpeg-decoder",
"miniz_oxide 0.4.4",
"weezl",
]
[[package]]
name = "time"
version = "0.2.27"
@ -1603,6 +1926,12 @@ dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
@ -1663,6 +1992,12 @@ version = "0.2.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc"
[[package]]
name = "weezl"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -15,3 +15,4 @@ argon2 = "0.3.1"
rand_core = { version = "0.6", features = ["std"] }
uuid = { version = "0.8", features = ["serde", "v5"] }
rand = "0.8.4"
captcha = "0.0.8"

View File

@ -8,7 +8,7 @@ A simple server for dealing with products & orders for the Catgirl Pharmacy.
- [x] Fix DB lock error
- [x] Keep track of orders
- [ ] Add proper error handling
- [ ] Return Json data
- [x] Return Json data
- [ ] Cleanup code
- [ ] Add comments

58
src/captcha.rs Normal file
View File

@ -0,0 +1,58 @@
// Generate & verify CAPTCHA's
extern crate captcha;
use std::fs::File;
use captcha::Captcha;
use captcha::filters::{Noise, Wave, Grid};
use std::path::Path;
use rocket::{serde::json::Json, form::Form, http::CookieJar, http::Cookie};
use crate::structures::{Status, CaptchaAnswer};
use uuid::Uuid;
fn create_captcha() -> Captcha {
let mut captcha = Captcha::new(); // create empty captcha
captcha.add_chars(7)
.apply_filter(Noise::new(0.2))
.apply_filter(Grid::new(41, 67))
.add_text_area()
.apply_filter(Wave::new(1.6373, 5.1363))
.view(220, 120); // create image
let captcha_text = captcha.chars_as_string();
captcha.save(Path::new(&("/tmp/captcha".to_owned()+&captcha_text+".png")))
.expect("save failed");// save captcha
return captcha;
}
#[get("/api/captcha")]
pub fn return_captcha(cookies: &CookieJar<'_>) -> File {
let captcha = create_captcha();
let captcha_text = captcha.chars_as_string();
let file = File::open(&("/tmp/captcha".to_owned()+&captcha_text+".png"));
println!("created new captcha {}", captcha_text);
cookies.add(Cookie::new("token",
Uuid::new_v5(
&Uuid::NAMESPACE_X500,
&captcha_text.as_bytes())
.to_simple()
.to_string())
);
return file.unwrap();
}
#[post("/api/captcha", data = "<answer>")]
pub fn check_captcha(answer: Form<CaptchaAnswer>) -> Json<Status> {
return Json( Status {
status: "fail".to_string(),
reason: "meow".to_string(),
});
}

View File

@ -2,6 +2,7 @@
extern crate rocket;
use rocket::{form::Form, State, Request, Response, http::Header};
use rocket::fairing::{Fairing, Info, Kind};
use rocket::serde::json::Json;
use uuid::Uuid;
use rand::Rng;
@ -22,6 +23,8 @@ use crate::database::*;
mod structures;
use crate::structures::*;
mod captcha;
use crate::captcha::*;
pub struct CORS;
@ -43,14 +46,12 @@ impl Fairing for CORS {
}
#[post("/api/new", data = "<request>")]
fn new_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<SaltString>, request: Form<ProductRequest>) -> String {
fn new_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<SaltString>, request: Form<ProductRequest>) -> Json<Status> {
let password = request.password.as_bytes();
let hashed_password = argon2.hash_password(password, &salt.as_ref()).unwrap().to_string();
let mut admin_password = env::var("ADMIN_PASSWD").unwrap();
admin_password = argon2.hash_password(admin_password.as_bytes(), &salt.as_ref()).unwrap().to_string();
println!("salt is: {}\n hashed password is: {}\nadmin password is: {}", &salt.as_ref(), hashed_password, admin_password);
if hashed_password == admin_password {
let new_product: Product = Product {
short_name: request.short_name.clone(),
@ -59,14 +60,16 @@ fn new_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<SaltSt
stock: request.stock,
};
register_product(db, &new_product.short_name, &new_product);
let new = read_product(db, &request.short_name).unwrap().unwrap();
return format!(
"registered new produt {} ${}. There are {} left in stock.",
new.full_name, new.price_usd, new.stock
);
return Json( Status {
status: "success".to_string(),
reason: "registered new product".to_string(),
});
};
return format!("wrong password!");
return Json( Status {
status: "fail".to_string(),
reason: "wrong password".to_string(),
});
}
#[post("/api/order", data = "<order>")]
@ -86,20 +89,13 @@ fn new_order(order: Form<OrderRequest<'_>>) -> String {
}
#[get("/api/order/<uuid>")]
fn order_info(uuid: &str) -> String {
fn order_info(uuid: &str) -> Json<Order> {
let order = read_order(uuid).unwrap().unwrap();
format!("Uuid: {}\nName: {}\nEmail: {}\nMessage: {}\nEncryption Key: {}\nPayment Type: {:?}",
order.uuid,
order.name,
order.email,
order.message,
order.encryption_key,
order.payment_type
)
return Json(order);
}
#[post("/api/orders", data="<auth>")]
fn all_orders(auth: Form<Authenticate>, argon2: &State<Argon2>, salt: &State<SaltString>) -> String {
fn all_orders(auth: Form<Authenticate>, argon2: &State<Argon2>, salt: &State<SaltString>) -> Result<Json<Vec<Order>>, Json<Status>>{
let entered_password = auth.password.as_bytes();
let hashed_password = argon2.hash_password(entered_password, &salt.as_ref()).unwrap().to_string();
let mut admin_password = env::var("ADMIN_PASSWD").unwrap();
@ -108,18 +104,23 @@ fn all_orders(auth: Form<Authenticate>, argon2: &State<Argon2>, salt: &State<Sal
if admin_password == hashed_password {
let all_orders = read_all_orders();
let mut return_string = String::new();
for x in &all_orders {
let mut orders_list: Vec<Order> = Vec::new();
for x in all_orders {
return_string = format!("{}\n{:?}",return_string, x);
orders_list.push(x);
}
return return_string;
return Ok(Json(orders_list));
} else {
return "wrong password".to_string();
return Err(Json( Status {
status: "fail".to_string(),
reason: "wrong password".to_string(),
}));
}
}
#[post("/api/update", data = "<update>")]
fn update_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<SaltString>, update: Form<Update<'_>>) -> String {
fn update_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<SaltString>, update: Form<Update<'_>>) -> Json<Status> {
let password = update.password.as_bytes();
let hashed_password = argon2.hash_password(password, &salt.as_ref()).unwrap().to_string();
let mut admin_password = env::var("ADMIN_PASSWD").unwrap();
@ -132,18 +133,27 @@ fn update_product(db: &State<sled::Db>, argon2: &State<Argon2>, salt: &State<Sal
let mut new_product = read_product(db, &update.product).unwrap().unwrap();
new_product.price_usd = update.value;
register_product(db, &update.product, &new_product);
return "price changed".to_string();
return Json( Status {
status: "success".to_string(),
reason: "price changed".to_string(),
});
}
"stock" => {
let mut new_product = read_product(db, &update.product).unwrap().unwrap();
new_product.stock = update.value as u32;
register_product(db, &update.product, &new_product);
return "stock changed".to_string();
return Json( Status {
status: "success".to_string(),
reason: "stock changed".to_string(),
});
}
_ => return "field not found".to_string(),
_ => return Json( Status { status: "fail".to_string(), reason: "field not found".to_string() }),
}
};
return "wrong password".to_string();
return Json( Status {
status: "fail".to_string(),
reason: "wrong password".to_string(),
});
}
#[get("/api/stock/<product>")]
@ -171,6 +181,6 @@ fn rocket() -> _ {
.manage(Argon2::default())
.manage(SaltString::generate(&mut OsRng))
.mount("/",
routes![get_stock, get_price, new_order, order_info, all_orders, new_product, update_product])
routes![get_stock, get_price, new_order, order_info, all_orders, new_product, update_product, return_captcha, check_captcha])
.attach(CORS)
}

View File

@ -1,5 +1,15 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use serde::{Deserialize, Serialize}; use uuid::Uuid;
#[derive(Clone, Debug, FromForm)]
pub struct CaptchaAnswer {
pub text: String,
}
#[derive(Serialize)]
pub struct Status {
pub status: String,
pub reason: String,
}
#[derive(Clone, Debug, FromForm)]
pub struct Authenticate {