From 1736e2d14d1fca2320a12d751fc486ad25391f84 Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 24 Jan 2023 18:01:34 -0400 Subject: [PATCH] Replaced attr macro with macrorules --- src/lib.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c56c49d..c8a80f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,6 @@ mod auth; - -use proc_macro::TokenStream; - -#[proc_macro_attribute] +/*#[proc_macro_attribute] pub fn authenticated_route(_: TokenStream, mut input: TokenStream) -> TokenStream { let tt_to_add = TokenStream::from(quote::quote!{ let header_conversion_result: dev_dtos::dtos::user::user_dtos::UserAuthHeader = match serde_json::from_str(match request.headers().get("authentication") { @@ -23,4 +20,23 @@ pub fn authenticated_route(_: TokenStream, mut input: TokenStream) -> TokenStrea }); input.extend(tt_to_add); input +}*/ + +macro_rules! authenticate_route { + () => { + let header_conversion_result: dev_dtos::dtos::user::user_dtos::UserAuthHeader = match serde_json::from_str(match request.headers().get("authentication") { + Some(auth_header) => match auth_header.to_str() { + Ok(string) => string, + Err(_) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(400, err::MessageResource::new_from_str("Auth header in incorrect format.")), + }, + None => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_str("No auth header present.")), + }) { + Ok(user_for_authentication) => user_for_authentication, + Err(error) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(error.to_string())), + }; + let authenticated_user = match dev_communicators::middleware::user_svc::user_service::authenticate_user_with_token(&client, &header_conversion_result.into()).await { + Ok(authed_user) => authed_user, + Err(error) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_error(401, err::MessageResource::new_from_string(error.to_string())), + }; + }; } \ No newline at end of file