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
//! Authorization and admission: the two steps an application supplies.
//!
//! Both are async because both can need I/O (a key fetch, a rate-limit
//! round trip). Decoding stays synchronous: it only reads bytes already in
//! memory.
use Future;
use Failure;
use Request;
use crateInvocation;
/// Decides who is calling and establishes the scope a handler runs with.
///
/// The returned `Scope` is evidence: it exists only because this policy
/// succeeded. Keep its fields private and its constructors restricted, so no
/// other code can fabricate one.
///
/// # Examples
///
/// ```
/// use davidrs::http::{Failure, Policy, Request, StatusCode};
/// use davidrs::Invocation;
///
/// pub struct Caller {
/// user: String,
/// }
///
/// struct UserHeader;
///
/// impl Policy for UserHeader {
/// type Scope = Caller;
///
/// async fn authorize(&self, request: &Request<'_>, _: &Invocation) -> Result<Caller, Failure> {
/// match request.header("x-user") {
/// Some(user) if !user.is_empty() => Ok(Caller { user: user.to_owned() }),
/// _ => Err(Failure::new(StatusCode::UNAUTHORIZED, "ERROR_UNAUTHORIZED", "Sign in first")),
/// }
/// }
/// }
/// ```
/// A policy for public endpoints: everyone is admitted and the scope is `()`.
;
/// A check that runs before the body is read: rate limits, quotas, anything
/// that should refuse a request before any parsing.
///
/// It returns the headers to add to the response, so a rate limiter reports
/// its budget on a success as well as on a refusal. Each name replaces the
/// handler's header of the same name; a name returned more than once, such as
/// `set-cookie`, keeps every value.
/// Admits every request and adds no headers; the default admission.
;