From 4274fdaf939ab2181b556fe169cd51b7b485b765 Mon Sep 17 00:00:00 2001 From: despiegk Date: Thu, 8 May 2025 06:28:24 +0300 Subject: [PATCH] ... --- src/config/oauth.rs | 8 +++++- src/controllers/gitea_auth.rs | 13 +++++++--- src/controllers/mod.rs | 1 + src/routes/mod.rs | 47 +++++++++++++++++++---------------- src/views/auth/login.html | 2 ++ src/views/auth/register.html | 2 ++ 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/config/oauth.rs b/src/config/oauth.rs index eb62d91..321c0a5 100644 --- a/src/config/oauth.rs +++ b/src/config/oauth.rs @@ -1,8 +1,12 @@ -use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl}; +#[cfg(feature = "gitea")] use serde::{Deserialize, Serialize}; use std::env; +#[cfg(feature = "gitea")] +use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl}; + /// Gitea OAuth configuration +#[cfg(feature = "gitea")] #[derive(Clone, Debug)] pub struct GiteaOAuthConfig { /// OAuth client @@ -11,6 +15,7 @@ pub struct GiteaOAuthConfig { pub instance_url: String, } +#[cfg(feature = "gitea")] impl GiteaOAuthConfig { /// Creates a new Gitea OAuth configuration pub fn new() -> Self { @@ -48,6 +53,7 @@ impl GiteaOAuthConfig { } /// Gitea user information structure +#[cfg(feature = "gitea")] #[derive(Debug, Deserialize, Serialize)] pub struct GiteaUser { /// User ID diff --git a/src/controllers/gitea_auth.rs b/src/controllers/gitea_auth.rs index fcecde2..6af9903 100644 --- a/src/controllers/gitea_auth.rs +++ b/src/controllers/gitea_auth.rs @@ -1,15 +1,21 @@ use actix_web::{web, HttpRequest, HttpResponse, Responder, Result, http::header, cookie::Cookie}; use actix_session::Session; -use oauth2::{AuthorizationCode, CsrfToken, Scope, TokenResponse}; -use reqwest::Client; -use crate::config::oauth::GiteaOAuthConfig; use crate::models::user::{User, UserRole}; use crate::controllers::auth::AuthController; +#[cfg(feature = "gitea")] +use oauth2::{AuthorizationCode, CsrfToken, Scope, TokenResponse}; +#[cfg(feature = "gitea")] +use reqwest::Client; +#[cfg(feature = "gitea")] +use crate::config::oauth::GiteaOAuthConfig; + /// Controller for handling Gitea authentication +#[cfg(feature = "gitea")] pub struct GiteaAuthController; +#[cfg(feature = "gitea")] impl GiteaAuthController { /// Initiate the OAuth flow pub async fn login( @@ -207,6 +213,7 @@ impl GiteaAuthController { } /// Query parameters for the OAuth callback +#[cfg(feature = "gitea")] #[derive(serde::Deserialize)] pub struct CallbackQuery { pub code: String, diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index f7d1d35..3d5604b 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,5 +1,6 @@ // Export controllers pub mod auth; pub mod debug; +#[cfg(feature = "gitea")] pub mod gitea_auth; pub mod home; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 4c20b1b..6cf1dfc 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,6 +1,8 @@ +#[cfg(feature = "gitea")] use crate::config::oauth::GiteaOAuthConfig; use crate::controllers::auth::AuthController; use crate::controllers::debug::DebugController; +#[cfg(feature = "gitea")] use crate::controllers::gitea_auth::GiteaAuthController; use crate::controllers::home::HomeController; use crate::middleware::JwtAuth; @@ -10,9 +12,6 @@ use actix_web::web; /// Configures all application routes pub fn configure_routes(cfg: &mut web::ServiceConfig) { - // Create the OAuth configuration - let oauth_config = web::Data::new(GiteaOAuthConfig::new()); - // Configure session middleware with the consistent key let session_middleware = SessionMiddleware::builder(CookieSessionStore::default(), SESSION_KEY.clone()) @@ -27,29 +26,35 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) { ) .build(); - // Public routes that don't require authentication - cfg.service( - web::scope("") - .wrap(session_middleware) + let mut scope = web::scope("") + .wrap(session_middleware) + // Home routes + .route("/", web::get().to(HomeController::index)) + .route("/about", web::get().to(HomeController::about)) + // Auth routes + .route("/login", web::get().to(AuthController::login_page)) + .route("/login", web::post().to(AuthController::login)) + .route("/register", web::get().to(AuthController::register_page)) + .route("/register", web::post().to(AuthController::register)) + .route("/logout", web::get().to(AuthController::logout)) + // Debug routes + .route("/debug", web::get().to(DebugController::debug_info)); + + #[cfg(feature = "gitea")] + { + // Create the OAuth configuration + let oauth_config = web::Data::new(GiteaOAuthConfig::new()); + // Gitea OAuth configuration and routes + scope = scope .app_data(oauth_config.clone()) - // Home routes - .route("/", web::get().to(HomeController::index)) - .route("/about", web::get().to(HomeController::about)) - // Auth routes - .route("/login", web::get().to(AuthController::login_page)) - .route("/login", web::post().to(AuthController::login)) - .route("/register", web::get().to(AuthController::register_page)) - .route("/register", web::post().to(AuthController::register)) - .route("/logout", web::get().to(AuthController::logout)) - // Gitea OAuth routes .route("/auth/gitea", web::get().to(GiteaAuthController::login)) .route( "/auth/gitea/callback", web::get().to(GiteaAuthController::callback), - ) - // Debug routes - .route("/debug", web::get().to(DebugController::debug_info)), - ); + ); + } + + cfg.service(scope); // Protected routes that require authentication cfg.service( diff --git a/src/views/auth/login.html b/src/views/auth/login.html index 5efae3d..6a913e8 100644 --- a/src/views/auth/login.html +++ b/src/views/auth/login.html @@ -25,6 +25,7 @@
+ {% if gitea_enabled %}

Or login with:

@@ -33,6 +34,7 @@ Login with Gitea
+ {% endif %}
diff --git a/src/views/auth/register.html b/src/views/auth/register.html index 8ee2146..06e5227 100644 --- a/src/views/auth/register.html +++ b/src/views/auth/register.html @@ -34,6 +34,7 @@
+ {% if gitea_enabled %}

Or register with:

@@ -42,6 +43,7 @@ Register with Gitea
+ {% endif %}