Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
rust-server.server-auth.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 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));
}
}