2021-09-04 19:20:31 +00:00
|
|
|
use crate::stream::LocalBoxFuture;
|
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::{
|
|
|
|
future::{ready, Ready},
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2020-06-14 02:28:06 +00:00
|
|
|
use tracing_futures::{Instrument, Instrumented};
|
2020-06-14 18:56:42 +00:00
|
|
|
use uuid::Uuid;
|
2020-06-14 02:28:06 +00:00
|
|
|
|
|
|
|
pub(crate) struct Tracing;
|
|
|
|
|
|
|
|
pub(crate) struct TracingMiddleware<S> {
|
|
|
|
inner: S,
|
|
|
|
}
|
|
|
|
|
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")]
|
|
|
|
struct ApiError;
|
|
|
|
|
|
|
|
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() }))
|
|
|
|
.unwrap_or(r#"{"msg":"unauthorized"}"#.to_string()),
|
|
|
|
)
|
2020-07-11 21:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl<S, Request> Transform<S, Request> for Tracing
|
2020-06-14 02:28:06 +00:00
|
|
|
where
|
2021-02-10 22:57:42 +00:00
|
|
|
S: Service<Request>,
|
2020-06-14 02:28:06 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = TracingMiddleware<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(TracingMiddleware { inner: service }))
|
2020-06-14 02:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl<S, Request> Service<Request> for TracingMiddleware<S>
|
2020-06-14 02:28:06 +00:00
|
|
|
where
|
2021-02-10 22:57:42 +00:00
|
|
|
S: Service<Request>,
|
2020-06-14 02:28:06 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Future = Instrumented<S::Future>;
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-06-14 02:28:06 +00:00
|
|
|
self.inner.poll_ready(cx)
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
fn call(&self, req: Request) -> Self::Future {
|
2020-06-14 18:56:42 +00:00
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
2020-06-14 02:28:06 +00:00
|
|
|
self.inner
|
|
|
|
.call(req)
|
2020-06-14 18:56:42 +00:00
|
|
|
.instrument(tracing::info_span!("request", ?uuid))
|
2020-06-14 02:28:06 +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;
|
|
|
|
type Future = LocalBoxFuture<'static, Result<S::Response, S::Error>>;
|
|
|
|
|
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") {
|
2020-12-26 02:39:19 +00:00
|
|
|
if value.to_str().is_ok() && value.to_str().ok() == self.0.as_deref() {
|
2020-07-11 21:28:49 +00:00
|
|
|
let fut = self.1.call(req);
|
|
|
|
return Box::pin(async move { fut.await });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::pin(async move { Err(ApiError.into()) })
|
|
|
|
}
|
|
|
|
}
|