11//! Either has No Authorization, or RBAC Token.
22
33use 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
610use 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.
1114pub ( 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+
1851impl From < NoneOrRBAC > for Option < IdUri > {
1952 fn from ( value : NoneOrRBAC ) -> Self {
2053 match value {
0 commit comments