Default catcher now returns json

pull/36/head
Jef Roosens 2021-09-01 17:29:39 +02:00
parent f50008ff99
commit 505907d3a1
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 19 additions and 14 deletions

View File

@ -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.",

View File

@ -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))
}
}
}

View File

@ -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<Build>) -> Result<Rocket<Build>, Rocket<Build>>
@ -88,6 +99,7 @@ fn rocket() -> _
))
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
.attach(AdHoc::config::<RbConfig>())
.register("/", catchers![default_catcher])
.mount(
"/api/auth",
routes![auth::already_logged_in, auth::login, auth::refresh_token,],