Compare commits
5 Commits
renovate/u
...
20251204
| Author | SHA1 | Date | |
|---|---|---|---|
| 054fc0c4cc | |||
| 3d19a0eba2 | |||
| 944e7dd00d | |||
| cb739ebe6d | |||
| a2af039759 |
1240
Cargo.lock
generated
1240
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
@@ -9,7 +9,7 @@ edition = "2024"
|
||||
actix = "0.13.5"
|
||||
actix-identity = "0.9.0"
|
||||
actix-web = "4.12.1"
|
||||
actix-session = { version = "0.11.0", features = ["cookie-session"] }
|
||||
actix-session = { version = "0.11.0", features = ["cookie-session", "redis-session"] }
|
||||
actix-remote-ip = "0.1.0"
|
||||
clap = { version = "4.5.53", features = ["derive", "env"] }
|
||||
include_dir = "0.7.4"
|
||||
@@ -23,11 +23,11 @@ uuid = { version = "1.19.0", features = ["v4"] }
|
||||
mime_guess = "2.0.5"
|
||||
askama = "0.14.0"
|
||||
urlencoding = "2.1.3"
|
||||
rand = "0.9.2"
|
||||
rand = "0.10.0-rc.5"
|
||||
base64 = "0.22.1"
|
||||
jwt-simple = { version = "0.12.13", default-features = false, features = ["pure-rust"] }
|
||||
digest = "0.10.7"
|
||||
sha2 = "0.10.9"
|
||||
digest = "0.11.0-rc.4"
|
||||
sha2 = "0.11.0-rc.3"
|
||||
lazy-regex = "3.4.2"
|
||||
totp_rfc6238 = "0.6.1"
|
||||
base32 = "0.5.1"
|
||||
@@ -39,3 +39,5 @@ bincode = "2.0.1"
|
||||
chrono = "0.4.42"
|
||||
lazy_static = "1.5.0"
|
||||
mailchecker = "6.0.19"
|
||||
httpdate = "1.0.3"
|
||||
build-time = "0.1.3"
|
||||
|
||||
2
assets/css/bootstrap.css
vendored
2
assets/css/bootstrap.css
vendored
@@ -11,7 +11,7 @@
|
||||
* Copyright 2011-2024 The Bootstrap Authors
|
||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
||||
*/
|
||||
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");
|
||||
@import url("/assets/robotfont/font.css");
|
||||
:root,
|
||||
[data-bs-theme=light] {
|
||||
--bs-blue: #2a9fd6;
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
services:
|
||||
redis:
|
||||
image: redis
|
||||
network_mode: host
|
||||
volumes:
|
||||
- ./redis/redis.conf:/redis.conf:ro
|
||||
command: [ "redis-server", "/redis.conf" ]
|
||||
healthcheck:
|
||||
test: [ "CMD", "redis-cli", "ping" ]
|
||||
interval: 10s
|
||||
timeout: 30s
|
||||
retries: 3
|
||||
upstream:
|
||||
image: dexidp/dex
|
||||
user: "1000"
|
||||
@@ -33,9 +44,14 @@ services:
|
||||
image: rust
|
||||
user: "1000"
|
||||
network_mode: host
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
- STORAGE_PATH=/storage
|
||||
- DISABLE_LOCAL_LOGIN=true
|
||||
- USE_REDIS=true
|
||||
- REDIS_PASSWORD=secretsecret
|
||||
#- RUST_LOG=debug
|
||||
volumes:
|
||||
- ../:/app
|
||||
|
||||
3
sample_upstream_provider/redis/redis.conf
Normal file
3
sample_upstream_provider/redis/redis.conf
Normal file
@@ -0,0 +1,3 @@
|
||||
maxmemory 256mb
|
||||
maxmemory-policy allkeys-lru
|
||||
requirepass secretsecret
|
||||
@@ -5,7 +5,7 @@ use std::net::IpAddr;
|
||||
use actix::{Actor, AsyncContext, Context, Handler, Message};
|
||||
|
||||
use crate::constants::{FAIL_LOGIN_ATTEMPT_CLEANUP_INTERVAL, KEEP_FAILED_LOGIN_ATTEMPTS_FOR};
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "()")]
|
||||
@@ -87,7 +87,7 @@ mod test {
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
use crate::actors::bruteforce_actor::BruteForceActor;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
const IP_1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
|
||||
const IP_2: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::data::jwt_signer::JWTSigner;
|
||||
use crate::data::user::UserID;
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
|
||||
pub struct SessionID(pub String);
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::net::IpAddr;
|
||||
use crate::data::login_redirect::LoginRedirect;
|
||||
use crate::data::provider::ProviderID;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct ProviderLoginState {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::path::Path;
|
||||
|
||||
use actix_web::{HttpResponse, web};
|
||||
use crate::utils::crypt_utils::sha256;
|
||||
use crate::utils::time_utils;
|
||||
use actix_web::http::header;
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use include_dir::{Dir, include_dir};
|
||||
use std::ops::Add;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Assets directory
|
||||
static ASSETS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/assets");
|
||||
@@ -12,14 +16,39 @@ pub async fn robots_txt() -> HttpResponse {
|
||||
.body(include_str!("../../assets/robots.txt"))
|
||||
}
|
||||
|
||||
pub async fn assets_route(path: web::Path<String>) -> HttpResponse {
|
||||
pub async fn assets_route(req: HttpRequest, path: web::Path<String>) -> HttpResponse {
|
||||
let path: &Path = path.as_ref().as_ref();
|
||||
match ASSETS_DIR.get_file(path) {
|
||||
None => HttpResponse::NotFound().body("404 Not found"),
|
||||
Some(file) => {
|
||||
let res = mime_guess::from_path(path).first_or_octet_stream();
|
||||
let digest = format!("{:x?}", sha256(file.contents()));
|
||||
|
||||
// Check if the browser already knows the file by date
|
||||
if let Some(c) = req.headers().get(header::IF_MODIFIED_SINCE) {
|
||||
let date_str = c.to_str().unwrap_or("");
|
||||
if let Ok(date) = httpdate::parse_http_date(date_str)
|
||||
&& date.add(Duration::from_secs(1))
|
||||
>= time_utils::unix_to_system_time(time_utils::build_time())
|
||||
{
|
||||
return HttpResponse::NotModified().finish();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the browser already knows the etag
|
||||
if let Some(c) = req.headers().get(header::IF_NONE_MATCH)
|
||||
&& c.to_str().unwrap_or("") == digest
|
||||
{
|
||||
return HttpResponse::NotModified().finish();
|
||||
}
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type(res.to_string())
|
||||
.insert_header(("etag", digest))
|
||||
.insert_header((
|
||||
"last-modified",
|
||||
time_utils::unix_to_http_date(time_utils::build_time()),
|
||||
))
|
||||
.body(file.contents())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ use crate::data::login_redirect::{LoginRedirect, get_2fa_url};
|
||||
use crate::data::session_identity::SessionIdentity;
|
||||
use crate::data::user::User;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
pub async fn get_configuration(req: HttpRequest) -> impl Responder {
|
||||
let is_secure_request = req
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::data::current_user::CurrentUser;
|
||||
use crate::data::totp_key::TotpKey;
|
||||
use crate::data::user::User;
|
||||
use crate::data::webauthn_manager::WebAuthManagerReq;
|
||||
use crate::utils::time::fmt_time;
|
||||
use crate::utils::time_utils::fmt_time;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "settings/two_factors_page.html")]
|
||||
|
||||
@@ -17,7 +17,7 @@ use crate::data::provider::{Provider, ProviderID};
|
||||
|
||||
use crate::data::session_identity::SessionIdentity;
|
||||
use crate::data::user::{FactorID, GrantedClients, TwoFactor, TwoFactorType, User, UserID};
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct LoggableUser {
|
||||
|
||||
@@ -67,6 +67,30 @@ pub struct AppConfig {
|
||||
/// to authenticate
|
||||
#[arg(long, env)]
|
||||
pub disable_local_login: bool,
|
||||
|
||||
/// Use Redis to store cookie sessions
|
||||
#[arg(long, env)]
|
||||
pub use_redis: bool,
|
||||
|
||||
/// Redis connection hostname
|
||||
#[clap(long, env, default_value = "localhost")]
|
||||
redis_hostname: String,
|
||||
|
||||
/// Redis connection port
|
||||
#[clap(long, env, default_value_t = 6379)]
|
||||
redis_port: u16,
|
||||
|
||||
/// Redis database number
|
||||
#[clap(long, env, default_value_t = 0)]
|
||||
redis_db_number: i64,
|
||||
|
||||
/// Redis username
|
||||
#[clap(long, env)]
|
||||
redis_username: Option<String>,
|
||||
|
||||
/// Redis password
|
||||
#[clap(long, env, default_value = "secretredis")]
|
||||
redis_password: String,
|
||||
}
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
@@ -137,6 +161,21 @@ impl AppConfig {
|
||||
let domain = self.domain_name();
|
||||
domain.split_once(':').map(|i| i.0).unwrap_or(domain)
|
||||
}
|
||||
|
||||
/// Get Redis connection configuration
|
||||
pub fn redis_connection_string(&self) -> Option<String> {
|
||||
match self.use_redis {
|
||||
true => Some(format!(
|
||||
"redis://{}:{}@{}:{}/{}",
|
||||
self.redis_username.as_deref().unwrap_or(""),
|
||||
self.redis_password,
|
||||
self.redis_hostname,
|
||||
self.redis_port,
|
||||
self.redis_db_number
|
||||
)),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::actors::users_actor::UsersActor;
|
||||
use crate::constants::SECOND_FACTOR_EXPIRATION_FOR_CRITICAL_OPERATIONS;
|
||||
use crate::data::session_identity::SessionIdentity;
|
||||
use crate::data::user::User;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
pub struct CurrentUser {
|
||||
user: User,
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::data::app_config::AppConfig;
|
||||
|
||||
use crate::data::provider::Provider;
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
/// Provider configuration
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -3,7 +3,7 @@ use actix_web::{HttpMessage, HttpRequest};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::data::user::{User, UserID};
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Default)]
|
||||
pub enum SessionStatus {
|
||||
|
||||
@@ -5,7 +5,7 @@ use totp_rfc6238::{HashAlgorithm, TotpGenerator};
|
||||
use crate::data::app_config::AppConfig;
|
||||
use crate::data::user::User;
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
const BASE32_ALPHABET: Alphabet = Alphabet::Rfc4648 { padding: true };
|
||||
const NUM_DIGITS: usize = 6;
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::data::login_redirect::LoginRedirect;
|
||||
use crate::data::provider::{Provider, ProviderID};
|
||||
use crate::data::totp_key::TotpKey;
|
||||
use crate::data::webauthn_manager::WebauthnPubKey;
|
||||
use crate::utils::time::{fmt_time, time};
|
||||
use crate::utils::time_utils::{fmt_time, time};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, Encode, Decode)]
|
||||
pub struct UserID(pub String);
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::actors::users_actor::{AuthorizedAuthenticationSources, UsersSyncBacke
|
||||
use crate::data::entity_manager::EntityManager;
|
||||
use crate::data::user::{FactorID, GeneralSettings, GrantedClients, TwoFactor, User, UserID};
|
||||
use crate::utils::err::{Res, new_error};
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
impl EntityManager<User> {
|
||||
/// Update user information
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::constants::{
|
||||
use crate::data::app_config::AppConfig;
|
||||
use crate::data::user::{User, UserID};
|
||||
use crate::utils::err::Res;
|
||||
use crate::utils::time::time;
|
||||
use crate::utils::time_utils::time;
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct WebauthnPubKey {
|
||||
|
||||
35
src/main.rs
35
src/main.rs
@@ -1,12 +1,12 @@
|
||||
use core::time::Duration;
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix::Actor;
|
||||
use actix::{Actor, Addr};
|
||||
use actix_identity::IdentityMiddleware;
|
||||
use actix_identity::config::LogoutBehavior;
|
||||
use actix_remote_ip::RemoteIPConfig;
|
||||
use actix_session::SessionMiddleware;
|
||||
use actix_session::storage::CookieSessionStore;
|
||||
use actix_session::storage::{CookieSessionStore, RedisSessionStore};
|
||||
use actix_web::cookie::{Key, SameSite};
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::{App, HttpResponse, HttpServer, get, middleware, web};
|
||||
@@ -14,6 +14,7 @@ use actix_web::{App, HttpResponse, HttpServer, get, middleware, web};
|
||||
use basic_oidc::actors::bruteforce_actor::BruteForceActor;
|
||||
use basic_oidc::actors::openid_sessions_actor::OpenIDSessionsActor;
|
||||
use basic_oidc::actors::providers_states_actor::ProvidersStatesActor;
|
||||
use basic_oidc::actors::users_actor;
|
||||
use basic_oidc::actors::users_actor::{UsersActor, UsersSyncBackend};
|
||||
use basic_oidc::constants::*;
|
||||
use basic_oidc::controllers::assets_controller::assets_route;
|
||||
@@ -26,9 +27,20 @@ use basic_oidc::data::provider::ProvidersManager;
|
||||
use basic_oidc::data::user::User;
|
||||
use basic_oidc::data::webauthn_manager::WebAuthManager;
|
||||
use basic_oidc::middlewares::auth_middleware::AuthMiddleware;
|
||||
use basic_oidc::middlewares::multi_session_store::MultiSessionStore;
|
||||
|
||||
#[get("/health")]
|
||||
async fn health() -> &'static str {
|
||||
async fn health(users: web::Data<Addr<UsersActor>>) -> &'static str {
|
||||
// Check users list is working
|
||||
let users = users
|
||||
.send(users_actor::GetAllUsers)
|
||||
.await
|
||||
.expect("Could not retrieve users list!");
|
||||
if users.is_empty() {
|
||||
log::error!("Got empty list of users!");
|
||||
panic!("Got empty list of users!");
|
||||
}
|
||||
|
||||
"Running"
|
||||
}
|
||||
|
||||
@@ -69,6 +81,18 @@ async fn main() -> std::io::Result<()> {
|
||||
.expect("Failed to change default admin password!");
|
||||
}
|
||||
|
||||
let redis_store = match AppConfig::get().redis_connection_string() {
|
||||
Some(s) => {
|
||||
log::info!("Connect to Redis session store...");
|
||||
Some(
|
||||
RedisSessionStore::new(s)
|
||||
.await
|
||||
.expect("Failed to connect to Redis!"),
|
||||
)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let users_actor = UsersActor::new(users).start();
|
||||
let bruteforce_actor = BruteForceActor::default().start();
|
||||
let providers_states_actor = ProvidersStatesActor::default().start();
|
||||
@@ -91,7 +115,10 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(move || {
|
||||
let session_mw = SessionMiddleware::builder(
|
||||
CookieSessionStore::default(),
|
||||
match redis_store.clone() {
|
||||
None => MultiSessionStore::Cookie(CookieSessionStore::default()),
|
||||
Some(s) => MultiSessionStore::Redis(s),
|
||||
},
|
||||
Key::from(config.token_key.as_bytes()),
|
||||
)
|
||||
.cookie_name(SESSION_COOKIE_NAME.to_string())
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod auth_middleware;
|
||||
pub mod multi_session_store;
|
||||
|
||||
63
src/middlewares/multi_session_store.rs
Normal file
63
src/middlewares/multi_session_store.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use actix_session::storage::{
|
||||
CookieSessionStore, LoadError, RedisSessionStore, SaveError, SessionKey, SessionStore,
|
||||
UpdateError,
|
||||
};
|
||||
use actix_web::cookie::time::Duration;
|
||||
use jwt_simple::Error;
|
||||
use std::collections::HashMap;
|
||||
|
||||
type SessionState = HashMap<String, String>;
|
||||
|
||||
/// Session store multiplexer.
|
||||
///
|
||||
/// Allows to easily support different store without having a lot of duplicate code
|
||||
pub enum MultiSessionStore {
|
||||
Cookie(CookieSessionStore),
|
||||
Redis(RedisSessionStore),
|
||||
}
|
||||
|
||||
impl SessionStore for MultiSessionStore {
|
||||
async fn load(&self, session_key: &SessionKey) -> Result<Option<SessionState>, LoadError> {
|
||||
match self {
|
||||
MultiSessionStore::Cookie(c) => c.load(session_key).await,
|
||||
MultiSessionStore::Redis(r) => r.load(session_key).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn save(
|
||||
&self,
|
||||
session_state: SessionState,
|
||||
ttl: &Duration,
|
||||
) -> Result<SessionKey, SaveError> {
|
||||
match self {
|
||||
MultiSessionStore::Cookie(c) => c.save(session_state, ttl).await,
|
||||
MultiSessionStore::Redis(r) => r.save(session_state, ttl).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn update(
|
||||
&self,
|
||||
session_key: SessionKey,
|
||||
session_state: SessionState,
|
||||
ttl: &Duration,
|
||||
) -> Result<SessionKey, UpdateError> {
|
||||
match self {
|
||||
MultiSessionStore::Cookie(c) => c.update(session_key, session_state, ttl).await,
|
||||
MultiSessionStore::Redis(r) => r.update(session_key, session_state, ttl).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_ttl(&self, session_key: &SessionKey, ttl: &Duration) -> Result<(), Error> {
|
||||
match self {
|
||||
MultiSessionStore::Cookie(c) => c.update_ttl(session_key, ttl).await,
|
||||
MultiSessionStore::Redis(r) => r.update_ttl(session_key, ttl).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete(&self, session_key: &SessionKey) -> Result<(), Error> {
|
||||
match self {
|
||||
MultiSessionStore::Cookie(c) => c.delete(session_key).await,
|
||||
MultiSessionStore::Redis(r) => r.delete(session_key).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod crypt_utils;
|
||||
pub mod err;
|
||||
pub mod string_utils;
|
||||
pub mod time;
|
||||
pub mod time_utils;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use chrono::DateTime;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Get the current time since epoch
|
||||
pub fn time() -> u64 {
|
||||
@@ -19,9 +19,27 @@ pub fn fmt_time(timestamp: u64) -> String {
|
||||
datetime.format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
/// Convert UNIX time to system time
|
||||
pub fn unix_to_system_time(time: u64) -> SystemTime {
|
||||
UNIX_EPOCH + Duration::from_secs(time)
|
||||
}
|
||||
|
||||
/// Format UNIX time to HTTP date
|
||||
pub fn unix_to_http_date(time: u64) -> String {
|
||||
httpdate::fmt_http_date(unix_to_system_time(time))
|
||||
}
|
||||
|
||||
/// Get build time in UNIX format
|
||||
pub fn build_time() -> u64 {
|
||||
let build_time = build_time::build_time_local!();
|
||||
let date =
|
||||
chrono::DateTime::parse_from_rfc3339(build_time).expect("Failed to parse compile date");
|
||||
date.timestamp() as u64
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::time::{fmt_time, time};
|
||||
use crate::utils::time_utils::{fmt_time, time};
|
||||
|
||||
#[test]
|
||||
fn test_time() {
|
||||
Reference in New Issue
Block a user