poem_openapi/auth/
bearer.rs

1use poem::{
2    Request, Result,
3    web::headers::{Authorization, HeaderMapExt},
4};
5
6use crate::{auth::BearerAuthorization, error::AuthorizationError};
7
8/// Used to extract the token68 from the request.
9#[derive(Debug)]
10pub struct Bearer {
11    /// token
12    pub token: String,
13}
14
15impl BearerAuthorization for Bearer {
16    fn from_request(req: &Request) -> Result<Self> {
17        if let Some(auth) = req
18            .headers()
19            .typed_get::<Authorization<poem::web::headers::authorization::Bearer>>()
20        {
21            return Ok(Bearer {
22                token: auth.token().to_string(),
23            });
24        }
25
26        Err(AuthorizationError.into())
27    }
28}