poem_openapi/auth/
basic.rs

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