poem_openapi/auth/
bearer.rs

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