From d1aea6c45695e8726272fbf6f88cc46112c71539 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sun, 11 Apr 2021 13:00:34 +0530 Subject: [PATCH] conditional registration blocking --- src/api/v1/auth.rs | 3 +++ src/errors.rs | 27 ++++++--------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/api/v1/auth.rs b/src/api/v1/auth.rs index 6939b2fe..194b581a 100644 --- a/src/api/v1/auth.rs +++ b/src/api/v1/auth.rs @@ -48,6 +48,9 @@ pub async fn signup( payload: web::Json, data: web::Data, ) -> ServiceResult { + if !crate::SETTINGS.server.allow_registration { + Err(ServiceError::ClosedForRegistration)? + } let username = data.creds.username(&payload.username)?; let hash = data.creds.password(&payload.password)?; data.creds.email(Some(&payload.email))?; diff --git a/src/errors.rs b/src/errors.rs index cc138560..2a0cf4a5 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -18,7 +18,6 @@ use std::convert::From; use actix_web::{ - client::SendRequestError, dev::HttpResponseBuilder, error::ResponseError, http::{header, StatusCode}, @@ -39,6 +38,11 @@ pub enum ServiceError { #[display(fmt = "internal server error")] InternalServerError, + #[display( + fmt = "This server is is closed for registration. Contact admin if this is unexpecter" + )] + ClosedForRegistration, + #[display(fmt = "The value you entered for email is not an email")] //405j NotAnEmail, #[display(fmt = "The value you entered for URL is not a URL")] //405j @@ -74,17 +78,12 @@ pub enum ServiceError { #[display(fmt = "Username not available")] UsernameTaken, /// when the a token name is already taken - #[display(fmt = "token name not available")] - TokenNameTaken, /// token not found #[display(fmt = "Token not found. Is token registered?")] TokenNotFound, #[display(fmt = "{}", _0)] CaptchaError(CaptchaError), - - #[display(fmt = "Couldn't reach your server. If Problem presists, contact support")] - ClientServerUnreachable, } #[derive(Serialize, Deserialize)] @@ -107,6 +106,7 @@ impl ResponseError for ServiceError { #[cfg(not(tarpaulin_include))] fn status_code(&self) -> StatusCode { match self { + ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN, ServiceError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR, ServiceError::NotAnEmail => StatusCode::BAD_REQUEST, ServiceError::NotAUrl => StatusCode::BAD_REQUEST, @@ -123,9 +123,7 @@ impl ResponseError for ServiceError { ServiceError::UsernameTaken => StatusCode::BAD_REQUEST, - ServiceError::TokenNameTaken => StatusCode::BAD_REQUEST, ServiceError::TokenNotFound => StatusCode::NOT_FOUND, - ServiceError::ClientServerUnreachable => StatusCode::SERVICE_UNAVAILABLE, ServiceError::CaptchaError(e) => match e { CaptchaError::MailboxError => StatusCode::INTERNAL_SERVER_ERROR, _ => StatusCode::BAD_REQUEST, @@ -156,19 +154,6 @@ impl From for ServiceError { } } -impl From for ServiceError { - fn from(e: SendRequestError) -> ServiceError { - debug!("{:?}", &e); - match e { - SendRequestError::Url(_) => ServiceError::NotAUrl, - SendRequestError::Send(_) => ServiceError::InternalServerError, - SendRequestError::Response(_) => ServiceError::InternalServerError, - SendRequestError::Body(_) => ServiceError::InternalServerError, - _ => ServiceError::ClientServerUnreachable, - } - } -} - impl From for ServiceError { fn from(_: ParseError) -> ServiceError { ServiceError::NotAUrl