use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AuthorizationResponse {
allowed: bool,
message: Option<String>,
code: Option<String>,
status: Option<StatusCode>,
}
impl AuthorizationResponse {
pub fn allow() -> Self {
Self { allowed: true, message: None, code: None, status: None }
}
pub fn deny() -> Self {
Self { allowed: false, message: None, code: None, status: None }
}
pub fn deny_with_status(status: StatusCode) -> Self {
Self::deny().with_status(status)
}
pub fn deny_as_not_found() -> Self {
Self::deny_with_status(StatusCode::NOT_FOUND)
}
pub fn with_message(mut self, message: impl Into<String>) -> Self {
self.message = Some(message.into());
self
}
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = Some(code.into());
self
}
pub fn with_status(mut self, status: StatusCode) -> Self {
self.status = Some(status);
self
}
pub fn allowed(&self) -> bool {
self.allowed
}
pub fn denied(&self) -> bool {
!self.allowed
}
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
pub fn code(&self) -> Option<&str> {
self.code.as_deref()
}
pub fn status(&self) -> Option<StatusCode> {
self.status
}
pub fn authorize(self) -> Result<Self, AuthorizationError> {
if self.allowed() { Ok(self) } else { Err(AuthorizationError::Denied(self)) }
}
}
pub trait IntoAuthorizationResponse {
fn into_authorization_response(self) -> AuthorizationResponse;
#[doc(hidden)]
fn into_on_demand_response(self, _allow_when: bool) -> AuthorizationResponse
where
Self: Sized,
{
self.into_authorization_response()
}
}
impl IntoAuthorizationResponse for AuthorizationResponse {
fn into_authorization_response(self) -> AuthorizationResponse {
self
}
}
impl IntoAuthorizationResponse for bool {
fn into_authorization_response(self) -> AuthorizationResponse {
if self { AuthorizationResponse::allow() } else { AuthorizationResponse::deny() }
}
fn into_on_demand_response(self, allow_when: bool) -> AuthorizationResponse {
(self == allow_when).into_authorization_response()
}
}
#[derive(Debug, thiserror::Error)]
pub enum AuthorizationError {
#[error("{message}", message = denial_message(.0))]
Denied(AuthorizationResponse),
#[error(transparent)]
Gate(#[from] super::GateError),
}
impl AuthorizationError {
pub fn response(&self) -> Option<&AuthorizationResponse> {
match self {
Self::Denied(response) => Some(response),
Self::Gate(_) => None,
}
}
}
impl IntoResponse for AuthorizationError {
fn into_response(self) -> Response {
match self {
Self::Denied(response) => {
let status = response.status.unwrap_or(StatusCode::FORBIDDEN);
let message = denial_message(&response).to_owned();
(status, message).into_response()
}
Self::Gate(error) => {
tracing::error!(error = %error, "authorization gate invocation failed");
(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error").into_response()
}
}
}
}
fn denial_message(response: &AuthorizationResponse) -> &str {
response.message.as_deref().unwrap_or("This action is unauthorized.")
}