backbone-auth 3.0.2

Backbone Framework Auth - Authentication and authorization system
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Resource-level permission guards — bind auth context to entity-level access control.
//!
//! `ResourcePolicy<E>` is the auth-layer counterpart to `DomainPolicy<E>` in
//! `backbone-core`.  Where `DomainPolicy` enforces business invariants (is the
//! entity in a valid state for this operation?), `ResourcePolicy` enforces
//! **identity-based** rules (does *this caller* have permission to touch *this record*?).
//!
//! # Typical wiring
//!
//! ```text
//! HTTP handler
//!   → AuthMiddleware extracts AuthContext
//!   → PermissionGuard<E>::check(action, entity, auth_ctx)
//!       → ResourcePolicy<E>::can(action, entity, auth_ctx)  ← module implements this
//!   → If Err → 403 Forbidden
//!   → Else   → service.execute()
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! pub struct OrderResourcePolicy;
//!
//! #[async_trait]
//! impl ResourcePolicy<Order> for OrderResourcePolicy {
//!     async fn can(&self, action: ResourceAction, entity: &Order, ctx: &AuthContext) -> bool {
//!         match action {
//!             ResourceAction::Read => ctx.user_id == entity.customer_id || ctx.has_role("admin"),
//!             ResourceAction::Update | ResourceAction::Delete => ctx.user_id == entity.customer_id,
//!             _ => ctx.has_role("admin"),
//!         }
//!     }
//! }
//! ```

use async_trait::async_trait;
use std::marker::PhantomData;
use std::sync::Arc;

use crate::middleware::AuthContext;

// ─── Resource actions ─────────────────────────────────────────────────────────

/// Standard CRUD operations that a resource policy can gate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceAction {
    /// Read / fetch a single entity.
    Read,
    /// List / query multiple entities.
    List,
    /// Create a new entity.
    Create,
    /// Fully update an entity.
    Update,
    /// Partially update an entity.
    Patch,
    /// Soft-delete an entity.
    Delete,
    /// Restore a soft-deleted entity.
    Restore,
    /// Permanently delete an entity.
    HardDelete,
    /// Custom action for domain-specific operations.
    Custom(&'static str),
}

impl ResourceAction {
    pub fn name(&self) -> &'static str {
        match self {
            ResourceAction::Read => "read",
            ResourceAction::List => "list",
            ResourceAction::Create => "create",
            ResourceAction::Update => "update",
            ResourceAction::Patch => "patch",
            ResourceAction::Delete => "delete",
            ResourceAction::Restore => "restore",
            ResourceAction::HardDelete => "hard_delete",
            ResourceAction::Custom(name) => name,
        }
    }
}

// ─── Policy trait ─────────────────────────────────────────────────────────────

/// Determines whether the caller described by `auth_ctx` may perform `action`
/// on `entity`.
///
/// Return `true` to permit, `false` to deny.  Use `PermissionGuard` to convert
/// this into a `Result<(), AccessDenied>` suitable for HTTP handlers.
///
/// ## Static permission string methods
///
/// The `resource_type()` and `*_permission()` associated functions return the
/// permission string identifiers used by RBAC systems.  They have a
/// `where Self: Sized` bound so they can only be called in generic contexts
/// (not through `dyn ResourcePolicy`), which is intentional — the strings are
/// known at compile time and used by code generators and RBAC setup code.
///
/// ```rust,ignore
/// // Generated usage:
/// let required = OrderResourcePolicy::update_permission(); // "orders:update"
/// rbac.require_permission(ctx, required)?;
/// ```
#[async_trait]
pub trait ResourcePolicy<E: Send + Sync + 'static>: Send + Sync {
    // ── Static permission strings ─────────────────────────────────────────

    /// The resource type name used in permission strings.
    ///
    /// Default: `"resource"`. Override per entity, e.g. `"orders"`.
    fn resource_type() -> &'static str
    where
        Self: Sized,
    {
        "resource"
    }

    /// Permission string required to create this resource.
    fn create_permission() -> &'static str
    where
        Self: Sized,
    {
        "create"
    }

    /// Permission string required to read/fetch this resource.
    fn read_permission() -> &'static str
    where
        Self: Sized,
    {
        "read"
    }

    /// Permission string required to list this resource.
    fn list_permission() -> &'static str
    where
        Self: Sized,
    {
        "list"
    }

    /// Permission string required to fully update this resource.
    fn update_permission() -> &'static str
    where
        Self: Sized,
    {
        "update"
    }

    /// Permission string required to partially patch this resource.
    fn patch_permission() -> &'static str
    where
        Self: Sized,
    {
        "patch"
    }

    /// Permission string required to delete this resource.
    fn delete_permission() -> &'static str
    where
        Self: Sized,
    {
        "delete"
    }

    /// Permission string required to restore a soft-deleted resource.
    fn restore_permission() -> &'static str
    where
        Self: Sized,
    {
        "restore"
    }

    // ── Instance methods ──────────────────────────────────────────────────

    async fn can(
        &self,
        action: ResourceAction,
        entity: &E,
        ctx: &AuthContext,
    ) -> bool;

    /// Optional: deny specific actions for all callers (e.g. hard-delete disabled).
    fn explicitly_disabled_actions(&self) -> Vec<ResourceAction> {
        vec![]
    }
}

// ─── Access denied ────────────────────────────────────────────────────────────

/// Returned when a `PermissionGuard` denies an operation.
#[derive(Debug, thiserror::Error)]
#[error("access denied: caller '{caller}' may not perform '{action}' on this resource")]
pub struct AccessDenied {
    pub caller: String,
    pub action: String,
}

impl AccessDenied {
    pub fn new(caller: impl Into<String>, action: &ResourceAction) -> Self {
        Self {
            caller: caller.into(),
            action: action.name().into(),
        }
    }
}

impl Default for AccessDenied {
    fn default() -> Self {
        Self {
            caller: "anonymous".into(),
            action: "unknown".into(),
        }
    }
}

// ─── Permission guard ─────────────────────────────────────────────────────────

/// Wraps a `ResourcePolicy<E>` and enforces it, returning typed errors.
///
/// Inject one `Arc<PermissionGuard<E>>` per handler family.
pub struct PermissionGuard<E> {
    policy: Arc<dyn ResourcePolicy<E>>,
}

impl<E: Send + Sync + 'static> PermissionGuard<E> {
    pub fn new(policy: Arc<dyn ResourcePolicy<E>>) -> Self {
        Self { policy }
    }

    /// Check whether `ctx` may perform `action` on `entity`.
    ///
    /// Returns `Ok(())` on permit, `Err(AccessDenied)` on deny.
    pub async fn check(
        &self,
        action: ResourceAction,
        entity: &E,
        ctx: &AuthContext,
    ) -> Result<(), AccessDenied> {
        if self.policy.explicitly_disabled_actions().contains(&action) {
            return Err(AccessDenied::new(&ctx.user_id, &action));
        }

        if self.policy.can(action, entity, ctx).await {
            Ok(())
        } else {
            Err(AccessDenied::new(&ctx.user_id, &action))
        }
    }
}

// ─── AuthContextProvider ─────────────────────────────────────────────────────

/// Extracts or provides the current caller's `AuthContext`.
///
/// Implement this in your HTTP middleware or request-scope container so that
/// `ServicePermissionGuard` can retrieve the auth context without being coupled
/// to Axum or any other framework.
#[async_trait]
pub trait AuthContextProvider: Send + Sync {
    async fn current(&self) -> Option<AuthContext>;
}

// ─── ServicePermissionGuard ───────────────────────────────────────────────────

/// Generic permission guard that wraps both a service and an auth context provider.
///
/// Generated modules emit a type alias:
///
/// ```rust,ignore
/// // Generated (Phase 1):
/// impl ResourcePolicy<Order> for OrderPolicy {
///     fn resource_type() -> &'static str { "orders" }
///     fn create_permission() -> &'static str { "orders:create" }
///     // ...
///     async fn can(&self, action, entity, ctx) -> bool { ... }
/// }
///
/// pub type OrderGuard = ServicePermissionGuard<Order, OrderService, OrderPolicy>;
/// ```
///
/// `E` — entity type
/// `S` — underlying service
/// `P` — resource policy implementation
pub struct ServicePermissionGuard<E, S, P>
where
    E: Send + Sync + 'static,
    P: ResourcePolicy<E>,
{
    service: Arc<S>,
    policy: Arc<P>,
    _phantom: std::marker::PhantomData<E>,
}

impl<E, S, P> ServicePermissionGuard<E, S, P>
where
    E: Send + Sync + 'static,
    P: ResourcePolicy<E>,
{
    pub fn new(service: Arc<S>, policy: Arc<P>) -> Self {
        Self {
            service,
            policy,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Access the underlying service.
    pub fn service(&self) -> &Arc<S> {
        &self.service
    }

    /// Access the underlying policy.
    pub fn policy(&self) -> &Arc<P> {
        &self.policy
    }

    /// Check whether `ctx` may perform `action` on `entity`, returning
    /// `Ok(())` on permit or `Err(AccessDenied)` on deny.
    pub async fn check(
        &self,
        action: ResourceAction,
        entity: &E,
        ctx: &AuthContext,
    ) -> Result<(), AccessDenied> {
        if self.policy.explicitly_disabled_actions().contains(&action) {
            return Err(AccessDenied::new(&ctx.user_id, &action));
        }
        if self.policy.can(action, entity, ctx).await {
            Ok(())
        } else {
            Err(AccessDenied::new(&ctx.user_id, &action))
        }
    }
}

// ─── Built-in policies ───────────────────────────────────────────────────────

/// Permits every action for every caller.
/// Use as the generated default — replace in custom decorators.
pub struct PermitAllResourcePolicy<E> {
    _phantom: PhantomData<E>,
}

impl<E> PermitAllResourcePolicy<E> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

impl<E> Default for PermitAllResourcePolicy<E> {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> ResourcePolicy<E> for PermitAllResourcePolicy<E> {
    async fn can(&self, _action: ResourceAction, _entity: &E, _ctx: &AuthContext) -> bool {
        true
    }
}

/// Denies every action for every caller.
/// Use for deprecated or not-yet-exposed resources.
pub struct DenyAllResourcePolicy<E> {
    _phantom: PhantomData<E>,
}

impl<E> DenyAllResourcePolicy<E> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> ResourcePolicy<E> for DenyAllResourcePolicy<E> {
    async fn can(&self, _action: ResourceAction, _entity: &E, _ctx: &AuthContext) -> bool {
        false
    }
}

/// Requires the caller to have one of the listed roles to perform any action.
pub struct RoleRequiredPolicy<E> {
    required_roles: Vec<String>,
    _phantom: PhantomData<E>,
}

impl<E> RoleRequiredPolicy<E> {
    pub fn new(required_roles: Vec<impl Into<String>>) -> Self {
        Self {
            required_roles: required_roles.into_iter().map(|r| r.into()).collect(),
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<E: Send + Sync + 'static> ResourcePolicy<E> for RoleRequiredPolicy<E> {
    async fn can(&self, _action: ResourceAction, _entity: &E, ctx: &AuthContext) -> bool {
        self.required_roles
            .iter()
            .any(|role| ctx.roles.contains(role))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug)]
    struct Document {
        owner_id: String,
    }

    struct OwnerPolicy;

    #[async_trait]
    impl ResourcePolicy<Document> for OwnerPolicy {
        async fn can(
            &self,
            _action: ResourceAction,
            entity: &Document,
            ctx: &AuthContext,
        ) -> bool {
            ctx.user_id == entity.owner_id
        }
    }

    fn auth_ctx(user_id: &str) -> AuthContext {
        AuthContext::new(user_id.to_string())
    }

    #[tokio::test]
    async fn owner_permitted_stranger_denied() {
        let guard = PermissionGuard::new(Arc::new(OwnerPolicy));
        let doc = Document {
            owner_id: "alice".into(),
        };

        assert!(guard
            .check(ResourceAction::Update, &doc, &auth_ctx("alice"))
            .await
            .is_ok());
        assert!(guard
            .check(ResourceAction::Update, &doc, &auth_ctx("bob"))
            .await
            .is_err());
    }

    #[tokio::test]
    async fn permit_all_always_ok() {
        let guard: PermissionGuard<Document> =
            PermissionGuard::new(Arc::new(PermitAllResourcePolicy::new()));
        let doc = Document {
            owner_id: "x".into(),
        };
        assert!(guard
            .check(ResourceAction::Delete, &doc, &auth_ctx("anyone"))
            .await
            .is_ok());
    }
}