use crate::domain::auth::ScopePermission;
use axum::{extract::Request, http::StatusCode};
use log::warn;
pub trait ScopeValidationExt {
fn require_scope(&self, scope: ScopePermission) -> Result<(), StatusCode>;
fn has_scope(&self, scope: ScopePermission) -> bool;
}
impl ScopeValidationExt for Request {
fn require_scope(&self, scope: ScopePermission) -> Result<(), StatusCode> {
let auth_state = self
.extensions()
.get::<super::auth_middleware::AuthState>()
.ok_or(StatusCode::UNAUTHORIZED)?;
if auth_state.scope.has_permission(scope) {
Ok(())
} else {
warn!(
"Scope validation failed: required {:?} but not granted",
scope
);
Err(StatusCode::FORBIDDEN)
}
}
fn has_scope(&self, scope: ScopePermission) -> bool {
self.extensions()
.get::<super::auth_middleware::AuthState>()
.map(|s| s.scope.has_permission(scope))
.unwrap_or(false)
}
}