-
Notifications
You must be signed in to change notification settings - Fork 38
/
routes.rs
329 lines (301 loc) · 11.2 KB
/
routes.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::sync::Arc;
use warp::Filter;
use crate::{invoice::HealthResponse, server::filters, signature::KeyRing};
/// A helper function that aggregates all routes into a complete API filter. If you only wish to
/// serve specific endpoints or versions, you can assemble them with the individual submodules
pub fn api<P, I, Authn, Authz, S>(
store: P,
index: I,
authn: Authn,
authz: Authz,
secret_store: S,
verification_strategy: crate::VerificationStrategy,
keyring: KeyRing,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: crate::provider::Provider + Clone + Send + Sync + 'static,
I: crate::search::Search + Clone + Send + Sync + 'static,
S: crate::invoice::signature::SecretKeyStorage + Clone + Send + Sync + 'static,
Authn: crate::authn::Authenticator + Clone + Send + Sync + 'static,
Authz: crate::authz::Authorizer + Clone + Send + Sync + 'static,
{
let health = warp::path("healthz").map(|| {
warp::reply::json(&HealthResponse {
status: "OK".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
})
});
// Use an Arc to avoid a possibly expensive clone of the keyring on every API call
let wrapped_keyring = Arc::new(keyring);
warp::path("v1")
.and(filters::authenticate_and_authorize(authn.clone(), authz))
.untuple_one()
.and(
v1::invoice::query(index)
.or(v1::invoice::create_toml(
store.clone(),
secret_store.clone(),
verification_strategy.clone(),
wrapped_keyring.clone(),
))
.boxed()
.or(v1::invoice::create_json(
store.clone(),
secret_store.clone(),
verification_strategy,
wrapped_keyring,
))
.boxed()
.or(v1::invoice::get(store.clone()))
.boxed()
.or(v1::invoice::head(store.clone()))
.boxed()
.or(v1::invoice::yank(store.clone()))
.boxed()
.or(v1::parcel::create(store.clone()))
.boxed()
.or(v1::parcel::get(store.clone()))
.boxed()
.or(v1::parcel::head(store.clone()))
.boxed()
.or(v1::relationships::get_missing_parcels(store))
.boxed()
.or(v1::auth::login(
authn.client_id().to_owned(),
authn.auth_url().to_owned(),
authn.token_url().to_owned(),
))
.boxed()
.or(v1::keyring::host_keys(secret_store))
.boxed(),
)
.or(health)
.boxed()
.recover(filters::handle_invalid_request_path)
.recover(filters::handle_authn_rejection)
.recover(filters::handle_authz_rejection)
.with(warp::trace::request())
}
pub mod v1 {
use crate::provider::Provider;
use crate::search::Search;
use crate::server::handlers::v1::*;
use crate::server::{filters, routes::with_store};
use warp::Filter;
pub mod auth {
use super::*;
use crate::LoginParams;
pub fn login(
provider_client_id: String,
device_auth_url: String,
token_url: String,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("login")
.and(warp::get())
.and(warp::query::<LoginParams>())
.and(warp::any().map(move || provider_client_id.clone()))
.and(warp::any().map(move || device_auth_url.clone()))
.and(warp::any().map(move || token_url.clone()))
.and(warp::header::optional::<String>("accept"))
.and_then(crate::server::handlers::v1::login)
.boxed()
}
}
pub mod invoice {
use crate::{
server::routes::with_secret_store,
signature::{KeyRing, SecretKeyStorage},
};
use super::*;
use std::sync::Arc;
pub fn query<S>(
index: S,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: Search + Clone + Send + Sync,
{
warp::path("_q")
.and(warp::get().or(warp::head()).unify())
.and(warp::query::<crate::QueryOptions>())
.and(warp::any().map(move || index.clone()))
.and(warp::header::optional::<String>("accept"))
.and_then(query_invoices)
}
pub fn create_toml<P, S>(
store: P,
secret_store: S,
verification_strategy: crate::VerificationStrategy,
keyring: Arc<KeyRing>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
S: SecretKeyStorage + Clone + Send + Sync,
{
warp::path("_i")
.and(warp::path::end())
.and(warp::post())
.and(with_store(store))
.and(with_secret_store(secret_store))
.and(warp::any().map(move || verification_strategy.clone()))
.and(warp::any().map(move || keyring.clone()))
.and(filters::toml())
.and(warp::header::optional::<String>("accept"))
.and_then(create_invoice)
.recover(filters::handle_deserialize_rejection)
}
pub fn create_json<P, S>(
store: P,
secret_store: S,
verification_strategy: crate::VerificationStrategy,
keyring: Arc<KeyRing>,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
S: SecretKeyStorage + Clone + Send + Sync,
{
warp::path("_i")
.and(warp::path::end())
.and(warp::post())
.and(with_store(store))
.and(with_secret_store(secret_store))
.and(warp::any().map(move || verification_strategy.clone()))
.and(warp::any().map(move || keyring.clone()))
.and(warp::body::json())
.and(warp::header::optional::<String>("accept"))
.and_then(create_invoice)
.recover(filters::handle_deserialize_rejection)
}
// The GET and HEAD endpoints handle both parcels and invoices through the request router function
pub fn get<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
filters::invoice()
.and(warp::get())
.and(warp::query::<filters::InvoiceQuery>())
.and(with_store(store))
.and(warp::header::optional::<String>("accept"))
.and_then(get_invoice)
}
pub fn head<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
filters::invoice()
.and(warp::head())
.and(warp::query::<filters::InvoiceQuery>())
.and(with_store(store))
.and(warp::header::optional::<String>("accept"))
.and_then(head_invoice)
}
pub fn yank<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
warp::path("_i")
.and(warp::path::tail())
.and(warp::delete())
.and(with_store(store))
.and(warp::header::optional::<String>("accept"))
.and_then(yank_invoice)
}
}
pub mod parcel {
use super::*;
pub fn create<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
filters::parcel()
.and(warp::post())
.and(warp::body::stream())
.and(with_store(store))
.and(warp::header::optional::<String>("accept"))
.and_then(create_parcel)
}
pub fn get<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
filters::parcel()
.and(warp::get())
.and(with_store(store))
.and_then(get_parcel)
}
pub fn head<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
filters::parcel()
.and(warp::head())
.and(with_store(store))
.and_then(head_parcel)
}
}
pub mod relationships {
use super::*;
pub fn get_missing_parcels<P>(
store: P,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
P: Provider + Clone + Send + Sync,
{
// For some reason, using the `path!` macro here was causing matching problems
warp::path("_r")
.and(warp::path("missing"))
.and(warp::path::tail())
.and(warp::get())
.and(with_store(store))
.and(warp::header::optional::<String>("accept"))
.and_then(get_missing)
}
}
pub mod keyring {
use super::*;
use crate::server::routes::with_secret_store;
use crate::signature::SecretKeyStorage;
pub fn host_keys<S>(
secret_store: S,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
where
S: SecretKeyStorage + Clone + Send + Sync,
{
warp::path("bindle-keys")
.and(warp::get().or(warp::head()).unify())
.and(warp::query::<crate::KeyOptions>())
.and(with_secret_store(secret_store))
.and(warp::header::optional::<String>("accept"))
.and_then(bindle_keys)
}
}
}
pub(crate) fn with_store<P>(
store: P,
) -> impl Filter<Extract = (P,), Error = std::convert::Infallible> + Clone
where
P: crate::provider::Provider + Clone + Send,
{
// We have to clone for this to be Fn instead of FnOnce
warp::any().map(move || store.clone())
}
pub(crate) fn with_secret_store<P>(
store: P,
) -> impl Filter<Extract = (P,), Error = std::convert::Infallible> + Clone
where
P: crate::invoice::signature::SecretKeyStorage + Clone + Send,
{
// We have to clone for this to be Fn instead of FnOnce
warp::any().map(move || store.clone())
}