diff --git a/src/errors.rs b/src/errors.rs index 2257fa5..bb7856a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -16,6 +16,7 @@ pub enum RbError AuthRefreshTokenExpired, AuthInvalidRefreshToken, AuthDuplicateRefreshToken, + AuthMissingHeader, // UM = User Management UMDuplicateUser, @@ -39,6 +40,7 @@ impl RbError RbError::AuthRefreshTokenExpired => Status::Unauthorized, RbError::AuthInvalidRefreshToken => Status::Unauthorized, RbError::AuthDuplicateRefreshToken => Status::Unauthorized, + RbError::AuthMissingHeader => Status::BadRequest, RbError::UMDuplicateUser => Status::Conflict, @@ -60,6 +62,7 @@ impl RbError RbError::AuthDuplicateRefreshToken => { "This refresh token has already been used. The user has been blocked." } + RbError::AuthMissingHeader => "Missing Authorization header.", RbError::UMDuplicateUser => "This user already exists.", diff --git a/src/guards.rs b/src/guards.rs index d515d77..7b40bdd 100644 --- a/src/guards.rs +++ b/src/guards.rs @@ -1,5 +1,3 @@ -use std::convert::From; - use hmac::{Hmac, NewMac}; use jwt::VerifyWithKey; use rocket::{ @@ -24,7 +22,7 @@ impl<'r> FromRequest<'r> for Bearer<'r> { // If the header isn't present, just forward to the next route let header = match req.headers().get_one("Authorization") { - None => return Outcome::Forward(()), + None => return Outcome::Failure((Status::BadRequest, Self::Error::AuthMissingHeader)), Some(val) => val, }; @@ -35,7 +33,7 @@ impl<'r> FromRequest<'r> for Bearer<'r> // Extract the jwt token from the header let auth_string = match header.get(7..) { Some(s) => s, - None => return Outcome::Forward(()), + None => return Outcome::Failure((Status::Unauthorized, Self::Error::AuthUnauthorized)), }; Outcome::Success(Self(auth_string)) @@ -45,14 +43,6 @@ impl<'r> FromRequest<'r> for Bearer<'r> /// Verifies the provided JWT is valid. pub struct Jwt(Claims); -impl From<()> for RbError -{ - fn from(_: ()) -> Self - { - RbError::Custom("Couldn't get config guard.") - } -} - #[rocket::async_trait] impl<'r> FromRequest<'r> for Jwt { @@ -123,7 +113,7 @@ impl<'r> FromRequest<'r> for Admin if user.admin { Outcome::Success(Self(user)) } else { - Outcome::Forward(()) + Outcome::Failure((Status::Unauthorized, RbError::AuthUnauthorized)) } } } diff --git a/src/main.rs b/src/main.rs index fa147e9..d4ee778 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,12 @@ use figment::{ providers::{Env, Format, Yaml}, Figment, }; -use rocket::{fairing::AdHoc, Build, Rocket}; +use rocket::{ + fairing::AdHoc, + http::Status, + serde::json::{json, Value}, + Build, Request, Rocket, +}; use rocket_sync_db_pools::database; use serde::{Deserialize, Serialize}; @@ -26,6 +31,12 @@ pub(crate) mod schema; #[database("postgres_rb")] pub struct RbDbConn(diesel::PgConnection); +#[catch(default)] +fn default_catcher(status: Status, _: &Request) -> Value +{ + json!({"status": status.code, "message": ""}) +} + embed_migrations!(); async fn run_db_migrations(rocket: Rocket) -> Result, Rocket> @@ -88,6 +99,7 @@ fn rocket() -> _ )) .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user)) .attach(AdHoc::config::()) + .register("/", catchers![default_catcher]) .mount( "/api/auth", routes![auth::already_logged_in, auth::login, auth::refresh_token,],