2023-09-30 22:20:32 +00:00
|
|
|
mod deadline;
|
2023-07-22 21:47:59 +00:00
|
|
|
mod metrics;
|
2023-09-30 21:26:43 +00:00
|
|
|
mod payload;
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2020-07-11 21:28:49 +00:00
|
|
|
use actix_web::{
|
2021-06-19 19:39:41 +00:00
|
|
|
dev::{Service, ServiceRequest, Transform},
|
2020-07-11 21:28:49 +00:00
|
|
|
http::StatusCode,
|
2021-06-19 19:39:41 +00:00
|
|
|
HttpResponse, ResponseError,
|
2020-07-11 21:28:49 +00:00
|
|
|
};
|
2021-09-04 19:20:31 +00:00
|
|
|
use std::{
|
2021-09-11 20:31:00 +00:00
|
|
|
future::{ready, Future, Ready},
|
|
|
|
pin::Pin,
|
2021-09-04 19:20:31 +00:00
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2020-06-14 02:28:06 +00:00
|
|
|
|
2023-09-30 22:20:32 +00:00
|
|
|
pub(crate) use self::deadline::Deadline;
|
2023-07-22 21:47:59 +00:00
|
|
|
pub(crate) use self::metrics::Metrics;
|
2023-09-30 21:26:43 +00:00
|
|
|
pub(crate) use self::payload::Payload;
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2020-07-11 21:28:49 +00:00
|
|
|
pub(crate) struct Internal(pub(crate) Option<String>);
|
|
|
|
pub(crate) struct InternalMiddleware<S>(Option<String>, S);
|
|
|
|
#[derive(Clone, Debug, thiserror::Error)]
|
|
|
|
#[error("Invalid API Key")]
|
2021-10-21 00:28:40 +00:00
|
|
|
pub(crate) struct ApiError;
|
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
#[project = InternalFutureProj]
|
|
|
|
#[project_replace = InternalFutureProjReplace]
|
|
|
|
pub(crate) enum InternalFuture<F> {
|
|
|
|
Internal {
|
|
|
|
#[pin]
|
|
|
|
future: F,
|
|
|
|
},
|
|
|
|
Error {
|
|
|
|
error: Option<ApiError>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 21:28:49 +00:00
|
|
|
|
|
|
|
impl ResponseError for ApiError {
|
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
StatusCode::UNAUTHORIZED
|
|
|
|
}
|
|
|
|
|
2021-06-19 19:39:41 +00:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
HttpResponse::build(self.status_code())
|
2021-04-17 19:59:42 +00:00
|
|
|
.content_type("application/json")
|
|
|
|
.body(
|
|
|
|
serde_json::to_string(&serde_json::json!({ "msg": self.to_string() }))
|
2021-09-12 15:42:44 +00:00
|
|
|
.unwrap_or_else(|_| r#"{"msg":"unauthorized"}"#.to_string()),
|
2021-04-17 19:59:42 +00:00
|
|
|
)
|
2020-07-11 21:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl<S> Transform<S, ServiceRequest> for Internal
|
2020-07-11 21:28:49 +00:00
|
|
|
where
|
2021-02-10 22:57:42 +00:00
|
|
|
S: Service<ServiceRequest, Error = actix_web::Error>,
|
2020-07-11 21:28:49 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = InternalMiddleware<S>;
|
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2021-09-04 19:20:31 +00:00
|
|
|
ready(Ok(InternalMiddleware(self.0.clone(), service)))
|
2020-07-11 21:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl<S> Service<ServiceRequest> for InternalMiddleware<S>
|
2020-07-11 21:28:49 +00:00
|
|
|
where
|
2021-02-10 22:57:42 +00:00
|
|
|
S: Service<ServiceRequest, Error = actix_web::Error>,
|
2020-07-11 21:28:49 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
2021-10-21 00:28:40 +00:00
|
|
|
type Future = InternalFuture<S::Future>;
|
2020-07-11 21:28:49 +00:00
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-07-11 21:28:49 +00:00
|
|
|
self.1.poll_ready(cx)
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
2020-07-11 21:28:49 +00:00
|
|
|
if let Some(value) = req.headers().get("x-api-token") {
|
2021-10-28 05:17:37 +00:00
|
|
|
if let (Ok(header), Some(api_key)) = (value.to_str(), &self.0) {
|
|
|
|
if header == api_key {
|
|
|
|
return InternalFuture::Internal {
|
|
|
|
future: self.1.call(req),
|
|
|
|
};
|
|
|
|
}
|
2020-07-11 21:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:28:40 +00:00
|
|
|
InternalFuture::Error {
|
|
|
|
error: Some(ApiError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, T, E> Future for InternalFuture<F>
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<T, E>>,
|
|
|
|
E: From<ApiError>,
|
|
|
|
{
|
|
|
|
type Output = F::Output;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
match self.as_mut().project() {
|
|
|
|
InternalFutureProj::Internal { future } => future.poll(cx),
|
|
|
|
InternalFutureProj::Error { error } => Poll::Ready(Err(error.take().unwrap().into())),
|
|
|
|
}
|
2020-07-11 21:28:49 +00:00
|
|
|
}
|
|
|
|
}
|