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
//! Authorizers for working with `tower_http` and other constructs in the
//! ecosystem, including `axum`.
//!
//! See the `examples` folder in the repository for a working example using
//! an `tonic` web server. For a more ergonomic experience in `axum`,
//! see the [`aliri_axum`](https://docs.rs/aliri_axum) crate.
//!
//! ```
//! # use axum::extract::Path;
//! use axum::handler::Handler;
//! # use axum::routing::{get, post};
//! # use aliri::{jwa, jwk, jwt, Jwk, Jwks};
//! # use aliri_base64::Base64UrlRef;
//! # use aliri_clock::UnixTime;
//! use aliri_oauth2::{scope, policy, ScopePolicy};
//! use aliri_tower::Oauth2Authorizer;
//!
//! # #[derive(Clone, Debug, serde::Deserialize)]
//! pub struct CustomClaims {
//! // …
//! # iss: aliri::jwt::Issuer,
//! # aud: aliri::jwt::Audiences,
//! # sub: aliri::jwt::Subject,
//! # scope: aliri_oauth2::scope::Scope,
//! }
//!
//! impl jwt::CoreClaims for CustomClaims {
//! // …
//! # fn nbf(&self) -> Option<UnixTime> { None }
//! # fn exp(&self) -> Option<UnixTime> { None }
//! # fn aud(&self) -> &aliri::jwt::Audiences { &self.aud }
//! # fn iss(&self) -> Option<&aliri::jwt::IssuerRef> { Some(&self.iss) }
//! # fn sub(&self) -> Option<&aliri::jwt::SubjectRef> { Some(&self.sub) }
//! }
//!
//! # impl aliri_oauth2::HasScope for CustomClaims {
//! # fn scope(&self) -> &aliri_oauth2::scope::Scope { &self.scope }
//! # }
//! #
//! # fn construct_authority() -> aliri_oauth2::Authority {
//! # // This authority might otherwise come from a well-known JWKS endpoint
//! # let secret = Base64UrlRef::from_slice(b"test").to_owned();
//! # let key = Jwk::from(jwa::Hmac::new(secret))
//! # .with_algorithm(jwa::Algorithm::HS256)
//! # .with_key_id(jwk::KeyId::from_static("test key"));
//! #
//! # let mut jwks = Jwks::default();
//! # jwks.add_key(key);
//! #
//! # let validator = jwt::CoreValidator::default()
//! # .ignore_expiration() // Only for demonstration purposes
//! # .add_approved_algorithm(jwa::Algorithm::HS256)
//! # .add_allowed_audience(jwt::Audience::from_static("my_api"))
//! # .require_issuer(jwt::Issuer::from_static("authority"));
//! #
//! # aliri_oauth2::Authority::new(jwks, validator)
//! # }
//! #
//! let authority = construct_authority();
//! let authorizer = Oauth2Authorizer::new()
//! .with_claims::<CustomClaims>()
//! .with_terse_error_handler();
//!
//! let app = axum::Router::new()
//! .route(
//! "/users",
//! post(handle_post
//! .layer(authorizer.scope_layer(policy![scope!["post_user"]]))),
//! )
//! .route(
//! "/users/:id",
//! get(handle_get
//! .layer(authorizer.scope_layer(ScopePolicy::allow_one_from_static("get_user")))),
//! )
//! .layer(authorizer.jwt_layer(authority));
//! #
//! # async fn handle_post() {}
//! #
//! # async fn handle_get(Path(id): Path<u64>) {}
//! #
//! # async {
//! # axum::serve(tokio::net::TcpListener::bind("").await.unwrap(), app).await.unwrap();
//! # };
//! ```
use ;
pub use crate::;
/// Terse responders for authentication and authorization failures
///
/// This handler will generate a default error response containing the
/// relevant status code and `www-authenticate` header with an empty body.
///
/// This type _does not_ provide `error_description` data to avoid leaking
/// internal error information, but does provide `scope` information in
/// when an otherwise valid token lacks sufficient permissions.
/// Verbose responders for authentication and authorization failures
///
/// This handler will generate a default error response containing the
/// relevant status code and `www-authenticate` header with an empty body.
///
/// This type provides `error_description` data in the `www-authenticate`
/// header.
/// ```
/// use aliri_tower::VerboseErrorHandler;
/// fn is_send_sync<T: Send + Sync>(_: T) {}
/// fn verbose_error_handler_is_send_sync<B>(v: VerboseErrorHandler<B>) {
/// is_send_sync(v)
/// }
/// ```
/// ```
/// use aliri_tower::TerseErrorHandler;
/// fn is_send_sync<T: Send + Sync>(_: T) {}
/// fn terse_error_handler_is_send_sync<B>(v: TerseErrorHandler<B>) {
/// is_send_sync(v)
/// }
/// ```