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

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

use std::io;
use std::marker::PhantomData;
use std::default::Default;
use hyper;
use hyper::{Request, Response, Error, StatusCode};
use server::url::form_urlencoded;
use swagger::auth::{Authorization, AuthData, Scopes};
use swagger::{Has, Pop, Push, XSpanIdString};
use swagger::headers::SafeHeaders;
use Api;

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

impl NewAddContext
    where
        A: Default + Push,
        B: Push, Result=C>,
        C: Push, Result=D>,
        T: hyper::server::NewService + 'static,
{
    pub fn new(inner: T) -> NewAddContext {
        NewAddContext {
            inner,
            marker: PhantomData,
        }
    }
}

impl hyper::server::NewService for NewAddContext
    where
        A: Default + Push,
        B: Push, Result=C>,
        C: Push, Result=D>,
        T: hyper::server::NewService + 'static,
{
    type Request = Request;
    type Response = Response;
    type Error = Error;
    type Instance = AddContext;

    fn new_service(&self) -> Result {
        self.inner.new_service().map(|s| AddContext::new(s))
    }
}

/// Middleware to extract authentication data from request
pub struct AddContext
{
    inner: T,
    marker: PhantomData,
}

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

impl hyper::server::Service for AddContext
    where
        A: Default + Push,
        B: Push, Result=C>,
        C: Push, Result=D>,
        T: hyper::server::Service,
{
    type Request = Request;
    type Response = Response;
    type Error = Error;
    type Future = T::Future;

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

        {{#authMethods}}
        {{#isBasic}}
        {
            use hyper::header::{Authorization as HyperAuth, Basic, Bearer};
            use std::ops::Deref;
            if let Some(basic) = req.headers().safe_get::>() {
                let auth_data = AuthData::Basic(basic.deref().clone());
                let context = context.push(Some(auth_data));
                let context = context.push(None::);
                return self.inner.call((req, context));
            }
        }
        {{/isBasic}}
        {{#isOAuth}}
        {
            use hyper::header::{Authorization as HyperAuth, Basic, Bearer};
            use std::ops::Deref;
            if let Some(bearer) = req.headers().safe_get::>() {
                let auth_data = AuthData::Bearer(bearer.deref().clone());
                let context = context.push(Some(auth_data));
                let context = context.push(None::);
                return self.inner.call((req, context));
            }
        }
        {{/isOAuth}}
        {{#isApiKey}}
        {{#isKeyInHeader}}
        {
            header! { (ApiKey{{-index}}, "{{{keyParamName}}}") => [String] }
            if let Some(header) = req.headers().safe_get::() {
                let auth_data = AuthData::ApiKey(header.0);
                let context = context.push(Some(auth_data));
                let context = context.push(None::);
                return self.inner.call((req, context));
            }
        }
        {{/isKeyInHeader}}
        {{#isKeyInQuery}}
        {
            let key = form_urlencoded::parse(req.query().unwrap_or_default().as_bytes())
                .filter(|e| e.0 == "api_key_query")
                .map(|e| e.1.clone().into_owned())
                .nth(0);
            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((req, context));
            }
        }
        {{/isKeyInQuery}}
        {{/isApiKey}}
        {{/authMethods}}

        let context = context.push(None::);
        let context = context.push(None::);
        return self.inner.call((req, context));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy