use crate::{ApiGatewayProxyRequestContext, HeaderMap, HttpResponse, LambdaContext};
use async_trait::async_trait;
use std::future::Future;
#[async_trait]
pub trait Middleware {
type AuthOk: Send;
async fn authenticate(
&self,
operation_id: &str,
headers: &HeaderMap,
request_context: &ApiGatewayProxyRequestContext,
lambda_context: &LambdaContext,
) -> Result<Self::AuthOk, HttpResponse>;
async fn wrap_handler_authed<F, Fut>(
&self,
api_handler: F,
operation_id: &str,
headers: HeaderMap,
request_context: ApiGatewayProxyRequestContext,
lambda_context: LambdaContext,
auth_ok: Self::AuthOk,
) -> HttpResponse
where
F: FnOnce(HeaderMap, ApiGatewayProxyRequestContext, LambdaContext, Self::AuthOk) -> Fut + Send,
Fut: Future<Output = HttpResponse> + Send,
{
let _ = operation_id;
api_handler(headers, request_context, lambda_context, auth_ok).await
}
async fn wrap_handler_unauthed<F, Fut>(
&self,
api_handler: F,
operation_id: &str,
headers: HeaderMap,
request_context: ApiGatewayProxyRequestContext,
lambda_context: LambdaContext,
) -> HttpResponse
where
F: FnOnce(HeaderMap, ApiGatewayProxyRequestContext, LambdaContext) -> Fut + Send,
Fut: Future<Output = HttpResponse> + Send,
{
let _ = operation_id;
api_handler(headers, request_context, lambda_context).await
}
}
pub struct UnauthenticatedMiddleware;
#[async_trait]
impl Middleware for UnauthenticatedMiddleware {
type AuthOk = ();
async fn authenticate(
&self,
_operation_id: &str,
_headers: &HeaderMap,
_request_context: &ApiGatewayProxyRequestContext,
_lambda_context: &LambdaContext,
) -> Result<Self::AuthOk, HttpResponse> {
Ok(())
}
}