atproto_oauth/axum/
state.rs

1//! Axum request extractors for AT Protocol OAuth services.
2//!
3//! Provides extractors that automatically inject OAuth request storage
4//! into Axum request handlers from application state.
5
6use axum::extract::{FromRef, FromRequestParts};
7use http::request::Parts;
8use std::{convert::Infallible, sync::Arc};
9
10use crate::storage::OAuthRequestStorage;
11
12/// Axum request extractor for OAuth request storage.
13///
14/// Automatically extracts an OAuth request storage implementation from the application state.
15#[derive(Clone)]
16pub struct OAuthRequestStorageExtractor(pub Arc<dyn OAuthRequestStorage + Send + Sync>);
17
18impl<S> FromRequestParts<S> for OAuthRequestStorageExtractor
19where
20    Arc<dyn OAuthRequestStorage + Send + Sync>: FromRef<S>,
21    S: Send + Sync,
22{
23    type Rejection = Infallible;
24
25    async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
26        let storage = Arc::<dyn OAuthRequestStorage + Send + Sync>::from_ref(state);
27        Ok(OAuthRequestStorageExtractor(storage))
28    }
29}