Load server config on react app

This commit is contained in:
2023-06-06 15:47:30 +02:00
parent ec98e728d8
commit 8f0a3e1f07
10 changed files with 170 additions and 38 deletions

View File

@ -19,6 +19,21 @@ dependencies = [
"tracing",
]
[[package]]
name = "actix-cors"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b340e9cfa5b08690aae90fb61beb44e9b06f44fe3d0f93781aaa58cfba86245e"
dependencies = [
"actix-utils",
"actix-web",
"derive_more",
"futures-util",
"log",
"once_cell",
"smallvec",
]
[[package]]
name = "actix-http"
version = "3.3.1"
@ -779,6 +794,7 @@ dependencies = [
name = "geneit_backend"
version = "0.1.0"
dependencies = [
"actix-cors",
"actix-remote-ip",
"actix-web",
"anyhow",

View File

@ -12,6 +12,7 @@ clap = { version = "4.3.0", features = ["derive", "env"] }
lazy_static = "1.4.0"
anyhow = "1.0.71"
actix-web = "4.3.1"
actix-cors = "0.6.4"
futures-util = "0.3.28"
diesel = { version = "2.0.4", features = ["postgres"] }
serde = { version = "1.0.163", features = ["derive"] }

View File

@ -81,19 +81,11 @@ pub struct AppConfig {
pub smtp_password: Option<String>,
/// Password reset URL
#[clap(
long,
env,
default_value = "http://localhost:3000/reset_password#TOKEN"
)]
#[clap(long, env, default_value = "APP_ORIGIN/reset_password#TOKEN")]
pub reset_password_url: String,
/// Delete account URL
#[clap(
long,
env,
default_value = "http://localhost:3000/delete_account#TOKEN"
)]
#[clap(long, env, default_value = "APP_ORIGIN/delete_account#TOKEN")]
pub delete_account_url: String,
/// URL where the OpenID configuration can be found
@ -121,8 +113,8 @@ pub struct AppConfig {
pub oidc_client_secret: String,
/// OpenID login redirect URL
#[arg(long, env, default_value = "http://localhost:3000/oidc_cb")]
pub oidc_redirect_url: String,
#[arg(long, env, default_value = "APP_ORIGIN/oidc_cb")]
oidc_redirect_url: String,
}
lazy_static::lazy_static! {
@ -159,12 +151,16 @@ impl AppConfig {
/// Get password reset URL
pub fn get_password_reset_url(&self, token: &str) -> String {
self.reset_password_url.replace("TOKEN", token)
self.reset_password_url
.replace("APP_ORIGIN", &self.website_origin)
.replace("TOKEN", token)
}
/// Get account delete URL
pub fn get_account_delete_url(&self, token: &str) -> String {
self.delete_account_url.replace("TOKEN", token)
self.delete_account_url
.replace("APP_ORIGIN", &self.website_origin)
.replace("TOKEN", token)
}
/// Get OpenID providers configuration
@ -181,6 +177,12 @@ impl AppConfig {
name: self.oidc_provider_name.as_str(),
}]
}
/// Get OIDC callback URL
pub fn oidc_redirect_url(&self) -> String {
self.oidc_redirect_url
.replace("APP_ORIGIN", &self.website_origin)
}
}
#[derive(Debug, Clone, serde::Serialize)]

View File

@ -1,3 +1,4 @@
use actix_cors::Cors;
use actix_remote_ip::RemoteIPConfig;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
@ -12,6 +13,14 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(
Cors::default()
.allowed_origin(&AppConfig::get().website_origin)
.allowed_methods(vec!["GET", "POST"])
.allowed_header("X-Auth-Token")
.supports_credentials()
.max_age(3600),
)
.wrap(Logger::default())
.app_data(web::Data::new(RemoteIPConfig {
proxy: AppConfig::get().proxy_ip.clone(),

View File

@ -96,7 +96,7 @@ pub async fn start_login(prov_id: &str, ip: IpAddr) -> anyhow::Result<String> {
Ok(prov.conf.gen_authorization_url(
prov.prov.client_id,
&state_key,
&AppConfig::get().oidc_redirect_url,
&AppConfig::get().oidc_redirect_url(),
))
}
@ -133,7 +133,7 @@ pub async fn finish_login(
prov.prov.client_id,
prov.prov.client_secret,
code,
&AppConfig::get().oidc_redirect_url,
&AppConfig::get().oidc_redirect_url(),
)
.await
.map_err(|e| OpenIDServiceError::QueryTokenEndpoint(e.to_string()))?;