71 lines
2.4 KiB
Rust
71 lines
2.4 KiB
Rust
use actix_web::{HttpResponse, http::header::{ContentType, self}, web, HttpRequest, cookie::{Cookie, SameSite}};
|
|
use chrono::Local;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{models::{AppState, SessionUuid, User, ProjectSummary}, templates};
|
|
|
|
pub mod authentication;
|
|
pub mod binary_cache;
|
|
|
|
pub use authentication::*;
|
|
pub use binary_cache::*;
|
|
|
|
pub async fn landing_page (app_state: web::Data<AppState<'_>>) -> HttpResponse {
|
|
let summaries: Vec<ProjectSummary> = vec![
|
|
ProjectSummary {
|
|
name: "Test Project".to_string(),
|
|
latest_closure: "/nix/store/blabla".to_string(),
|
|
latest_closure_datetime: Local::now(),
|
|
}
|
|
];
|
|
let content: String = templates::landing_page(app_state.hbs.clone(), true, summaries).unwrap();
|
|
HttpResponse::Ok()
|
|
.content_type(ContentType::html())
|
|
.body(content)
|
|
/*
|
|
match check_authentication(app_state.clone(), req).await {
|
|
Ok(_) => {
|
|
|
|
let content: String = templates::landing_page(app_state.hbs.clone(), true).unwrap();
|
|
HttpResponse::Ok()
|
|
.content_type(ContentType::html())
|
|
.body(content)
|
|
},
|
|
Err(redirect) => redirect,
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
Checks if the user requesting the page is authenticated.
|
|
*/
|
|
async fn check_authentication(app_state: &web::Data<AppState<'_>>, req: HttpRequest) -> Result<User, HttpResponse> {
|
|
|
|
fn redirect_to_login<T>(e: T, req: &HttpRequest) -> HttpResponse
|
|
where T: ToString
|
|
{
|
|
eprintln!("check_authentication: invalid session {}", e.to_string());
|
|
let secure = false;
|
|
#[cfg(not(debug_assertions))]
|
|
let secure = true;
|
|
let redirect_cookie = Cookie::build("redirect", req.path())
|
|
.http_only(true)
|
|
.same_site(SameSite::Strict)
|
|
.path("/")
|
|
.secure(secure)
|
|
.finish();
|
|
HttpResponse::Found()
|
|
.append_header((header::LOCATION, "/login"))
|
|
.cookie(redirect_cookie)
|
|
.body("Please login")
|
|
}
|
|
|
|
let auth_session = app_state.session.user_sessions.read().await;
|
|
let cookie = req.cookie("auth-uuid").ok_or_else(|| redirect_to_login("missing cookie in request", &req))?;
|
|
let cookie = cookie.value();
|
|
let client_uuid = Uuid::parse_str(&cookie).map_err(|e| redirect_to_login(e, &req))?;
|
|
let user = auth_session.get(&SessionUuid(client_uuid)).ok_or_else(|| redirect_to_login("cannot find UUID in session", &req))?;
|
|
Ok(user.clone())
|
|
}
|