diff --git a/example/src/main.rs b/example/src/main.rs index ea16d0f..97c2c39 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -89,9 +89,13 @@ fn delete(_state : &mut State, id : u64) } #[rest_read_all(Auth)] -fn auth_read_all(auth : AuthStatus<()>) -> Success +fn auth_read_all(auth : AuthStatus<()>) -> AuthResult> { - format!("{:?}", auth).into() + let str : Success = match auth { + AuthStatus::Authenticated(data) => format!("{:?}", data).into(), + _ => return AuthErr + }; + str.into() } const ADDR : &str = "127.0.0.1:18080"; diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index a2b9077..9f5c185 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -23,7 +23,7 @@ use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN}; use openapiv3::{ APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema, - SchemaKind, SecurityRequirement, SecurityScheme, Server, StatusCode, Type + SchemaKind, SecurityScheme, Server, StatusCode, Type }; use serde::de::DeserializeOwned; use std::panic::RefUnwindSafe; @@ -149,7 +149,7 @@ impl NewHandler for OpenapiHandler const SECURITY_NAME : &'static str = "authToken"; #[cfg(feature = "auth")] -fn get_security(state : &mut State) -> (Vec, IndexMap>) +fn get_security(state : &mut State) -> IndexMap> { use crate::AuthSource; use gotham::state::FromState; @@ -159,10 +159,6 @@ fn get_security(state : &mut State) -> (Vec, IndexMap return Default::default() }; - let mut security : IndexMap> = Default::default(); - security.insert(SECURITY_NAME.to_owned(), Vec::new()); - let security = vec![security]; - let security_scheme = match source { AuthSource::Cookie(name) => SecurityScheme::APIKey { location: APIKeyLocation::Cookie, @@ -181,7 +177,7 @@ fn get_security(state : &mut State) -> (Vec, IndexMap> = Default::default(); security_schemes.insert(SECURITY_NAME.to_owned(), ReferenceOr::Item(security_scheme)); - (security, security_schemes) + security_schemes } #[cfg(not(feature = "auth"))] @@ -195,8 +191,7 @@ impl Handler for OpenapiHandler fn handle(self, mut state : State) -> Box { let mut openapi = self.0; - let (security, security_schemes) = get_security(&mut state); - openapi.security = security; + let security_schemes = get_security(&mut state); let mut components = openapi.components.unwrap_or_default(); components.security_schemes = security_schemes; openapi.components = Some(components); @@ -318,7 +313,15 @@ impl<'a> OperationParams<'a> } } -fn new_operation(default_status : hyper::StatusCode, accepted_types : Option>, schema : ReferenceOr, params : OperationParams, body_schema : Option>, supported_types : Option>) -> Operation +fn new_operation( + default_status : hyper::StatusCode, + accepted_types : Option>, + schema : ReferenceOr, + params : OperationParams, + body_schema : Option>, + supported_types : Option>, + requires_auth : bool +) -> Operation { let content = schema_to_content(accepted_types.unwrap_or_default(), schema); @@ -336,6 +339,14 @@ fn new_operation(default_status : hyper::StatusCode, accepted_types : Option() @@ -409,7 +420,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None)); + item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).read::() @@ -425,7 +436,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/search", &self.1); let mut item = (self.0).1.remove_path(&self.1); - item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None)); + item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None, Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).search::() @@ -442,7 +453,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types())); + item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).create::() @@ -459,7 +470,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types())); + item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).update_all::() @@ -477,7 +488,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types())); + item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types(), Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).update::() @@ -492,7 +503,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None)); + item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None, Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).delete_all::() @@ -508,7 +519,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None)); + item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).delete::() diff --git a/gotham_restful/src/result.rs b/gotham_restful/src/result.rs index 22c819e..f6057ae 100644 --- a/gotham_restful/src/result.rs +++ b/gotham_restful/src/result.rs @@ -93,6 +93,12 @@ pub trait ResourceResult { StatusCode::OK } + + #[cfg(feature = "openapi")] + fn requires_auth() -> bool + { + false + } } #[cfg(feature = "openapi")] @@ -326,6 +332,12 @@ impl ResourceResult for AuthResult { T::default_status() } + + #[cfg(feature = "openapi")] + fn requires_auth() -> bool + { + true + } } /**