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
//! Gate implementation for protecting axum routes with JWT authentication.
//!
//! The `Gate` provides a high-level API for adding authentication and authorization
//! to your axum routes using JWT cookies or bearer tokens. It supports role-based
//! access control, group-based access control, and fine-grained permission systems.
//!
//! # Basic Usage
//!
//! ```rust
//! use axum::{routing::get, Router};
//! use axum_gate::prelude::*;
//! use std::sync::Arc;
//!
//! # async fn protected_handler() -> &'static str { "Protected!" }
//! let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! let cookie_template = CookieTemplate::recommended()
//! .name("auth-token")
//! .persistent(cookie::time::Duration::hours(24));
//!
//! let app = Router::<()>::new()
//! .route("/admin", get(protected_handler))
//! .layer(
//! Gate::cookie("my-app", jwt_codec)
//! .with_policy(AccessPolicy::<Role, Group>::require_role(Role::Admin))
//! .with_cookie_template(cookie_template)
//! );
//! ```
//!
//! # Access Control Examples
//!
//! ## Role-Based Access
//! ```rust
//! # use axum_gate::prelude::*;
//! # use std::sync::Arc;
//! # let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! // Allow only Admin role
//! let gate = Gate::cookie("my-app", Arc::clone(&jwt_codec))
//! .with_policy(AccessPolicy::<Role, Group>::require_role(Role::Admin));
//!
//! // Allow Admin or Moderator roles
//! let gate = Gate::cookie("my-app", Arc::clone(&jwt_codec))
//! .with_policy(
//! AccessPolicy::<Role, Group>::require_role(Role::Admin)
//! .or_require_role(Role::Moderator)
//! );
//! ```
//!
//! ## Hierarchical Access
//! ```rust
//! # use axum_gate::prelude::*;
//! # use std::sync::Arc;
//! # let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! // Allow User role and all supervisor roles (Reporter, Moderator, Admin)
//! let gate = Gate::cookie("my-app", jwt_codec)
//! .with_policy(AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::User));
//! ```
//!
//! ## Permission-Based Access
//! ```rust
//! # use axum_gate::prelude::*;
//! # use std::sync::Arc;
//! # let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! let gate = Gate::cookie("my-app", jwt_codec)
//! .with_policy(
//! AccessPolicy::<Role, Group>::require_permission(PermissionId::from("read:api"))
//! );
//! ```
//! ## Bearer Gate (JWT)
//! Strict bearer (JWT) example:
//! ```rust
//! # use axum::{routing::get, Router};
//! # use axum_gate::prelude::*;
//! # use std::sync::Arc;
//! # async fn handler() {}
//! let jwt = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! let app = Router::<()>::new()
//! .route("/admin", get(handler))
//! .layer(
//! Gate::bearer("my-app", Arc::clone(&jwt))
//! .with_policy(AccessPolicy::<Role, Group>::require_role(Role::Admin))
//! );
//! ```
//!
//! Optional user context (never blocks; handlers must enforce access):
//! ```rust
//! # use axum_gate::prelude::*;
//! # use std::sync::Arc;
//! let jwt = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! let gate = Gate::bearer::<JsonWebToken<JwtClaims<Account<Role, Group>>>, Role, Group>("my-app", jwt).allow_anonymous_with_optional_user();
//! // Inserts Option<Account<Role, Group>> and Option<RegisteredClaims> into request extensions.
//! ```
//!
use BearerGate;
use CookieGate;
use OAuth2Gate;
use crateAccount;
use crateAccessHierarchy;
use crateCodec;
use crateJwtClaims;
use Arc;
/// Main entry point for creating authentication gates.
///
/// Gates protect your axum routes from unauthorized access using JWT tokens.
/// All requests are denied by default unless explicitly granted access through
/// an access policy. Choose between cookie-based gates for web applications
/// and bearer token gates for APIs and SPAs.
;