diff --git a/Cargo.toml b/Cargo.toml index df70468..5e3cd83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,13 @@ version = "0.1.0" edition = "2021" [lib] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +proc-macro = true [dependencies] syn = "1.0" -quote = "1.0" \ No newline at end of file +quote = "1.0" +#proc-macro2 = "1.0" + +dev-dtos = { git = "https://github.com/franklinblanco/user-svc-dtos-rust.git" } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1" } \ No newline at end of file diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs index 4ef2ff0..9e0b96a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,24 @@ +mod auth; -macro_rules! test_macro { - ($out_struct:path, $path:literal) => ( { - println!($path); - }); -} -fn _s() { - test_macro!(String, "a"); +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn authenticated_route(_: TokenStream, _: TokenStream) -> TokenStream { + 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") { + 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