feat: add auth

This commit is contained in:
2025-12-26 00:19:25 +01:00
parent c07d6072fd
commit 85a02076f6
8 changed files with 73 additions and 5 deletions

36
Cargo.lock generated
View File

@@ -208,6 +208,21 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "actix-web-httpauth"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456348ed9dcd72a13a1f4a660449fafdecee9ac8205552e286809eb5b0b29bd3"
dependencies = [
"actix-utils",
"actix-web",
"base64",
"futures-core",
"futures-util",
"log",
"pin-project-lite",
]
[[package]] [[package]]
name = "adler2" name = "adler2"
version = "2.0.1" version = "2.0.1"
@@ -498,7 +513,10 @@ version = "0.1.0"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",
"actix-web-httpauth",
"chrono", "chrono",
"dotenv",
"futures-util",
"quartz", "quartz",
"reqwest", "reqwest",
"scp_core", "scp_core",
@@ -575,6 +593,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "dotenvy" name = "dotenvy"
version = "0.15.7" version = "0.15.7"
@@ -750,6 +774,17 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.31" version = "0.3.31"
@@ -770,6 +805,7 @@ checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io", "futures-io",
"futures-macro",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr",

View File

@@ -6,7 +6,10 @@ edition = "2024"
[dependencies] [dependencies]
actix-files = "0.6.9" actix-files = "0.6.9"
actix-web = "4.12.1" actix-web = "4.12.1"
actix-web-httpauth = "0.8.2"
chrono = "0.4.42" chrono = "0.4.42"
dotenv = "0.15.0"
futures-util = "0.3.31"
quartz = "0.0.4" quartz = "0.0.4"
reqwest = "0.12.28" reqwest = "0.12.28"
scp_core = { path = "./scp_core" } scp_core = { path = "./scp_core" }

View File

@@ -4,7 +4,6 @@ import {
IconDashboard, IconDashboard,
IconFileAi, IconFileAi,
IconFileDescription, IconFileDescription,
IconInnerShadowTop,
IconServer, IconServer,
} from "@tabler/icons-react"; } from "@tabler/icons-react";

View File

@@ -1,6 +1,5 @@
import { IconCirclePlusFilled, IconMail, type Icon } from "@tabler/icons-react"; import { type Icon } from "@tabler/icons-react";
import { Button } from "@/components/ui/button";
import { import {
SidebarGroup, SidebarGroup,
SidebarGroupContent, SidebarGroupContent,

View File

@@ -2,7 +2,6 @@ import { Card } from "@/components/ui/card";
import { import {
Table, Table,
TableBody, TableBody,
TableCaption,
TableCell, TableCell,
TableFooter, TableFooter,
TableHead, TableHead,

View File

@@ -21,7 +21,6 @@ pub async fn get_access_token() -> Result<String, Box<dyn Error>> {
.fetch_one(&pool) .fetch_one(&pool)
.await?; .await?;
dbg!(&tok);
Ok(tok.token) Ok(tok.token)
} }

View File

@@ -2,6 +2,7 @@ use std::env;
use actix_files::Files; use actix_files::Files;
use actix_web::{App, HttpResponse, HttpServer, Responder, get, web}; use actix_web::{App, HttpResponse, HttpServer, Responder, get, web};
use actix_web_httpauth::middleware::HttpAuthentication;
use scp_core::apis::configuration::Configuration; use scp_core::apis::configuration::Configuration;
use scp_core::apis::default_api; use scp_core::apis::default_api;
@@ -11,6 +12,7 @@ mod helper;
mod jobs; mod jobs;
mod models; mod models;
mod servers; mod servers;
mod validator;
#[get("/api/hello")] #[get("/api/hello")]
async fn hello() -> impl Responder { async fn hello() -> impl Responder {
@@ -31,6 +33,7 @@ async fn ping_netcup() -> impl Responder {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
let port = env::var("PORT") let port = env::var("PORT")
.unwrap_or_else(|_| "8080".to_string()) .unwrap_or_else(|_| "8080".to_string())
.parse::<u16>() .parse::<u16>()
@@ -43,6 +46,7 @@ async fn main() -> std::io::Result<()> {
let res = HttpServer::new(|| { let res = HttpServer::new(|| {
App::new() App::new()
.wrap(HttpAuthentication::basic(validator::basic_validator))
.service(hello) .service(hello)
.service(ping_netcup) .service(ping_netcup)
.service(auth::is_scp_logged_in) .service(auth::is_scp_logged_in)

29
src/validator.rs Normal file
View File

@@ -0,0 +1,29 @@
use std::env;
use actix_web::{Error, dev::ServiceRequest};
use actix_web_httpauth::extractors::{
AuthenticationError,
basic::{BasicAuth, Config},
};
use futures_util::future::{Ready, ready};
pub fn basic_validator(
req: ServiceRequest,
creds: BasicAuth,
) -> Ready<Result<ServiceRequest, (Error, ServiceRequest)>> {
let username = env::var("BASIC_USERNAME").expect("BASIC_USERNAME not set");
let password = env::var("BASIC_PASSWORD").expect("BASIC_PASSWORD not set");
let user_ok = creds.user_id() == username;
let pass_ok = creds.password().map(|p| p == password).unwrap_or(false);
if user_ok && pass_ok {
ready(Ok(req))
} else {
let mut config = Config::default();
config = config.realm("Restricted");
let err = AuthenticationError::from(config);
ready(Err((err.into(), req)))
}
}