From d10895076e0cb1f3d5c7fd03f09308d63eef58a9 Mon Sep 17 00:00:00 2001 From: msrd0 Date: Sat, 11 Apr 2020 18:13:36 +0000 Subject: [PATCH] auth is now per-method and not per-return-type --- gotham_restful/src/openapi/router.rs | 16 +++++++-------- gotham_restful/src/resource.rs | 5 +++++ gotham_restful/src/result.rs | 12 ----------- gotham_restful_derive/src/method.rs | 30 +++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index a97a4d7..9f2e984 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -384,7 +384,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.get = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::Res::requires_auth())); + item.get = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).read_all::() @@ -396,7 +396,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.get = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::Res::requires_auth())); + item.get = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).read::() @@ -408,7 +408,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(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_query_params(Handler::Query::schema()), None, None, Handler::Res::requires_auth())); + item.get = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_query_params(Handler::Query::schema()), None, None, Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).search::() @@ -421,7 +421,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.post = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth())); + item.post = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).create::() @@ -434,7 +434,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.put = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth())); + item.put = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).update_all::() @@ -447,7 +447,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.put = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth())); + item.put = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Handler::Body::supported_types(), Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).update::() @@ -459,7 +459,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.delete = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::Res::requires_auth())); + item.delete = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).delete_all::() @@ -471,7 +471,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.delete = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::Res::requires_auth())); + item.delete = Some(new_operation(Handler::operation_id(), Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::wants_auth())); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1).delete::() diff --git a/gotham_restful/src/resource.rs b/gotham_restful/src/resource.rs index a825cac..fd1c7e6 100644 --- a/gotham_restful/src/resource.rs +++ b/gotham_restful/src/resource.rs @@ -28,6 +28,11 @@ pub trait ResourceMethod { None } + + fn wants_auth() -> bool + { + false + } } /// Handle a GET request on the Resource root. diff --git a/gotham_restful/src/result.rs b/gotham_restful/src/result.rs index 8d93d6c..55f4c3c 100644 --- a/gotham_restful/src/result.rs +++ b/gotham_restful/src/result.rs @@ -95,12 +95,6 @@ pub trait ResourceResult { StatusCode::OK } - - #[cfg(feature = "openapi")] - fn requires_auth() -> bool - { - false - } } #[cfg(feature = "openapi")] @@ -348,12 +342,6 @@ impl ResourceResult for AuthResult { T::default_status() } - - #[cfg(feature = "openapi")] - fn requires_auth() -> bool - { - true - } } /** diff --git a/gotham_restful_derive/src/method.rs b/gotham_restful_derive/src/method.rs index 6cfb185..110f415 100644 --- a/gotham_restful_derive/src/method.rs +++ b/gotham_restful_derive/src/method.rs @@ -10,6 +10,7 @@ use syn::{ FnArg, ItemFn, Lit, + LitBool, Meta, NestedMeta, PatType, @@ -259,6 +260,31 @@ fn expand_operation_id(_ : &AttributeArgs) -> TokenStream2 quote!() } +fn expand_wants_auth(attrs : &AttributeArgs, default : bool) -> TokenStream2 +{ + let default_lit = Lit::Bool(LitBool { value: default, span: Span::call_site() }); + let mut wants_auth = &default_lit; + for meta in attrs + { + match meta { + NestedMeta::Meta(Meta::NameValue(kv)) => { + if kv.path.segments.last().map(|p| p.ident.to_string()) == Some("wants_auth".to_owned()) + { + wants_auth = &kv.lit + } + }, + _ => {} + } + } + + quote! { + fn wants_auth() -> bool + { + #wants_auth + } + } +} + fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result { let krate = super::krate(); @@ -390,8 +416,9 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result Result