Skip to content

Commit

Permalink
some more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Oct 23, 2023
1 parent d69d021 commit 2375eed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
27 changes: 4 additions & 23 deletions fastn-core/src/auth/github.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,25 +7,11 @@ pub struct UserDetail {
pub async fn login(
req: &fastn_core::http::Request,
) -> fastn_core::Result<fastn_core::http::Response> {
// 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::<std::collections::HashMap<String, String>>::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.
Expand All @@ -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/
Expand All @@ -69,10 +51,9 @@ pub async fn callback(
}
let query = actix_web::web::Query::<QueryParams>::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
Expand Down
16 changes: 3 additions & 13 deletions fastn-core/src/auth/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@ pub async fn login(req: &fastn_core::http::Request) -> fastn_core::Result<actix_
return Ok(fastn_core::http::redirect("/".to_string()));
}

#[derive(serde::Deserialize)]
pub struct QueryParams {
pub platform: String,
}
let platform = req.q("platform", "github".to_string())?;

let query = match actix_web::web::Query::<QueryParams>::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()

Check failure on line 12 in fastn-core/src/auth/routes.rs

View workflow job for this annotation

GitHub Actions / Rust Checks

unneeded `return` statement
Expand Down Expand Up @@ -46,6 +35,7 @@ pub async fn handle_auth(
) -> fastn_core::Result<fastn_core::http::Response> {
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())),
Expand Down
12 changes: 12 additions & 0 deletions fastn-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ impl Request {
&self.query
}

pub fn q<T>(&self, key: &str, default: T) -> fastn_core::Result<T>
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<String> {
self.ip.clone()
}
Expand Down

0 comments on commit 2375eed

Please sign in to comment.