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

View File

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

View File

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