poem_openapi/auth/
mod.rs

1//! Some certificate types for security scheme.
2
3mod api_key;
4mod basic;
5mod bearer;
6
7use poem::{Request, Result};
8
9pub use self::{api_key::ApiKey, basic::Basic, bearer::Bearer};
10use crate::{base::UrlQuery, error::AuthorizationError, registry::MetaParamIn};
11
12/// Represents a basic authorization extractor.
13pub trait BasicAuthorization: Sized {
14    /// Extract from the HTTP request.
15    fn from_request(req: &Request) -> Result<Self>;
16}
17
18/// Represents a bearer authorization extractor.
19pub trait BearerAuthorization: Sized {
20    /// Extract from the HTTP request.
21    fn from_request(req: &Request) -> Result<Self>;
22}
23
24/// Represents an api key authorization extractor.
25pub trait ApiKeyAuthorization: Sized {
26    /// Extract from the HTTP request.
27    fn from_request(
28        req: &Request,
29        query: &UrlQuery,
30        name: &str,
31        in_type: MetaParamIn,
32    ) -> Result<Self>;
33}
34
35/// Facilitates the conversion of `Option` into `Results`, for `SecuritySchema`
36/// checker.
37#[doc(hidden)]
38pub enum CheckerReturn<T> {
39    Result(Result<T>),
40    Option(Option<T>),
41}
42
43impl<T> CheckerReturn<T> {
44    pub fn into_result(self) -> Result<T> {
45        match self {
46            Self::Result(result) => result,
47            Self::Option(option) => option.ok_or(AuthorizationError.into()),
48        }
49    }
50}
51
52impl<T> From<poem::Result<T>> for CheckerReturn<T> {
53    #[inline]
54    fn from(result: Result<T>) -> Self {
55        Self::Result(result)
56    }
57}
58
59impl<T> From<Option<T>> for CheckerReturn<T> {
60    #[inline]
61    fn from(option: Option<T>) -> Self {
62        Self::Option(option)
63    }
64}