use super::*;
#[macro_export]
macro_rules! impl_auth_plugin {
(@pat get) => { $crate::HttpMethod::Get };
(@pat post) => { $crate::HttpMethod::Post };
(@pat put) => { $crate::HttpMethod::Put };
(@pat delete) => { $crate::HttpMethod::Delete };
(@pat patch) => { $crate::HttpMethod::Patch };
(@pat head) => { $crate::HttpMethod::Head };
(@route get) => { $crate::AuthRoute::get };
(@route post) => { $crate::AuthRoute::post };
(@route put) => { $crate::AuthRoute::put };
(@route delete) => { $crate::AuthRoute::delete };
(
$plugin:ty, $name:expr;
routes {
$( $method:ident $path:literal => $handler:ident, $op_id:literal );* $(;)?
}
$( extra { $($extra:tt)* } )?
) => {
#[::async_trait::async_trait]
impl<S: $crate::AuthSchema> $crate::AuthPlugin<S> for $plugin {
fn name(&self) -> &'static str { $name }
fn routes(&self) -> Vec<$crate::AuthRoute> {
vec![
$( $crate::AuthRoute::new($crate::impl_auth_plugin!(@pat $method), $path, $op_id), )*
]
}
async fn on_request(
&self,
req: &$crate::AuthRequest,
ctx: &$crate::AuthContext<S>,
) -> $crate::AuthResult<Option<$crate::AuthResponse>> {
match (req.method(), req.path()) {
$(
($crate::impl_auth_plugin!(@pat $method), $path) => {
Ok(Some(self.$handler(req, ctx).await?))
}
)*
_ => Ok(None),
}
}
$( $($extra)* )?
}
};
}
#[derive(Clone, Debug, Default)]
pub struct ResolvedEndpoint {
pub path: String,
pub params: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub struct AuthRoute {
pub path: String,
pub context_path: Option<String>,
pub method: HttpMethod,
pub operation_id: String,
}
impl AuthRoute {
#[must_use]
pub fn new(
method: HttpMethod,
path: impl Into<String>,
operation_id: impl Into<String>,
) -> Self {
Self {
path: path.into(),
context_path: None,
method,
operation_id: operation_id.into(),
}
}
#[must_use]
pub fn with_context_path(mut self, path: impl Into<String>) -> Self {
self.context_path = Some(path.into());
self
}
#[must_use]
pub fn get(path: impl Into<String>, operation_id: impl Into<String>) -> Self {
Self::new(HttpMethod::Get, path, operation_id)
}
#[must_use]
pub fn post(path: impl Into<String>, operation_id: impl Into<String>) -> Self {
Self::new(HttpMethod::Post, path, operation_id)
}
#[must_use]
pub fn put(path: impl Into<String>, operation_id: impl Into<String>) -> Self {
Self::new(HttpMethod::Put, path, operation_id)
}
#[must_use]
pub fn delete(path: impl Into<String>, operation_id: impl Into<String>) -> Self {
Self::new(HttpMethod::Delete, path, operation_id)
}
}