Skip to content

Commit 01ef2ca

Browse files
Mr-Leshiyminikin
andauthored
remove unused NoneOrApiKey, update processing of NoneOrRBAC auth (#2294)
Co-authored-by: Oleksandr Prokhorenko <[email protected]>
1 parent da0b08c commit 01ef2ca

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

catalyst-gateway/bin/src/service/common/auth/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
33
pub(crate) mod api_key;
44
pub(crate) mod none;
5-
pub(crate) mod none_or_api_key;
65
pub(crate) mod none_or_rbac;
76
pub(crate) mod rbac;

catalyst-gateway/bin/src/service/common/auth/none.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Means the API Endpoint does not need to use any Auth.
44
55
/// Endpoint can be used without any authorization.
6-
pub(crate) struct NoAuthorization();
6+
pub(crate) struct NoAuthorization;
77

88
impl<'a> poem_openapi::ApiExtractor<'a> for NoAuthorization {
99
type ParamRawType = ();
@@ -36,6 +36,6 @@ impl<'a> poem_openapi::ApiExtractor<'a> for NoAuthorization {
3636
_req: &'a poem::Request, _body: &mut poem::RequestBody,
3737
_param_opts: poem_openapi::ExtractParamOptions<Self::ParamType>,
3838
) -> poem::Result<Self> {
39-
Ok(Self())
39+
Ok(Self)
4040
}
4141
}

catalyst-gateway/bin/src/service/common/auth/none_or_api_key.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

catalyst-gateway/bin/src/service/common/auth/none_or_rbac.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Either has No Authorization, or RBAC Token.
22
33
use catalyst_types::id_uri::IdUri;
4-
use poem_openapi::SecurityScheme;
4+
use poem::{
5+
web::headers::{authorization::Bearer, Authorization, HeaderMapExt},
6+
Request, RequestBody,
7+
};
8+
use poem_openapi::{registry::Registry, ApiExtractor, ApiExtractorType, ExtractParamOptions};
59

610
use super::{none::NoAuthorization, rbac::scheme::CatalystRBACSecurityScheme};
711

8-
#[derive(SecurityScheme)]
912
#[allow(dead_code, clippy::upper_case_acronyms, clippy::large_enum_variant)]
1013
/// Endpoint allows Authorization with or without RBAC Token.
1114
pub(crate) enum NoneOrRBAC {
@@ -15,6 +18,36 @@ pub(crate) enum NoneOrRBAC {
1518
None(NoAuthorization),
1619
}
1720

21+
impl<'a> ApiExtractor<'a> for NoneOrRBAC {
22+
type ParamRawType = ();
23+
type ParamType = ();
24+
25+
const TYPES: &'static [ApiExtractorType] = &[ApiExtractorType::SecurityScheme];
26+
27+
fn register(registry: &mut Registry) {
28+
CatalystRBACSecurityScheme::register(registry);
29+
NoAuthorization::register(registry);
30+
}
31+
32+
fn security_schemes() -> Vec<&'static str> {
33+
let mut schemas = Vec::new();
34+
schemas.extend(CatalystRBACSecurityScheme::security_schemes());
35+
schemas.extend(NoAuthorization::security_schemes());
36+
schemas
37+
}
38+
39+
async fn from_request(
40+
req: &'a Request, body: &mut RequestBody, param_opts: ExtractParamOptions<Self::ParamType>,
41+
) -> poem::Result<Self> {
42+
if req.headers().typed_get::<Authorization<Bearer>>().is_some() {
43+
let auth = CatalystRBACSecurityScheme::from_request(req, body, param_opts).await?;
44+
Ok(NoneOrRBAC::RBAC(auth))
45+
} else {
46+
Ok(NoneOrRBAC::None(NoAuthorization))
47+
}
48+
}
49+
}
50+
1851
impl From<NoneOrRBAC> for Option<IdUri> {
1952
fn from(value: NoneOrRBAC) -> Self {
2053
match value {

0 commit comments

Comments
 (0)