Compare commits

..

2 Commits

Author SHA1 Message Date
4274fdaf93 ... 2025-05-08 06:28:24 +03:00
d98a2ac802 ... 2025-05-08 06:26:11 +03:00
9 changed files with 71 additions and 27 deletions

View File

@ -23,8 +23,11 @@ futures-util = "0.3"
num_cpus = "1.15" num_cpus = "1.15"
bcrypt = "0.14" bcrypt = "0.14"
uuid = { version = "1.3", features = ["v4", "serde"] } uuid = { version = "1.3", features = ["v4", "serde"] }
oauth2 = "4.3" oauth2 = { version = "4.3", optional = true }
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.11", features = ["json"], optional = true }
[features]
gitea = ["oauth2", "reqwest"]
[dev-dependencies] [dev-dependencies]
actix-rt = "2.8" actix-rt = "2.8"

View File

@ -1,8 +1,12 @@
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl}; #[cfg(feature = "gitea")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env; use std::env;
#[cfg(feature = "gitea")]
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
/// Gitea OAuth configuration /// Gitea OAuth configuration
#[cfg(feature = "gitea")]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct GiteaOAuthConfig { pub struct GiteaOAuthConfig {
/// OAuth client /// OAuth client
@ -11,6 +15,7 @@ pub struct GiteaOAuthConfig {
pub instance_url: String, pub instance_url: String,
} }
#[cfg(feature = "gitea")]
impl GiteaOAuthConfig { impl GiteaOAuthConfig {
/// Creates a new Gitea OAuth configuration /// Creates a new Gitea OAuth configuration
pub fn new() -> Self { pub fn new() -> Self {
@ -48,6 +53,7 @@ impl GiteaOAuthConfig {
} }
/// Gitea user information structure /// Gitea user information structure
#[cfg(feature = "gitea")]
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct GiteaUser { pub struct GiteaUser {
/// User ID /// User ID

View File

@ -1,15 +1,21 @@
use actix_web::{web, HttpRequest, HttpResponse, Responder, Result, http::header, cookie::Cookie}; use actix_web::{web, HttpRequest, HttpResponse, Responder, Result, http::header, cookie::Cookie};
use actix_session::Session; 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::models::user::{User, UserRole};
use crate::controllers::auth::AuthController; 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 /// Controller for handling Gitea authentication
#[cfg(feature = "gitea")]
pub struct GiteaAuthController; pub struct GiteaAuthController;
#[cfg(feature = "gitea")]
impl GiteaAuthController { impl GiteaAuthController {
/// Initiate the OAuth flow /// Initiate the OAuth flow
pub async fn login( pub async fn login(
@ -207,6 +213,7 @@ impl GiteaAuthController {
} }
/// Query parameters for the OAuth callback /// Query parameters for the OAuth callback
#[cfg(feature = "gitea")]
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct CallbackQuery { pub struct CallbackQuery {
pub code: String, pub code: String,

View File

@ -1,5 +1,6 @@
// Export controllers // Export controllers
pub mod auth; pub mod auth;
pub mod debug; pub mod debug;
#[cfg(feature = "gitea")]
pub mod gitea_auth; pub mod gitea_auth;
pub mod home; pub mod home;

View File

@ -1,6 +1,8 @@
#[cfg(feature = "gitea")]
use crate::config::oauth::GiteaOAuthConfig; use crate::config::oauth::GiteaOAuthConfig;
use crate::controllers::auth::AuthController; use crate::controllers::auth::AuthController;
use crate::controllers::debug::DebugController; use crate::controllers::debug::DebugController;
#[cfg(feature = "gitea")]
use crate::controllers::gitea_auth::GiteaAuthController; use crate::controllers::gitea_auth::GiteaAuthController;
use crate::controllers::home::HomeController; use crate::controllers::home::HomeController;
use crate::middleware::JwtAuth; use crate::middleware::JwtAuth;
@ -10,9 +12,6 @@ use actix_web::web;
/// Configures all application routes /// Configures all application routes
pub fn configure_routes(cfg: &mut web::ServiceConfig) { 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 // Configure session middleware with the consistent key
let session_middleware = let session_middleware =
SessionMiddleware::builder(CookieSessionStore::default(), SESSION_KEY.clone()) SessionMiddleware::builder(CookieSessionStore::default(), SESSION_KEY.clone())
@ -27,11 +26,8 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) {
) )
.build(); .build();
// Public routes that don't require authentication let mut scope = web::scope("")
cfg.service(
web::scope("")
.wrap(session_middleware) .wrap(session_middleware)
.app_data(oauth_config.clone())
// Home routes // Home routes
.route("/", web::get().to(HomeController::index)) .route("/", web::get().to(HomeController::index))
.route("/about", web::get().to(HomeController::about)) .route("/about", web::get().to(HomeController::about))
@ -41,15 +37,24 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) {
.route("/register", web::get().to(AuthController::register_page)) .route("/register", web::get().to(AuthController::register_page))
.route("/register", web::post().to(AuthController::register)) .route("/register", web::post().to(AuthController::register))
.route("/logout", web::get().to(AuthController::logout)) .route("/logout", web::get().to(AuthController::logout))
// Gitea OAuth routes // 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())
.route("/auth/gitea", web::get().to(GiteaAuthController::login)) .route("/auth/gitea", web::get().to(GiteaAuthController::login))
.route( .route(
"/auth/gitea/callback", "/auth/gitea/callback",
web::get().to(GiteaAuthController::callback), web::get().to(GiteaAuthController::callback),
)
// Debug routes
.route("/debug", web::get().to(DebugController::debug_info)),
); );
}
cfg.service(scope);
// Protected routes that require authentication // Protected routes that require authentication
cfg.service( cfg.service(

View File

@ -25,6 +25,7 @@
<hr> <hr>
{% if gitea_enabled %}
<div class="text-center"> <div class="text-center">
<p>Or login with:</p> <p>Or login with:</p>
<a href="/auth/gitea" class="btn btn-secondary"> <a href="/auth/gitea" class="btn btn-secondary">
@ -33,6 +34,7 @@
Login with Gitea Login with Gitea
</a> </a>
</div> </div>
{% endif %}
<hr> <hr>

View File

@ -34,6 +34,7 @@
<hr> <hr>
{% if gitea_enabled %}
<div class="text-center"> <div class="text-center">
<p>Or register with:</p> <p>Or register with:</p>
<a href="/auth/gitea" class="btn btn-secondary"> <a href="/auth/gitea" class="btn btn-secondary">
@ -42,6 +43,7 @@
Register with Gitea Register with Gitea
</a> </a>
</div> </div>
{% endif %}
<hr> <hr>

8
start.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/bash
# Get the directory of the script and change to it
cd "$(dirname "$0")"
export SECRET_KEY=1234
cargo run

10
start_with_gitea.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
# Get the directory of the script and change to it
cd "$(dirname "$0")"
export GITEA_CLIENT_ID="your_client_id"
export GITEA_CLIENT_SECRET="your_client_secret"
export GITEA_INSTANCE_URL="https://gitea.example.com"
export APP_URL="http://localhost:9999"
cargo run --features gitea