diff --git a/src/google_oauth.rs b/src/google_oauth.rs index ec061bb..0eb8bb5 100644 --- a/src/google_oauth.rs +++ b/src/google_oauth.rs @@ -4,6 +4,6 @@ async fn google_auth(Extension(client): Extension) -> impl IntoResp .add_scope(Scope::new("identify".to_string())) .url(); - // Redirect to Discord's oauth service + // Redirect to Google's oauth service Redirect::to(auth_url.to_string().parse().unwrap()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e30639f..e3bb297 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,15 +18,15 @@ use axum::{ Router, }; use http::header; -use oauth2::{basic::BasicClient, reqwest::async_http_client, AuthorizationCode, TokenResponse}; +use oauth2::{ + basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId, + ClientSecret, RedirectUrl, TokenResponse, TokenUrl, +}; use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; +use std::{env, net::SocketAddr}; use tower_http::{services::ServeDir, trace::TraceLayer}; use uuid::Uuid; -mod oauth; -use oauth::oauth_client; - const COOKIE_NAME: &str = "SESSION"; // The user data we'll get back from Discord. @@ -67,6 +67,7 @@ async fn main() { ), ) .route("/", get(index)) + .route("/login", get(login)) .layer(Extension(store)) .layer(Extension(oauth_client)); @@ -90,6 +91,46 @@ async fn index(user: Option) -> impl IntoResponse { HtmlTemplate(template) } +async fn login() -> impl IntoResponse { + let name = "".to_string(); + let template = LoginTemplate { name }; + HtmlTemplate(template) +} + +fn oauth_client() -> BasicClient { + // Environment variables (* = required): + // *"CLIENT_ID" "REPLACE_ME"; + // *"CLIENT_SECRET" "REPLACE_ME"; + // "REDIRECT_URL" "http://127.0.0.1:3000/auth/authorized"; + // "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code"; + // "TOKEN_URL" "https://discord.com/api/oauth2/token"; + + let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!"); + let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!"); + let redirect_url = env::var("REDIRECT_URL") + .unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string()); + + let auth_url = env::var("AUTH_URL").unwrap_or_else(|_| { + "https://discord.com/api/oauth2/authorize?response_type=code".to_string() + }); + + let token_url = env::var("TOKEN_URL") + .unwrap_or_else(|_| "https://discord.com/api/oauth2/token".to_string()); + + BasicClient::new( + ClientId::new(client_id), + Some(ClientSecret::new(client_secret)), + AuthUrl::new(auth_url).unwrap(), + Some(TokenUrl::new(token_url).unwrap()), + ) + .set_redirect_uri(RedirectUrl::new(redirect_url).unwrap()) +} + +#[derive(Template)] +#[template(path = "login.html")] +struct LoginTemplate { + name: String, +} struct FreshUserId { pub user_id: UserId, pub cookie: HeaderValue, diff --git a/src/oauth.rs b/src/oauth.rs deleted file mode 100644 index 9d461cd..0000000 --- a/src/oauth.rs +++ /dev/null @@ -1,31 +0,0 @@ -use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl}; -use std::env; - -pub fn oauth_client() -> BasicClient { - // Environment variables (* = required): - // *"CLIENT_ID" "REPLACE_ME"; - // *"CLIENT_SECRET" "REPLACE_ME"; - // "REDIRECT_URL" "http://127.0.0.1:3000/auth/authorized"; - // "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code"; - // "TOKEN_URL" "https://discord.com/api/oauth2/token"; - - let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!"); - let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!"); - let redirect_url = env::var("REDIRECT_URL") - .unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string()); - - let auth_url = env::var("AUTH_URL").unwrap_or_else(|_| { - "https://discord.com/api/oauth2/authorize?response_type=code".to_string() - }); - - let token_url = env::var("TOKEN_URL") - .unwrap_or_else(|_| "https://discord.com/api/oauth2/token".to_string()); - - BasicClient::new( - ClientId::new(client_id), - Some(ClientSecret::new(client_secret)), - AuthUrl::new(auth_url).unwrap(), - Some(TokenUrl::new(token_url).unwrap()), - ) - .set_redirect_uri(RedirectUrl::new(redirect_url).unwrap()) -} diff --git a/templates/base.html b/templates/base.html index bc7c911..e32d9ca 100644 --- a/templates/base.html +++ b/templates/base.html @@ -44,10 +44,8 @@