summaryrefslogtreecommitdiffstats
path: root/rust/fatcat-api/src/server/auth.rs
blob: 48dfd7d6fb6a28bcc86c7f9b8e1d6e1b6fcca026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use Api;
use hyper;
use hyper::{Error, Request, Response, StatusCode};
use server::url::form_urlencoded;
use std::io;
use swagger::auth::{AuthData, Authorization, Scopes};

pub struct NewService<T>
where
    T: hyper::server::NewService<
        Request = (Request, Option<AuthData>),
        Response = Response,
        Error = Error,
    >,
{
    inner: T,
}

impl<T> NewService<T>
where
    T: hyper::server::NewService<
            Request = (Request, Option<AuthData>),
            Response = Response,
            Error = Error,
        >
        + 'static,
{
    pub fn new(inner: T) -> NewService<T> {
        NewService { inner }
    }
}

impl<T> hyper::server::NewService for NewService<T>
where
    T: hyper::server::NewService<
            Request = (Request, Option<AuthData>),
            Response = Response,
            Error = Error,
        >
        + 'static,
{
    type Request = Request;
    type Response = Response;
    type Error = Error;
    type Instance = Service<T::Instance>;

    fn new_service(&self) -> Result<Self::Instance, io::Error> {
        self.inner.new_service().map(|s| Service::new(s))
    }
}

/// Middleware to extract authentication data from request
pub struct Service<T>
where
    T: hyper::server::Service<
        Request = (Request, Option<AuthData>),
        Response = Response,
        Error = Error,
    >,
{
    inner: T,
}

impl<T> Service<T>
where
    T: hyper::server::Service<
        Request = (Request, Option<AuthData>),
        Response = Response,
        Error = Error,
    >,
{
    pub fn new(inner: T) -> Service<T> {
        Service { inner }
    }
}

impl<T> hyper::server::Service for Service<T>
where
    T: hyper::server::Service<
        Request = (Request, Option<AuthData>),
        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 {
        return self.inner.call((req, None));
    }
}