Skip to content
Snippets Groups Projects
Verified Commit ca0b8411 authored by insert's avatar insert
Browse files

Use proper error handling.

parent badaf8db
Branches
Tags
No related merge requests found
...@@ -6,7 +6,7 @@ pub struct Channel { ...@@ -6,7 +6,7 @@ pub struct Channel {
pub id: String, pub id: String,
#[serde(rename = "type")] #[serde(rename = "type")]
pub channel_type: u8, pub channel_type: u8,
pub last_message: Option<String>, pub last_message: Option<String>,
// for Direct Messages // for Direct Messages
......
use serde::{Deserialize, Serialize};
use bson::{bson, doc}; use bson::{bson, doc};
use serde::{Deserialize, Serialize};
use super::get_collection; use super::get_collection;
use mongodb::options::FindOneOptions; use mongodb::options::FindOneOptions;
...@@ -15,7 +15,11 @@ bitfield! { ...@@ -15,7 +15,11 @@ bitfield! {
pub get_send_messages, set_send_messages: 2; pub get_send_messages, set_send_messages: 2;
} }
pub fn find_member_permissions<C: Into<Option<String>>>(id: String, guild: String, channel: C) -> u8 { pub fn find_member_permissions<C: Into<Option<String>>>(
id: String,
guild: String,
channel: C,
) -> u8 {
let col = get_collection("guilds"); let col = get_collection("guilds");
match col.find_one( match col.find_one(
...@@ -28,27 +32,25 @@ pub fn find_member_permissions<C: Into<Option<String>>>(id: String, guild: Strin ...@@ -28,27 +32,25 @@ pub fn find_member_permissions<C: Into<Option<String>>>(id: String, guild: Strin
} }
}, },
FindOneOptions::builder() FindOneOptions::builder()
.projection( .projection(doc! {
doc! { "members.$": 1,
"members.$": 1, "owner": 1,
"owner": 1, "default_permissions": 1,
"default_permissions": 1, })
} .build(),
)
.build()
) { ) {
Ok(result) => { Ok(result) => {
if let Some(doc) = result { if let Some(doc) = result {
if doc.get_str("owner").unwrap() == id { if doc.get_str("owner").unwrap() == id {
return u8::MAX; return u8::MAX;
} }
doc.get_i32("default_permissions").unwrap() as u8 doc.get_i32("default_permissions").unwrap() as u8
} else { } else {
0 0
} }
}, }
Err(_) => 0 Err(_) => 0,
} }
} }
......
use super::Response;
use crate::database; use crate::database;
use crate::email; use crate::email;
...@@ -33,38 +34,30 @@ pub struct Create { ...@@ -33,38 +34,30 @@ pub struct Create {
/// (2) check email existence /// (2) check email existence
/// (3) add user and send email verification /// (3) add user and send email verification
#[post("/create", data = "<info>")] #[post("/create", data = "<info>")]
pub fn create(info: Json<Create>) -> JsonValue { pub fn create(info: Json<Create>) -> Response {
let col = database::get_collection("users"); let col = database::get_collection("users");
if info.username.len() < 2 || info.username.len() > 32 { if info.username.len() < 2 || info.username.len() > 32 {
return json!({ return Response::NotAcceptable(
"success": false, json!({ "error": "Username needs to be at least 2 chars and less than 32 chars." }),
"error": "Username requirements not met! Must be between 2 and 32 characters.", );
});
} }
if info.password.len() < 8 || info.password.len() > 72 { if info.password.len() < 8 || info.password.len() > 72 {
return json!({ return Response::NotAcceptable(
"success": false, json!({ "error": "Password needs to be at least 8 chars and at most 72." }),
"error": "Password requirements not met! Must be between 8 and 72 characters.", );
});
} }
if !validate_email(info.email.clone()) { if !validate_email(info.email.clone()) {
return json!({ return Response::UnprocessableEntity(json!({ "error": "Invalid email." }));
"success": false,
"error": "Invalid email provided!",
});
} }
if let Some(_) = col if let Some(_) = col
.find_one(doc! { "email": info.email.clone() }, None) .find_one(doc! { "email": info.email.clone() }, None)
.expect("Failed user lookup") .expect("Failed user lookup")
{ {
return json!({ return Response::Conflict(json!({ "error": "Email already in use!" }));
"success": false,
"error": "Email already in use!",
});
} }
if let Ok(hashed) = hash(info.password.clone(), 10) { if let Ok(hashed) = hash(info.password.clone(), 10) {
...@@ -91,21 +84,17 @@ pub fn create(info: Json<Create>) -> JsonValue { ...@@ -91,21 +84,17 @@ pub fn create(info: Json<Create>) -> JsonValue {
Ok(_) => { Ok(_) => {
let sent = email::send_verification_email(info.email.clone(), code); let sent = email::send_verification_email(info.email.clone(), code);
json!({ Response::Success(json!({
"success": true, "success": true,
"email_sent": sent, "email_sent": sent,
}) }))
}
Err(_) => {
Response::InternalServerError(json!({ "error": "Failed to create account." }))
} }
Err(_) => json!({
"success": false,
"error": "Failed to create account!",
}),
} }
} else { } else {
json!({ Response::InternalServerError(json!({ "error": "Failed to hash." }))
"success": false,
"error": "Failed to hash password!",
})
} }
} }
...@@ -114,7 +103,7 @@ pub fn create(info: Json<Create>) -> JsonValue { ...@@ -114,7 +103,7 @@ pub fn create(info: Json<Create>) -> JsonValue {
/// (2) check if it expired yet /// (2) check if it expired yet
/// (3) set account as verified /// (3) set account as verified
#[get("/verify/<code>")] #[get("/verify/<code>")]
pub fn verify_email(code: String) -> JsonValue { pub fn verify_email(code: String) -> Response {
let col = database::get_collection("users"); let col = database::get_collection("users");
if let Some(u) = col if let Some(u) = col
...@@ -125,10 +114,10 @@ pub fn verify_email(code: String) -> JsonValue { ...@@ -125,10 +114,10 @@ pub fn verify_email(code: String) -> JsonValue {
let ev = user.email_verification; let ev = user.email_verification;
if Utc::now() > *ev.expiry.unwrap() { if Utc::now() > *ev.expiry.unwrap() {
json!({ Response::Gone(json!({
"success": false, "success": false,
"error": "Token has expired!", "error": "Token has expired!",
}) }))
} else { } else {
let target = ev.target.unwrap(); let target = ev.target.unwrap();
col.update_one( col.update_one(
...@@ -151,15 +140,12 @@ pub fn verify_email(code: String) -> JsonValue { ...@@ -151,15 +140,12 @@ pub fn verify_email(code: String) -> JsonValue {
email::send_welcome_email(target.to_string(), user.username); email::send_welcome_email(target.to_string(), user.username);
json!({ Response::Redirect(
"success": true super::Redirect::to("https://example.com"), // ! FIXME; redirect to landing page
}) )
} }
} else { } else {
json!({ Response::BadRequest(json!({ "error": "Invalid code." }))
"success": false,
"error": "Invalid code!",
})
} }
} }
...@@ -173,7 +159,7 @@ pub struct Resend { ...@@ -173,7 +159,7 @@ pub struct Resend {
/// (2) check for rate limit /// (2) check for rate limit
/// (3) resend the email /// (3) resend the email
#[post("/resend", data = "<info>")] #[post("/resend", data = "<info>")]
pub fn resend_email(info: Json<Resend>) -> JsonValue { pub fn resend_email(info: Json<Resend>) -> Response {
let col = database::get_collection("users"); let col = database::get_collection("users");
if let Some(u) = col if let Some(u) = col
...@@ -190,18 +176,16 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue { ...@@ -190,18 +176,16 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue {
let rate_limit = ev.rate_limit.unwrap(); let rate_limit = ev.rate_limit.unwrap();
if Utc::now() < *rate_limit { if Utc::now() < *rate_limit {
json!({ Response::TooManyRequests(
"success": false, json!({ "error": "You are being rate limited, please try again in a while." }),
"error": "Hit rate limit! Please try again in a minute or so.", )
})
} else { } else {
let mut new_expiry = UtcDatetime(Utc::now() + chrono::Duration::days(1)); let mut new_expiry = UtcDatetime(Utc::now() + chrono::Duration::days(1));
if info.email.clone() != user.email { if info.email.clone() != user.email {
if Utc::now() > *expiry { if Utc::now() > *expiry {
return json!({ return Response::Gone(
"success": "false", json!({ "error": "To help protect your account, please login and change your email again. The original request was made over one day ago." }),
"error": "For security reasons, please login and change your email again.", );
});
} }
new_expiry = UtcDatetime(*expiry); new_expiry = UtcDatetime(*expiry);
...@@ -221,20 +205,16 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue { ...@@ -221,20 +205,16 @@ pub fn resend_email(info: Json<Resend>) -> JsonValue {
).expect("Failed to update user!"); ).expect("Failed to update user!");
match email::send_verification_email(info.email.to_string(), code) { match email::send_verification_email(info.email.to_string(), code) {
true => json!({ true => Response::Ok(None),
"success": true, false => Response::InternalServerError(
}), json!({ "success": false, "error": "Failed to send email! Likely an issue with the backend API." }),
false => json!({ ),
"success": false,
"error": "Failed to send email! Likely an issue with the backend API.",
}),
} }
} }
} else { } else {
json!({ Response::NotFound(
"success": false, json!({ "success": false, "error": "Email not found or pending verification!" }),
"error": "Email not pending verification!", )
})
} }
} }
...@@ -249,7 +229,7 @@ pub struct Login { ...@@ -249,7 +229,7 @@ pub struct Login {
/// (2) verify password /// (2) verify password
/// (3) return access token /// (3) return access token
#[post("/login", data = "<info>")] #[post("/login", data = "<info>")]
pub fn login(info: Json<Login>) -> JsonValue { pub fn login(info: Json<Login>) -> Response {
let col = database::get_collection("users"); let col = database::get_collection("users");
if let Some(u) = col if let Some(u) = col
...@@ -276,22 +256,12 @@ pub fn login(info: Json<Login>) -> JsonValue { ...@@ -276,22 +256,12 @@ pub fn login(info: Json<Login>) -> JsonValue {
} }
}; };
json!({ Response::Success(json!({ "access_token": token, "id": user.id }))
"success": true,
"access_token": token,
"id": user.id
})
} }
false => json!({ false => Response::Unauthorized(json!({ "error": "Invalid password." })),
"success": false,
"error": "Invalid password."
}),
} }
} else { } else {
json!({ Response::NotFound(json!({ "error": "Email is not registered." }))
"success": false,
"error": "Email is not registered.",
})
} }
} }
...@@ -302,21 +272,23 @@ pub struct Token { ...@@ -302,21 +272,23 @@ pub struct Token {
/// login to a Revolt account via token /// login to a Revolt account via token
#[post("/token", data = "<info>")] #[post("/token", data = "<info>")]
pub fn token(info: Json<Token>) -> JsonValue { pub fn token(info: Json<Token>) -> Response {
let col = database::get_collection("users"); let col = database::get_collection("users");
if let Some(u) = col if let Some(u) = col
.find_one(doc! { "access_token": info.token.clone() }, None) .find_one(doc! { "access_token": info.token.clone() }, None)
.expect("Failed user lookup") .expect("Failed user lookup")
{ {
json!({ Response::Success(
"success": true, json!({
"id": u.get_str("_id").unwrap(), "id": u.get_str("_id").unwrap(),
}) })
)
} else { } else {
json!({ Response::Unauthorized(
"success": false, json!({
"error": "Invalid token!", "error": "Invalid token!",
}) })
)
} }
} }
use crate::database::{self, channel::Channel, guild::{ Guild, find_member_permissions }, user::User}; use crate::database::{
self,
channel::Channel,
guild::{find_member_permissions, Guild},
user::User,
};
use bson::{bson, doc, from_bson, Bson}; use bson::{bson, doc, from_bson, Bson};
use rocket_contrib::json::{Json, JsonValue}; use rocket_contrib::json::{Json, JsonValue};
......
use rocket::http::Status;
pub use rocket::response::Redirect;
use rocket::Rocket; use rocket::Rocket;
use rocket_contrib::json::JsonValue;
pub mod account; pub mod account;
pub mod channel; pub mod channel;
...@@ -6,6 +9,36 @@ pub mod guild; ...@@ -6,6 +9,36 @@ pub mod guild;
pub mod root; pub mod root;
pub mod user; pub mod user;
#[derive(Responder)]
pub enum Response {
#[response()]
Ok(Option<JsonValue>),
#[response()]
Success(JsonValue),
#[response()]
Redirect(Redirect),
#[response(status = 400)]
BadRequest(JsonValue),
#[response(status = 401)]
Unauthorized(JsonValue),
#[response(status = 404)]
NotFound(JsonValue),
#[response(status = 406)]
NotAcceptable(JsonValue),
#[response(status = 409)]
Conflict(JsonValue),
#[response(status = 410)]
Gone(JsonValue),
#[response(status = 422)]
UnprocessableEntity(JsonValue),
#[response(status = 429)]
TooManyRequests(JsonValue),
#[response(status = 500)]
InternalServerError(JsonValue),
#[response()]
Error(Status),
}
pub fn mount(rocket: Rocket) -> Rocket { pub fn mount(rocket: Rocket) -> Rocket {
rocket rocket
.mount("/api", routes![root::root]) .mount("/api", routes![root::root])
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment