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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::collections::BTreeSet;
use std::io;
use std::marker::PhantomData;
use hyper;
use hyper::{Request, Response, Error};
use super::Push;
#[derive(Clone, Debug, PartialEq)]
pub enum Scopes {
Some(BTreeSet<String>),
All,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Authorization {
pub subject: String,
pub scopes: Scopes,
pub issuer: Option<String>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AuthData {
Basic(hyper::header::Basic),
Bearer(hyper::header::Bearer),
ApiKey(String),
}
impl AuthData {
pub fn basic(username: &str, password: &str) -> Self {
AuthData::Basic(hyper::header::Basic {
username: username.to_owned(),
password: Some(password.to_owned()),
})
}
pub fn bearer(token: &str) -> Self {
AuthData::Bearer(hyper::header::Bearer { token: token.to_owned() })
}
pub fn apikey(apikey: &str) -> Self {
AuthData::ApiKey(apikey.to_owned())
}
}
#[derive(Debug)]
pub struct AllowAllAuthenticator<T, C>
where
C: Push<Option<Authorization>>,
{
inner: T,
subject: String,
marker: PhantomData<C>,
}
impl<T, C> AllowAllAuthenticator<T, C>
where
C: Push<Option<Authorization>>,
{
pub fn new<U: Into<String>>(inner: T, subject: U) -> AllowAllAuthenticator<T, C> {
AllowAllAuthenticator {
inner,
subject: subject.into(),
marker: PhantomData,
}
}
}
impl<T, C> hyper::server::NewService for AllowAllAuthenticator<T, C>
where
C: Push<Option<Authorization>>,
T: hyper::server::NewService<Request=(Request, C::Result), Response=Response, Error=Error>,
{
type Request = (Request, C);
type Response = Response;
type Error = Error;
type Instance = AllowAllAuthenticator<T::Instance, C>;
fn new_service(&self) -> Result<Self::Instance, io::Error> {
self.inner.new_service().map(|s| AllowAllAuthenticator::new(s, self.subject.clone()))
}
}
impl<T, C> hyper::server::Service for AllowAllAuthenticator<T, C>
where
C : Push<Option<Authorization>>,
T: hyper::server::Service<Request=(Request, C::Result), Response=Response, Error=Error>,
{
type Request = (Request, C);
type Response = Response;
type Error = Error;
type Future = T::Future;
fn call(&self, (req, context): Self::Request) -> Self::Future {
let context = context.push(
Some(Authorization{
subject: self.subject.clone(),
scopes: Scopes::All,
issuer: None,
}));
self.inner.call((req, context))
}
}