From 2375eedc6899a728fb3e363f9f6f5d5117b1cc7b Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Mon, 23 Oct 2023 16:47:39 +0530 Subject: [PATCH] some more cleanups --- fastn-core/src/auth/github.rs | 27 ++++----------------------- fastn-core/src/auth/routes.rs | 16 +++------------- fastn-core/src/http.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/fastn-core/src/auth/github.rs b/fastn-core/src/auth/github.rs index fab82799c8..d082fe3b3e 100644 --- a/fastn-core/src/auth/github.rs +++ b/fastn-core/src/auth/github.rs @@ -1,5 +1,3 @@ -// TODO: This has be set while creating the GitHub OAuth Application -pub const CALLBACK_URL: &str = "/-/auth/github/callback/"; #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct UserDetail { pub token: String, @@ -9,25 +7,11 @@ pub struct UserDetail { pub async fn login( req: &fastn_core::http::Request, ) -> fastn_core::Result { - // GitHub will be redirect to this url after login process completed - - let mut next_url = "/".to_string(); - if let Ok(queries) = - actix_web::web::Query::>::from_query( - req.query_string(), - ) - { - if queries.get("next").is_some() { - next_url = queries.get("next").unwrap().to_string(); - } - } - let redirect_url: String = format!( - "{}://{}{}?next={}", + "{}://{}/-/auth/github/?next={}", req.connection_info.scheme(), req.connection_info.host(), - CALLBACK_URL, - next_url, + req.q("next", "/".to_string())?, ); // Set up the config for the Github OAuth2 process. @@ -50,9 +34,7 @@ pub async fn login( // let mut pairs: Vec<(&str, &str)> = vec![("response_type", self.response_type.as_ref())]; // send redirect to /auth/github/callback/ - Ok(actix_web::HttpResponse::Found() - .append_header((actix_web::http::header::LOCATION, authorize_url.to_string())) - .finish()) + Ok(fastn_core::http::redirect(authorize_url.to_string())) } // route: /auth/github/callback/ @@ -69,10 +51,9 @@ pub async fn callback( } let query = actix_web::web::Query::::from_query(req.query_string())?.0; let auth_url = format!( - "{}://{}{}", + "{}://{}/-/auth/github/", req.connection_info.scheme(), req.connection_info.host(), - CALLBACK_URL ); let client = utils::github_client().set_redirect_uri(oauth2::RedirectUrl::new(auth_url)?); match client diff --git a/fastn-core/src/auth/routes.rs b/fastn-core/src/auth/routes.rs index 7b688ae156..84b6807d47 100644 --- a/fastn-core/src/auth/routes.rs +++ b/fastn-core/src/auth/routes.rs @@ -4,20 +4,9 @@ pub async fn login(req: &fastn_core::http::Request) -> fastn_core::Result::from_query(req.query_string()) { - Ok(q) => q, - Err(err) => { - dbg!(err); - return Ok(actix_web::HttpResponse::BadRequest() - .body("Please select the platform, by which you want to login")); - } - }; - match query.platform.as_str() { + match platform.as_str() { "github" => fastn_core::auth::github::login(req).await, _ => { return Ok(actix_web::HttpResponse::BadRequest() @@ -46,6 +35,7 @@ pub async fn handle_auth( ) -> fastn_core::Result { match req.path() { "/-/auth/login/" => login(&req).await, + // TODO: This has be set while creating the GitHub OAuth Application "/-/auth/github/" => fastn_core::auth::github::callback(&req).await, "/-/auth/logout/" => logout(), _ => Ok(fastn_core::not_found!("route not found: {}", req.path())), diff --git a/fastn-core/src/http.rs b/fastn-core/src/http.rs index 67dc462100..55e6893715 100644 --- a/fastn-core/src/http.rs +++ b/fastn-core/src/http.rs @@ -231,6 +231,18 @@ impl Request { &self.query } + pub fn q(&self, key: &str, default: T) -> fastn_core::Result + where + T: serde::de::DeserializeOwned, + { + let value = match self.query.get(key) { + Some(v) => v, + None => return Ok(default), + }; + + Ok(serde_json::from_value(value.clone())?) + } + pub fn get_ip(&self) -> Option { self.ip.clone() }