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

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

There is a newer version: 3.0.0-rc1
Show newest version
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 Api;

pub struct NewService
    where
        C: Default + Push,
        C::Result: Push>,
        T: hyper::server::NewService>>::Result), Response = Response, Error = Error>,
{
    inner: T,
    marker: PhantomData,
}

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

impl hyper::server::NewService for NewService
    where
        C: Default + Push,
        C::Result: Push>,
        T: hyper::server::NewService>>::Result), Response = Response, Error = Error> + 'static,
{
    type Request = Request;
    type Response = Response;
    type Error = Error;
    type Instance = Service;

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

/// Middleware to extract authentication data from request
pub struct Service
    where
        C: Default + Push,
        C::Result: Push>,
        T: hyper::server::Service>>::Result), Response = Response, Error = Error>,
{
    inner: T,
    marker: PhantomData,
}

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

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

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

        {{#authMethods}}
        {{#isBasic}}
        {
            use hyper::header::{Authorization, Basic, Bearer};
            use std::ops::Deref;
            if let Some(basic) = req.headers().get::>().cloned() {
                let auth_data = AuthData::Basic(basic.deref().clone());
                let context = context.push(Some(auth_data));
                return self.inner.call((req, context));
            }
        }
        {{/isBasic}}
        {{#isOAuth}}
        {
            use hyper::header::{Authorization, Basic, Bearer};
            use std::ops::Deref;
            if let Some(bearer) = req.headers().get::>().cloned() {
                let auth_data = AuthData::Bearer(bearer.deref().clone());
                let context = context.push(Some(auth_data));
                return self.inner.call((req, context));
            }
        }
        {{/isOAuth}}
        {{#isApiKey}}
        {{#isKeyInHeader}}
        {
            header! { (ApiKey{{-index}}, "{{keyParamName}}") => [String] }
            if let Some(header) = req.headers().get::().cloned() {
                let auth_data = AuthData::ApiKey(header.0);
                let context = context.push(Some(auth_data));
                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));
                return self.inner.call((req, context));
            }
        }
        {{/isKeyInQuery}}
        {{/isApiKey}}
        {{/authMethods}}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy