logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use poem::{Request, Result};
use typed_headers::{AuthScheme, Authorization, HeaderMapExt};

use crate::{auth::BasicAuthorization, error::AuthorizationError};

/// Used to extract the username/password from the request.
pub struct Basic {
    /// username
    pub username: String,

    /// password
    pub password: String,
}

impl BasicAuthorization for Basic {
    fn from_request(req: &Request) -> Result<Self> {
        if let Some(auth) = req.headers().typed_get::<Authorization>().ok().flatten() {
            if auth.0.scheme() == &AuthScheme::BASIC {
                if let Some(token68) = auth.token68() {
                    if let Ok(value) = base64::decode(token68.as_str()) {
                        if let Ok(value) = String::from_utf8(value) {
                            let mut s = value.split(':');
                            if let (Some(username), Some(password), None) =
                                (s.next(), s.next(), s.next())
                            {
                                return Ok(Basic {
                                    username: username.to_string(),
                                    password: password.to_string(),
                                });
                            }
                        }
                    }
                }
            }
        }

        Err(AuthorizationError.into())
    }
}