Add auth module

This commit is contained in:
Lennart
2023-09-07 18:50:21 +02:00
parent dcd6c01b12
commit e7d73e180a
6 changed files with 232 additions and 0 deletions

33
crates/auth/src/error.rs Normal file
View File

@@ -0,0 +1,33 @@
use actix_web::{http::StatusCode, HttpResponse};
use derive_more::{Display, Error};
#[derive(Debug, Display, Error)]
pub enum Error {
#[display(fmt = "Internal server error")]
InternalError,
#[display(fmt = "Not found")]
NotFound,
#[display(fmt = "Bad request")]
BadRequest,
Unauthorized,
}
impl actix_web::error::ResponseError for Error {
fn status_code(&self) -> StatusCode {
match *self {
Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
Self::NotFound => StatusCode::NOT_FOUND,
Self::BadRequest => StatusCode::BAD_REQUEST,
Self::Unauthorized => StatusCode::UNAUTHORIZED,
}
}
fn error_response(&self) -> HttpResponse {
match self {
Error::Unauthorized => HttpResponse::build(self.status_code())
.append_header(("WWW-Authenticate", "Basic"))
.body(self.to_string()),
_ => HttpResponse::build(self.status_code()).body(self.to_string()),
}
}
}