Compare commits
No commits in common. "3a5fb7bcd08d6d85a6f41cdf05c6f184a4fca22e" and "2cd4cf784edfb51ca240059bf2d6fc365b46040d" have entirely different histories.
3a5fb7bcd0
...
2cd4cf784e
146
src/main.rs
146
src/main.rs
|
@ -24,7 +24,7 @@ use rocket::fs::TempFile;
|
|||
use rocket::http::CookieJar;
|
||||
use rocket::serde::{json::Json, Deserialize};
|
||||
|
||||
use fossil::tables::{Db, Image, LoginStatus, Post, User};
|
||||
use fossil::tables::{Db, Image, Post, User};
|
||||
|
||||
#[get("/login")]
|
||||
fn login_page() -> RawHtml<String> {
|
||||
|
@ -114,46 +114,38 @@ async fn account(mut db: Connection<Db>, cookies: &CookieJar<'_>) -> status::Cus
|
|||
}
|
||||
|
||||
#[post("/login", data = "<info>")]
|
||||
async fn login(
|
||||
mut db: Connection<Db>,
|
||||
info: Json<LoginInfo>,
|
||||
cookies: &CookieJar<'_>,
|
||||
) -> status::Custom<&'static str> {
|
||||
async fn login(mut db: Connection<Db>, info: Json<LoginInfo>, cookies: &CookieJar<'_>) -> String {
|
||||
let token = cookies.get_private("token");
|
||||
match token {
|
||||
Some(_) => {
|
||||
status::Custom(Status::Continue, "already logged in")
|
||||
"already logged in".to_string()
|
||||
}
|
||||
None => {
|
||||
match User::get_by_username(&mut db, &info.username).await {
|
||||
Some(user) => {
|
||||
if user.password == sha256::digest(&info.password) {
|
||||
match user.token {
|
||||
Some(t) => {cookies.add_private(("token", t)); status::Custom(Status::Ok, "Logged in")},
|
||||
Some(t) => {cookies.add_private(("token", t)); "Logged in".to_string()},
|
||||
None => {
|
||||
match user.set_new_token(&mut db).await {
|
||||
Ok(t) => {
|
||||
cookies.add_private(("token", t));
|
||||
status::Custom(Status::Ok, "Logged in")
|
||||
},
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
status::Custom(Status::InternalServerError, "Couldn't generate a token for you, therefore you weren't logged in.")
|
||||
"logged in".to_string()
|
||||
},
|
||||
Err(why) => why,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
status::Custom(Status::Forbidden, "Invalid username or password (to those whining about why it doesn't tell you if the username or password is incorrect, security)")
|
||||
String::from("Invalid username or password (to those whining about why it doesn't tell you if the username or password is incorrect, security)")
|
||||
}
|
||||
}
|
||||
None =>
|
||||
status::Custom(Status::Forbidden, "Invalid username or password (to those whining about why it doesn't tell you if the username or password is incorrect, security)")
|
||||
String::from("Invalid username or password (to those whining about why it doesn't tell you if the username or password is incorrect, security)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/logout")]
|
||||
async fn logout(cookies: &CookieJar<'_>) -> status::Custom<&'static str> {
|
||||
match cookies.get_private("token") {
|
||||
|
@ -261,14 +253,11 @@ async fn toggleperms(
|
|||
cookies: &CookieJar<'_>,
|
||||
) -> status::Custom<String> {
|
||||
match cookies.get_private("token") {
|
||||
Some(t) => {
|
||||
match User::get_by_token(&mut db, t).await {
|
||||
Some(user) => {
|
||||
match user.admin {
|
||||
Some(t) => match User::get_by_token(&mut db, t).await {
|
||||
Some(user) => match user.admin {
|
||||
true => match User::get_by_username(&mut db, &info.username).await {
|
||||
Some(toggled_user) => {
|
||||
match toggled_user.username == user.username && info.perm == "admin"
|
||||
{
|
||||
match toggled_user.username == user.username && info.perm == "admin" {
|
||||
true => status::Custom(
|
||||
Status::Forbidden,
|
||||
"You can't change your own admin status".to_string(),
|
||||
|
@ -279,8 +268,7 @@ async fn toggleperms(
|
|||
match toggled_user.username == admin_username {
|
||||
true => status::Custom(
|
||||
Status::Forbidden,
|
||||
"You can't change the system admin's perms."
|
||||
.to_string(),
|
||||
"You can't change the system admin's perms.".to_string(),
|
||||
),
|
||||
false => {
|
||||
match info.perm == "admin"
|
||||
|
@ -307,27 +295,19 @@ async fn toggleperms(
|
|||
}
|
||||
None => status::Custom(
|
||||
Status::NotFound,
|
||||
"The user you're trying to toggle perms for doesn't exist."
|
||||
.to_string(),
|
||||
"The user you're trying to toggle perms for doesn't exist.".to_string(),
|
||||
),
|
||||
},
|
||||
false => {
|
||||
status::Custom(Status::Unauthorized, "You aren't an admin.".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
false => status::Custom(Status::Unauthorized, "You aren't an admin.".to_string()),
|
||||
},
|
||||
None => status::Custom(Status::Unauthorized, "Invalid login token".to_string()),
|
||||
}
|
||||
}
|
||||
},
|
||||
None => status::Custom(Status::Unauthorized, "Not logged in".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/images/<image>")]
|
||||
async fn get_image(
|
||||
image: String,
|
||||
mut db: Connection<Db>,
|
||||
) -> Result<NamedFile, status::Custom<&'static str>> {
|
||||
async fn get_image(image: String, mut db: Connection<Db>) -> Result<NamedFile, Status> {
|
||||
let mut split = image.split('.').collect::<Vec<&str>>();
|
||||
let format = split.pop().unwrap();
|
||||
let image = split.join(".");
|
||||
|
@ -355,95 +335,77 @@ async fn get_image(
|
|||
Ok(file)
|
||||
}
|
||||
Err(why) => Err(match &why.to_string()[..] {
|
||||
"Format error decoding Png: Invalid PNG signature." => {
|
||||
status::Custom(Status::NotAcceptable, "That file isn't an image.")
|
||||
}
|
||||
_ => status::Custom(Status::InternalServerError, "Unknown decoding error"),
|
||||
"Format error decoding Png: Invalid PNG signature." => Status::NotAcceptable,
|
||||
_ => Status::InternalServerError,
|
||||
}),
|
||||
},
|
||||
Err(_) => Err(status::Custom(
|
||||
Status::InternalServerError,
|
||||
"Unknown decoding error",
|
||||
)),
|
||||
Err(_) => Err(Status::NotFound),
|
||||
},
|
||||
Err(_) => Err(status::Custom(
|
||||
Status::InternalServerError,
|
||||
"File not found",
|
||||
)),
|
||||
Err(_) => Err(Status::ImATeapot),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/upload", format = "image/png", data = "<file>")]
|
||||
async fn upload(
|
||||
mut file: TempFile<'_>,
|
||||
cookies: &CookieJar<'_>,
|
||||
mut db: Connection<Db>,
|
||||
) -> status::Custom<String> {
|
||||
async fn upload(mut file: TempFile<'_>, cookies: &CookieJar<'_>, mut db: Connection<Db>) -> String {
|
||||
let uuid = Uuid::new_v4().to_string();
|
||||
|
||||
// login & perms check
|
||||
match User::login_status(&mut db, cookies).await {
|
||||
LoginStatus::LoggedIn(user) => {
|
||||
match user.make_posts || user.admin {
|
||||
// image validation check
|
||||
true => {
|
||||
match file.copy_to(format!("/srv/tmpimages/{uuid}.png")).await {
|
||||
Ok(_) => {
|
||||
// validate that it is, in fact, an image
|
||||
match Reader::open(format!("/srv/tmpimages/{uuid}.png")) {
|
||||
Ok(_) => {
|
||||
match file.copy_to(format!("/srv/images/{uuid}.png")).await {
|
||||
Ok(_) => match Image::create(&mut db, &uuid, user).await {
|
||||
match cookies.get_private("token") {
|
||||
Some(t) => {
|
||||
match User::get_by_token(&mut db, t).await {
|
||||
Some(user) => {
|
||||
if user.make_posts == true || user.admin == true {
|
||||
// upload to db
|
||||
match Image::create(&mut db, &uuid, user).await {
|
||||
Ok(_) => {
|
||||
// move image
|
||||
match file
|
||||
.copy_to(format!("/srv/images/{uuid}.png"))
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
match fs::remove_file(format!(
|
||||
"/srv/tmpimages/{uuid}.png"
|
||||
)) {
|
||||
Ok(_) => status::Custom(Status::Created, uuid),
|
||||
Ok(_) => uuid,
|
||||
Err(why) => {
|
||||
eprintln!("couldn't clear image from tmpimages: {why:?}");
|
||||
status::Custom(Status::Ok, uuid)
|
||||
eprintln!("{why:?}");
|
||||
"couldn't delete old file"
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
eprintln!("{why:?}");
|
||||
status::Custom(
|
||||
Status::InternalServerError,
|
||||
"Couldn't save to DB".to_string(),
|
||||
)
|
||||
}
|
||||
},
|
||||
Err(_) => status::Custom(
|
||||
Status::InternalServerError,
|
||||
"Couldn't copy file to final location".to_string(),
|
||||
),
|
||||
"couldn't save file to final destination"
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
Err(_) => status::Custom(
|
||||
Status::Forbidden,
|
||||
"File isn't an image".to_string(),
|
||||
),
|
||||
}
|
||||
Err(_) => "Couldn't save to DB".to_string(),
|
||||
}
|
||||
} else {
|
||||
"Invalid perms".to_string()
|
||||
}
|
||||
}
|
||||
Err(_) => status::Custom(
|
||||
Status::InternalServerError,
|
||||
"Couldn't save temporary file".to_string(),
|
||||
),
|
||||
None => "Invalid login token".to_string(),
|
||||
}
|
||||
}
|
||||
false => status::Custom(
|
||||
Status::Unauthorized,
|
||||
"You don't have the right permissions for that".to_string(),
|
||||
),
|
||||
None => "Not logged in".to_string(),
|
||||
}
|
||||
}
|
||||
LoginStatus::InvalidToken => {
|
||||
status::Custom(Status::Unauthorized, "Invalid login token".to_string())
|
||||
Err(why) => {
|
||||
// isn't an image, or something else went wrong
|
||||
eprintln!("{why:?}");
|
||||
"error".to_string()
|
||||
}
|
||||
LoginStatus::NotLoggedIn => {
|
||||
status::Custom(Status::Unauthorized, "Not logged in".to_string())
|
||||
}
|
||||
}
|
||||
Err(why) => why.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[delete("/images/<uuid>")]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use base64::{engine::general_purpose, Engine};
|
||||
use rand::{RngCore, SeedableRng};
|
||||
use regex::Regex;
|
||||
use rocket::http::{Cookie, CookieJar};
|
||||
use rocket::http::Cookie;
|
||||
use rocket_db_pools::sqlx::Executor;
|
||||
use rocket_db_pools::Connection;
|
||||
use rocket_db_pools::{
|
||||
|
@ -67,12 +67,6 @@ pub struct User {
|
|||
pub comment: bool,
|
||||
}
|
||||
|
||||
pub enum LoginStatus {
|
||||
InvalidToken,
|
||||
NotLoggedIn,
|
||||
LoggedIn(User)
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub async fn create(db: &mut Connection<Db>, username: &String, password: &String) -> Result<String, String> {
|
||||
match Regex::new(r"[^A-Za-z0-9_]") {
|
||||
|
@ -223,22 +217,6 @@ impl User {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn login_status(db: &mut Connection<Db>, cookies: &CookieJar<'_>) -> LoginStatus {
|
||||
match cookies.get_private("token") {
|
||||
Some(t) => {
|
||||
match User::get_by_token(db, t).await {
|
||||
Some(user) => {
|
||||
LoginStatus::LoggedIn(user)
|
||||
},
|
||||
None => {
|
||||
LoginStatus::InvalidToken
|
||||
}
|
||||
}
|
||||
},
|
||||
None => LoginStatus::NotLoggedIn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Image {
|
||||
|
|
|
@ -32,8 +32,6 @@
|
|||
},
|
||||
body: input.files[0]
|
||||
})
|
||||
|
||||
// TODO: if status is 200, not 201, alert the user to tell the admins to clear tmpimages
|
||||
alert(await token.text());
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue