poem_openapi/auth/
mod.rs

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Some certificate types for security scheme.

mod api_key;
mod basic;
mod bearer;

use poem::{Request, Result};

pub use self::{api_key::ApiKey, basic::Basic, bearer::Bearer};
use crate::{base::UrlQuery, error::AuthorizationError, registry::MetaParamIn};

/// Represents a basic authorization extractor.
pub trait BasicAuthorization: Sized {
    /// Extract from the HTTP request.
    fn from_request(req: &Request) -> Result<Self>;
}

/// Represents a bearer authorization extractor.
pub trait BearerAuthorization: Sized {
    /// Extract from the HTTP request.
    fn from_request(req: &Request) -> Result<Self>;
}

/// Represents an api key authorization extractor.
pub trait ApiKeyAuthorization: Sized {
    /// Extract from the HTTP request.
    fn from_request(
        req: &Request,
        query: &UrlQuery,
        name: &str,
        in_type: MetaParamIn,
    ) -> Result<Self>;
}

/// Facilitates the conversion of `Option` into `Results`, for `SecuritySchema`
/// checker.
#[doc(hidden)]
pub enum CheckerReturn<T> {
    Result(Result<T>),
    Option(Option<T>),
}

impl<T> CheckerReturn<T> {
    pub fn into_result(self) -> Result<T> {
        match self {
            Self::Result(result) => result,
            Self::Option(option) => option.ok_or(AuthorizationError.into()),
        }
    }
}

impl<T> From<poem::Result<T>> for CheckerReturn<T> {
    #[inline]
    fn from(result: Result<T>) -> Self {
        Self::Result(result)
    }
}

impl<T> From<Option<T>> for CheckerReturn<T> {
    #[inline]
    fn from(option: Option<T>) -> Self {
        Self::Option(option)
    }
}