aurelin-framework 0.4.1

Aurelin is a Rust translation of Laravel
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::{
    convert::Infallible,
    error::Error,
    sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    },
};

use aurelin_framework::{
    authorization::{
        AuthorizationError, AuthorizationResponse, AuthorizeLayer, Gate, GateError, Policy, PolicyRegistrar,
    },
    axum::{
        Router,
        body::{Body, to_bytes},
        http::{Request, StatusCode, header},
        response::IntoResponse,
        routing::{get, post},
    },
};
use axum_login::{AuthManagerLayerBuilder, AuthSession, AuthUser, AuthnBackend};
use tower::ServiceExt;
use tower_sessions::{MemoryStore, SessionManagerLayer};

#[derive(Clone, Debug)]
struct User {
    id: i64,
    administrator: bool,
}

#[derive(Clone, Debug)]
struct Article {
    owner_id: i64,
    published: bool,
}

#[derive(Clone)]
struct ArticlePolicy;

impl Policy<User> for ArticlePolicy {
    fn before(&self, user: &User, _ability: &str) -> Option<AuthorizationResponse> {
        user.administrator.then(AuthorizationResponse::allow)
    }

    fn register(&self, policy: &mut PolicyRegistrar<'_, User, Self>) {
        policy.define("create", |_policy, _user: User, (): ()| async move { false });
        policy.define("update", |_policy, user: User, article: Article| async move {
            if user.id == article.owner_id {
                AuthorizationResponse::allow()
            } else {
                AuthorizationResponse::deny().with_message("You do not own this article.").with_code("not-owner")
            }
        });
        policy.define("view", |_policy, _user: User, article: Article| async move {
            if article.published { AuthorizationResponse::allow() } else { AuthorizationResponse::deny_as_not_found() }
        });
    }
}

fn policy_gate() -> Gate<User> {
    let mut builder = Gate::builder();
    builder.policy(ArticlePolicy);
    builder.build()
}

#[tokio::test]
async fn policies_authorize_users_and_preserve_denial_details() -> Result<(), Box<dyn Error>> {
    let gate = policy_gate();
    let owner = User { id: 1, administrator: false };
    let stranger = User { id: 2, administrator: false };
    let administrator = User { id: 3, administrator: true };
    let article = Article { owner_id: owner.id, published: false };

    assert!(gate.for_user(Some(&owner)).allows("update", &article).await?);
    assert!(gate.for_user(Some(&administrator)).allows("update", &article).await?);
    let denied = gate.for_user(Some(&stranger)).inspect("update", &article).await?;
    assert!(denied.denied());
    assert_eq!(denied.message(), Some("You do not own this article."));
    assert_eq!(denied.code(), Some("not-owner"));

    let concealed = gate.for_user(Some(&owner)).inspect("view", &article).await?;
    assert_eq!(concealed.status(), Some(StatusCode::NOT_FOUND));
    assert!(gate.for_user(Some(&administrator)).allows("create", &()).await?);
    assert!(gate.for_user(Some(&owner)).denies("create", &()).await?);
    Ok(())
}

#[tokio::test]
async fn a_policy_before_response_skips_the_ability_method() -> Result<(), Box<dyn Error>> {
    #[derive(Clone)]
    struct CountingPolicy(Arc<AtomicUsize>);

    impl Policy<User> for CountingPolicy {
        fn before(&self, user: &User, ability: &str) -> Option<AuthorizationResponse> {
            (user.administrator && ability == "delete").then(AuthorizationResponse::allow)
        }

        fn register(&self, policy: &mut PolicyRegistrar<'_, User, Self>) {
            policy.define("delete", |policy, _user: User, _article: Article| async move {
                policy.0.fetch_add(1, Ordering::SeqCst);
                false
            });
        }
    }

    let calls = Arc::new(AtomicUsize::new(0));
    let mut builder = Gate::<User>::builder();
    builder.policy(CountingPolicy(Arc::clone(&calls)));
    let gate = builder.build();
    let administrator = User { id: 1, administrator: true };
    let article = Article { owner_id: 2, published: false };

    assert!(gate.for_user(Some(&administrator)).allows("delete", &article).await?);
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    Ok(())
}

#[tokio::test]
async fn action_authorization_prevents_denied_resource_mutation() -> Result<(), Box<dyn Error>> {
    let gate = policy_gate();
    let owner = User { id: 1, administrator: false };
    let stranger = User { id: 2, administrator: false };
    let article = Article { owner_id: owner.id, published: false };
    let mutations = AtomicUsize::new(0);

    if gate.for_user(Some(&stranger)).authorize("update", &article).await.is_ok() {
        mutations.fetch_add(1, Ordering::SeqCst);
    }
    assert_eq!(mutations.load(Ordering::SeqCst), 0);

    gate.for_user(Some(&owner)).authorize("update", &article).await?;
    mutations.fetch_add(1, Ordering::SeqCst);
    assert_eq!(mutations.load(Ordering::SeqCst), 1);
    Ok(())
}

#[tokio::test]
async fn guests_are_denied_unless_an_ability_explicitly_accepts_them() -> Result<(), Box<dyn Error>> {
    let mut builder = Gate::<User>::builder();
    builder.define("private", |_user: User, (): ()| async { true });
    builder.define_for_guests("published", |user: Option<User>, article: Article| async move {
        user.is_some() || article.published
    });
    let gate = builder.build();
    let published = Article { owner_id: 1, published: true };

    assert!(gate.for_user(None).denies("private", &()).await?);
    assert!(gate.for_user(None).allows("published", &published).await?);
    Ok(())
}

#[tokio::test]
async fn before_and_after_hooks_follow_laravel_ordering() -> Result<(), Box<dyn Error>> {
    let after_calls = Arc::new(AtomicUsize::new(0));
    let observed_after = Arc::clone(&after_calls);
    let mut builder = Gate::<User>::builder();
    builder.define("ordinary", |_user: User, (): ()| async { false });
    builder.before(|user: Option<User>, ability| async move {
        (user.is_some_and(|user| user.administrator) && ability == "ordinary").then(AuthorizationResponse::allow)
    });
    builder.after(move |_user, _ability, _result| {
        let observed_after = Arc::clone(&observed_after);
        async move {
            observed_after.fetch_add(1, Ordering::SeqCst);
            Some(AuthorizationResponse::allow())
        }
    });
    let gate = builder.build();
    let user = User { id: 1, administrator: false };
    let administrator = User { id: 2, administrator: true };

    assert!(gate.for_user(Some(&user)).denies("ordinary", &()).await?);
    assert!(gate.for_user(Some(&administrator)).allows("ordinary", &()).await?);
    assert!(gate.for_user(Some(&user)).allows("missing", &()).await?);
    assert_eq!(after_calls.load(Ordering::SeqCst), 3);
    Ok(())
}

#[tokio::test]
async fn checks_combine_abilities_and_unknown_abilities_use_the_default_denial() -> Result<(), Box<dyn Error>> {
    let mut builder = Gate::<User>::builder();
    builder.define("read", |_user: User, (): ()| async { true });
    builder.define("write", |_user: User, (): ()| async { false });
    builder.default_denial(AuthorizationResponse::deny_as_not_found().with_message("Hidden resource."));
    let gate = builder.build();
    let user = User { id: 1, administrator: false };
    let gate = gate.for_user(Some(&user));

    assert!(!gate.check(["read", "write"], &()).await?);
    assert!(gate.any(["read", "write"], &()).await?);
    assert!(!gate.none(["read", "write"], &()).await?);
    let missing = gate.inspect("missing", &()).await?;
    assert_eq!(missing.status(), Some(StatusCode::NOT_FOUND));
    assert_eq!(missing.message(), Some("Hidden resource."));
    Ok(())
}

#[tokio::test]
async fn mismatched_context_is_a_configuration_error() {
    let mut builder = Gate::<User>::builder();
    builder.define("update", |_user: User, _article: Article| async { true });
    let gate = builder.build();
    let user = User { id: 1, administrator: false };

    let error = gate
        .for_user(Some(&user))
        .inspect("update", &())
        .await
        .expect_err("the wrong context type must not become an ordinary denial");
    assert!(matches!(error, GateError::ContextMismatch { .. }));
}

#[test]
fn inline_authorization_bypasses_registered_hooks() {
    let hook_calls = Arc::new(AtomicUsize::new(0));
    let observed = Arc::clone(&hook_calls);
    let mut builder = Gate::<User>::builder();
    builder.before(move |_user, _ability| {
        let observed = Arc::clone(&observed);
        async move {
            observed.fetch_add(1, Ordering::SeqCst);
            Some(true)
        }
    });
    let gate = builder.build();
    let user = User { id: 1, administrator: false };
    let gate = gate.for_user(Some(&user));

    assert!(gate.allow_if_with_user(|user| user.id == 1).is_ok());
    assert!(gate.deny_if_with_user(|user| user.id == 2).is_ok());
    assert!(gate.deny_if_with_user(|user| user.id == 1).is_err());
    assert!(gate.allow_if(true).is_ok());
    assert_eq!(hook_calls.load(Ordering::SeqCst), 0);
}

#[test]
fn inline_authorization_can_explicitly_handle_guests() {
    let gate = Gate::<User>::builder().build();
    let guest = gate.for_user(None);

    assert!(guest.allow_if(true).is_ok());
    assert!(guest.allow_if_with_user(|_| true).is_err());
    assert!(guest.allow_if_for_guests(|user| user.is_none()).is_ok());
    assert!(guest.deny_if(false).is_ok());
}

#[test]
fn authorization_errors_render_the_selected_status_and_message() {
    let response =
        AuthorizationError::Denied(AuthorizationResponse::deny_as_not_found().with_message("Article not found."))
            .into_response();

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[derive(Clone, Debug)]
struct Backend;

#[derive(Clone, Debug)]
struct Credentials;

impl AuthUser for User {
    type Id = i64;

    fn id(&self) -> Self::Id {
        self.id
    }

    fn session_auth_hash(&self) -> &[u8] {
        b"authorization-test"
    }
}

impl AuthnBackend for Backend {
    type User = User;
    type Credentials = Credentials;
    type Error = Infallible;

    async fn authenticate(&self, _credentials: Credentials) -> Result<Option<User>, Infallible> {
        Ok(Some(test_user()))
    }

    async fn get_user(&self, user_id: &i64) -> Result<Option<User>, Infallible> {
        Ok((*user_id == 1).then(test_user))
    }
}

fn test_user() -> User {
    User { id: 1, administrator: false }
}

#[tokio::test]
async fn route_authorization_uses_the_authenticated_session_and_blocks_denied_actions() -> Result<(), Box<dyn Error>> {
    let executions = Arc::new(AtomicUsize::new(0));
    let observed = Arc::clone(&executions);
    let mut builder = Gate::<User>::builder();
    builder.define("mutate", |user: User, (): ()| async move { user.id == 1 });
    let gate = builder.build();
    let protected = Router::new()
        .route(
            "/protected",
            post(move || {
                let observed = Arc::clone(&observed);
                async move {
                    observed.fetch_add(1, Ordering::SeqCst);
                    StatusCode::NO_CONTENT
                }
            }),
        )
        .route_layer(AuthorizeLayer::<Backend>::new(gate, "mutate"));
    let sessions = SessionManagerLayer::new(MemoryStore::default()).with_secure(false);
    let authentication = AuthManagerLayerBuilder::new(Backend, sessions).build();
    let app = Router::new().route("/login", get(login)).merge(protected).layer(authentication);

    let denied = app.clone().oneshot(Request::post("/protected").body(Body::empty())?).await?;
    assert_eq!(denied.status(), StatusCode::FORBIDDEN);
    assert_eq!(executions.load(Ordering::SeqCst), 0);

    let login = app.clone().oneshot(Request::get("/login").body(Body::empty())?).await?;
    let cookie = login
        .headers()
        .get(header::SET_COOKIE)
        .ok_or("login did not set a session cookie")?
        .to_str()?
        .split(';')
        .next()
        .ok_or("session cookie was empty")?
        .to_owned();
    let allowed = app.oneshot(Request::post("/protected").header(header::COOKIE, cookie).body(Body::empty())?).await?;
    assert_eq!(allowed.status(), StatusCode::NO_CONTENT);
    assert_eq!(executions.load(Ordering::SeqCst), 1);
    Ok(())
}

#[tokio::test]
async fn route_authorization_reports_a_missing_authentication_layer() -> Result<(), Box<dyn Error>> {
    let mut builder = Gate::<User>::builder();
    builder.define("view", |_user: User, (): ()| async { true });
    let app = Router::new()
        .route("/protected", get(|| async { StatusCode::OK }))
        .route_layer(AuthorizeLayer::<Backend>::new(builder.build(), "view"));

    let response = app.oneshot(Request::get("/protected").body(Body::empty())?).await?;

    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
    Ok(())
}

#[tokio::test]
async fn route_authorization_preserves_fixed_context_denials() -> Result<(), Box<dyn Error>> {
    let mut builder = Gate::<User>::builder();
    builder.define_for_guests("view", |_user: Option<User>, article: Article| async move {
        if article.published {
            AuthorizationResponse::allow()
        } else {
            AuthorizationResponse::deny_as_not_found().with_message("Article not found.")
        }
    });
    let article = Article { owner_id: 1, published: false };
    let protected = Router::new().route("/articles/1", get(|| async { StatusCode::OK })).route_layer(AuthorizeLayer::<
        Backend,
        Article,
    >::with_context(
        builder.build(),
        "view",
        article,
    ));
    let sessions = SessionManagerLayer::new(MemoryStore::default()).with_secure(false);
    let app = protected.layer(AuthManagerLayerBuilder::new(Backend, sessions).build());

    let response = app.oneshot(Request::get("/articles/1").body(Body::empty())?).await?;
    let status = response.status();
    let body = to_bytes(response.into_body(), usize::MAX).await?;

    assert_eq!(status, StatusCode::NOT_FOUND);
    assert_eq!(&body[..], b"Article not found.");
    Ok(())
}

async fn login(mut auth: AuthSession<Backend>) -> StatusCode {
    match auth.login(&test_user()).await {
        Ok(()) => StatusCode::NO_CONTENT,
        Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
    }
}

#[tokio::test]
async fn denied_error_body_uses_laravels_default_message() -> Result<(), Box<dyn Error>> {
    let response = AuthorizationError::Denied(AuthorizationResponse::deny()).into_response();
    let body = to_bytes(response.into_body(), usize::MAX).await?;

    assert_eq!(&body[..], b"This action is unauthorized.");
    Ok(())
}

#[tokio::test]
async fn denied_error_body_preserves_a_policy_message() -> Result<(), Box<dyn Error>> {
    let response =
        AuthorizationError::Denied(AuthorizationResponse::deny().with_message("You do not own this article."))
            .into_response();
    let body = to_bytes(response.into_body(), usize::MAX).await?;

    assert_eq!(&body[..], b"You do not own this article.");
    Ok(())
}