All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rust-server.context.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
use futures::future::BoxFuture;
use hyper::header::HeaderName;
use hyper::{Error, Request, Response, StatusCode, service::Service};
use url::form_urlencoded;
use std::default::Default;
use std::io;
use std::marker::PhantomData;
use std::task::{Poll, Context};
use swagger::auth::{AuthData, Authorization, Bearer, Scopes};
use swagger::{EmptyContext, Has, Pop, Push, XSpanIdString};
use crate::Api;

pub struct MakeAddContext {
    inner: T,
    marker: PhantomData,
}

impl MakeAddContext
where
    A: Default + Push,
    B: Push, Result = C>,
    C: Push, Result = D>,
{
    pub fn new(inner: T) -> MakeAddContext {
        MakeAddContext {
            inner,
            marker: PhantomData,
        }
    }
}

// Make a service that adds context.
impl Service for
    MakeAddContext
where
    Target: Send,
    A: Default + Push + Send,
    B: Push, Result = C>,
    C: Push, Result = D>,
    D: Send + 'static,
    T: Service + Send,
    T::Future: Send + 'static
{
    type Error = T::Error;
    type Response = AddContext;
    type Future = BoxFuture<'static, Result>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, target: Target) -> Self::Future {
        let service = self.inner.call(target);

        Box::pin(async move {
            Ok(AddContext::new(service.await?))
        })
    }
}

/// Middleware to add context data from the request
pub struct AddContext
where
    A: Default + Push,
    B: Push, Result = C>,
    C: Push, Result = D>
{
    inner: T,
    marker: PhantomData,
}

impl AddContext
where
    A: Default + Push,
    B: Push, Result = C>,
    C: Push, Result = D>,
{
    pub fn new(inner: T) -> Self {
        AddContext {
            inner,
            marker: PhantomData,
        }
    }
}

impl Service> for AddContext
    where
        A: Default + Push,
        B: Push, Result=C>,
        C: Push, Result=D>,
        D: Send + 'static,
        T: Service<(Request, D)>
{
    type Error = T::Error;
    type Future = T::Future;
    type Response = T::Response;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> {
        self.inner.poll_ready(cx)
    }


    fn call(&mut self, request: Request) -> Self::Future {
        let context = A::default().push(XSpanIdString::get_or_generate(&request));
        let headers = request.headers();

        {{#authMethods}}
        {{#isBasic}}
        {{#isBasicBasic}}
        {
            use swagger::auth::Basic;
            use std::ops::Deref;
            if let Some(basic) = swagger::auth::from_headers::(headers) {
                let auth_data = AuthData::Basic(basic);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);

                return self.inner.call((request, context))
            }
        }
        {{/isBasicBasic}}
        {{#isBasicBearer}}
        {
            use swagger::auth::Bearer;
            use std::ops::Deref;
            if let Some(bearer) = swagger::auth::from_headers::(headers) {
                let auth_data = AuthData::Bearer(bearer);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);

                return self.inner.call((request, context))
            }
        }
        {{/isBasicBearer}}
        {{/isBasic}}
        {{#isOAuth}}
        {
            use swagger::auth::Bearer;
            use std::ops::Deref;
            if let Some(bearer) = swagger::auth::from_headers::(headers) {
                let auth_data = AuthData::Bearer(bearer);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);

                return self.inner.call((request, context))
            }
        }
        {{/isOAuth}}
        {{#isApiKey}}
        {{#isKeyInHeader}}
        {
            use swagger::auth::api_key_from_header;

            if let Some(header) = api_key_from_header(headers, "{{{keyParamName}}}") {
                let auth_data = AuthData::ApiKey(header);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);

                return self.inner.call((request, context))
            }
        }
        {{/isKeyInHeader}}
        {{#isKeyInQuery}}
        {
            let key = form_urlencoded::parse(request.uri().query().unwrap_or_default().as_bytes())
                .filter(|e| e.0 == "{{{keyParamName}}}")
                .map(|e| e.1.clone().into_owned())
                .next();
            if let Some(key) = key {
                let auth_data = AuthData::ApiKey(key);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);

                return self.inner.call((request, context))
            }
        }
        {{/isKeyInQuery}}
        {{/isApiKey}}
        {{/authMethods}}

        let context = context.push(None::);
        let context = context.push(None::);

        self.inner.call((request, context))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy