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
//! Authorization: the [`Policy`] trait and the [`Auth::authorize`] seam.
//!
//! Authentication answers "who is this?" ([`crate::auth::extract`]);
//! authorization answers "may they do this?". The two are deliberately
//! separate steps -- loading a user, or binding a model, never authorizes
//! anything on its own.
//!
//! Laravel calls this pair Gate and Policy. Arcature has no Gate: there is no
//! global registry to look a policy up in, so [`Auth::authorize`] names the
//! policy type at the call site and the compiler checks that it matches the
//! resource and the user.
use ;
use crateAuthUser;
use crateAuth;
/// A typed authorization error.
/// A policy for resource type `M`.
///
/// The application implements this for its policy type. The `check` method
/// receives the authenticated user, an action name (e.g. `"view"`,
/// `"update"`), and the resource, and returns whether the action is allowed.
///
/// Authorization stays explicit: a policy is a type that decides whether a
/// user may perform an action on a resource. The application writes the
/// policy methods; the framework provides the [`Auth::authorize`] seam.
///
/// # Example
///
/// ```
/// use arcature::prelude::*;
///
/// struct User {
/// id: i64,
/// }
///
/// impl AuthUser for User {
/// type Id = i64;
///
/// fn id(&self) -> &i64 {
/// &self.id
/// }
/// }
///
/// struct Link {
/// id: i64,
/// user_id: i64,
/// }
///
/// pub struct LinkPolicy;
///
/// impl Policy<Link> for LinkPolicy {
/// type User = User;
///
/// fn check(user: &User, action: &str, link: &Link) -> bool {
/// match action {
/// "view" => true,
/// "update" => user.id == link.user_id,
/// _ => false,
/// }
/// }
/// }
///
/// async fn show(auth: Auth<User>, link: Bound<Link>) -> Result<String> {
/// let link = link.into_inner();
/// // Both type parameters are named -- the resource first, then the
/// // policy. Rust has no partial turbofish, so `authorize::<LinkPolicy>`
/// // does not compile.
/// //
/// // `AuthzError` does not convert into the framework `Error`, so the
/// // handler decides what a denial looks like. `forbidden` gives it an
/// // RFC 9457 body; returning `Result<_, AuthzError>` instead gives a
/// // bare 403.
/// auth.authorize::<Link, LinkPolicy>("view", &link)
/// .map_err(|_| forbidden("not your link"))?;
///
/// Ok(format!("link {}", link.id))
/// }
/// # fn main() {}
/// ```
///
/// # Binding does NOT imply authorization
///
/// `Bound<T>` loads the model; `Auth::authorize` checks the policy. These are
/// separate steps. Authorization is never automatic.