cloudillo_core/
extract.rs1use async_trait::async_trait;
4use axum::extract::FromRequestParts;
5use axum::http::request::Parts;
6
7use crate::app::AppState;
8use crate::prelude::*;
9use cloudillo_types::auth_adapter;
10
11pub use cloudillo_types::extract::{IdTag, TnIdResolver};
13
14#[async_trait]
18impl TnIdResolver for AppState {
19 async fn resolve_tn_id(&self, id_tag: &str) -> Result<TnId, Error> {
20 self.auth_adapter.read_tn_id(id_tag).await.map_err(|_| Error::PermissionDenied)
21 }
22}
23
24#[derive(Debug, Clone)]
27pub struct Auth(pub auth_adapter::AuthCtx);
28
29impl<S> FromRequestParts<S> for Auth
30where
31 S: Send + Sync,
32{
33 type Rejection = Error;
34
35 async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
36 if let Some(auth) = parts.extensions.get::<Auth>().cloned() {
37 Ok(auth)
38 } else {
39 Err(Error::PermissionDenied)
40 }
41 }
42}
43
44#[derive(Debug, Clone)]
48pub struct OptionalAuth(pub Option<auth_adapter::AuthCtx>);
49
50impl<S> FromRequestParts<S> for OptionalAuth
51where
52 S: Send + Sync,
53{
54 type Rejection = Error;
55
56 async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
57 let auth = parts.extensions.get::<Auth>().cloned().map(|a| a.0);
58 Ok(OptionalAuth(auth))
59 }
60}
61
62#[derive(Clone, Debug)]
66pub struct RequestId(pub String);
67
68#[derive(Clone, Debug)]
70pub struct OptionalRequestId(pub Option<String>);
71
72impl<S> FromRequestParts<S> for OptionalRequestId
73where
74 S: Send + Sync,
75{
76 type Rejection = Error;
77
78 async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
79 let req_id = parts.extensions.get::<RequestId>().map(|r| r.0.clone());
80 Ok(OptionalRequestId(req_id))
81 }
82}
83
84