BasicOIDC/src/main.rs

99 lines
3.2 KiB
Rust
Raw Normal View History

2022-04-02 13:44:09 +00:00
use actix::Actor;
use actix_identity::{CookieIdentityPolicy, IdentityService};
2022-03-30 08:29:10 +00:00
use actix_web::{App, get, HttpServer, web};
use actix_web::cookie::SameSite;
use actix_web::cookie::time::Duration;
2022-03-30 08:29:10 +00:00
use actix_web::middleware::Logger;
2022-03-30 08:14:39 +00:00
use clap::Parser;
2022-04-02 13:44:09 +00:00
use basic_oidc::actors::users_actor::UsersActor;
use basic_oidc::constants::{DEFAULT_ADMIN_PASSWORD, DEFAULT_ADMIN_USERNAME, MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME};
2022-03-30 08:14:39 +00:00
use basic_oidc::controllers::assets_controller::assets_route;
2022-04-01 17:05:40 +00:00
use basic_oidc::controllers::login_controller::{login_route, logout_route};
use basic_oidc::data::app_config::AppConfig;
use basic_oidc::data::entity_manager::EntityManager;
use basic_oidc::data::user::{hash_password, User};
2022-04-02 13:44:09 +00:00
use basic_oidc::middlewares::auth_middleware::AuthMiddleware;
#[get("/health")]
2022-03-30 08:14:39 +00:00
async fn health() -> &'static str {
"Running"
2022-03-29 16:19:23 +00:00
}
2022-03-30 08:14:39 +00:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2022-03-30 14:58:00 +00:00
let mut config: AppConfig = AppConfig::parse();
// In debug mode only, use dummy token
if cfg!(debug_assertions) && config.token_key.is_empty() {
config.token_key = String::from_utf8_lossy(&[32; 32]).to_string();
}
if !config.storage_path().exists() {
2022-03-30 06:42:18 +00:00
log::error!(
"Specified storage path {:?} does not exists!",
config.storage_path()
);
panic!()
}
let mut users = EntityManager::<User>::open_or_create(config.users_file())
.expect("Failed to load users list!");
// Create initial user if required
2022-03-30 06:42:18 +00:00
if users.is_empty() {
log::info!("Create default {} user", DEFAULT_ADMIN_USERNAME);
2022-03-30 06:42:18 +00:00
let default_admin = User {
username: DEFAULT_ADMIN_USERNAME.to_string(),
password: hash_password(DEFAULT_ADMIN_PASSWORD).unwrap(),
need_reset_password: true,
authorized_services: None,
admin: true,
..Default::default()
};
users
.insert(default_admin)
.expect("Failed to create initial user!");
}
2022-03-30 09:40:03 +00:00
let users_actor = UsersActor::new(users).start();
2022-03-30 08:14:39 +00:00
log::info!("Server will listen on {}", config.listen_address);
2022-03-30 09:40:03 +00:00
HttpServer::new(move || {
2022-03-30 14:58:00 +00:00
let policy = CookieIdentityPolicy::new(config.token_key.as_bytes())
2022-04-02 15:03:51 +00:00
.name(SESSION_COOKIE_NAME)
.secure(config.secure_auth_cookie)
.visit_deadline(Duration::seconds(MAX_INACTIVITY_DURATION))
.login_deadline(Duration::seconds(MAX_SESSION_DURATION))
.same_site(SameSite::Strict);
2022-03-30 14:58:00 +00:00
2022-03-30 08:14:39 +00:00
App::new()
2022-03-30 09:40:03 +00:00
.app_data(web::Data::new(users_actor.clone()))
2022-03-30 08:29:10 +00:00
.wrap(Logger::default())
2022-04-02 13:44:09 +00:00
.wrap(AuthMiddleware {})
2022-04-02 15:03:51 +00:00
.wrap(IdentityService::new(policy))
2022-03-30 09:40:03 +00:00
// /health route
2022-03-30 08:14:39 +00:00
.service(health)
2022-03-30 09:40:03 +00:00
// Assets serving
2022-03-30 08:14:39 +00:00
.route("/assets/{path:.*}", web::get().to(assets_route))
2022-03-30 09:40:03 +00:00
// Login page
2022-03-30 08:29:10 +00:00
.route("/login", web::get().to(login_route))
2022-03-30 09:40:03 +00:00
.route("/login", web::post().to(login_route))
2022-04-01 17:05:40 +00:00
// Logout page
.route("/logout", web::get().to(logout_route))
2022-03-30 08:14:39 +00:00
})
.bind(config.listen_address)?
.run()
.await
2022-03-30 06:42:18 +00:00
}