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
//! axum integration, enabled by the `axum` feature.
use axum::Router;
use axum::extract::Request;
use axum::routing::{any, get};
use crate::authorization::Authorization;
use crate::constants::DEFAULT_BASE_PATH;
impl Authorization {
/// Returns a router serving every endpoint under
/// [`DEFAULT_BASE_PATH`]:
///
/// ```text
/// GET {base}/providers -> get_providers
/// ANY {base}/provider/{provider} -> handle_sign_in
/// ANY {base}/provider/{provider}/callback -> handle_callback
/// ```
///
/// The three handlers are also callable individually if you prefer to
/// register the routes yourself.
#[cfg_attr(docsrs, doc(cfg(feature = "axum")))]
pub fn handler(&self) -> Router {
let providers = self.clone();
let sign_in = self.clone();
let callback = self.clone();
Router::new()
.route(
&format!("{DEFAULT_BASE_PATH}/providers"),
get(move |req: Request| {
let auth = providers.clone();
async move { auth.get_providers(req).await }
}),
)
.route(
&format!("{DEFAULT_BASE_PATH}/provider/{{provider}}"),
any(move |req: Request| {
let auth = sign_in.clone();
async move { auth.handle_sign_in(req).await }
}),
)
.route(
&format!("{DEFAULT_BASE_PATH}/provider/{{provider}}/callback"),
any(move |req: Request| {
let auth = callback.clone();
async move { auth.handle_callback(req).await }
}),
)
}
}