diff --git a/crates/common/src/config/configuration.rs b/crates/common/src/config/configuration.rs index 45d5dbb..c5e3f17 100644 --- a/crates/common/src/config/configuration.rs +++ b/crates/common/src/config/configuration.rs @@ -18,12 +18,16 @@ //! This defines the app configuration use std::{fmt, fs}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use tracing::info; -use super::{client::OAuthClientConfig, database_config::DatabaseConfig, plugin::Plugin}; +use super::{ + client::OAuthClientConfig, + database_config::{DatabaseConfig, DatabaseDriver}, + plugin::Plugin, +}; -#[derive(Debug, PartialEq, Deserialize, Clone)] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Configuration { pub internal_url: String, pub external_url: String, @@ -36,19 +40,33 @@ pub struct Configuration { impl Configuration { pub fn new(path: &str) -> Option { info!("Load configuration file {}", path); - let data = fs::read_to_string(path).expect("Unable to read configuration file!"); - let config: Configuration = - serde_json::from_str(&data).expect("Configuration file could not be parsed as JSON!"); + + let read_file_result = fs::read_to_string(path); + + let config = match read_file_result { + Ok(data) => serde_json::from_str(&data) + .expect("Configuration file could not be parsed as JSON!"), + Err(_) => { + let default_config = Configuration::empty(); + + fs::write(path, serde_json::to_string_pretty(&default_config).unwrap()) + .expect("Could not write default Configuration to file!"); + + default_config + } + }; Some(config) } - /// Use this for tests pub fn empty() -> Self { Configuration { - internal_url: "".into(), - external_url: "".into(), - database: None, + internal_url: "127.0.0.1".into(), + external_url: "127.0.0.1".into(), + database: Some(DatabaseConfig { + driver: DatabaseDriver::SQLite, + url: "sqlite://data/core.sqlite3".into(), + }), clients: vec![], plugins: vec![], } diff --git a/crates/common/src/config/plugin.rs b/crates/common/src/config/plugin.rs index 3ad93ed..e8d8a39 100644 --- a/crates/common/src/config/plugin.rs +++ b/crates/common/src/config/plugin.rs @@ -18,10 +18,10 @@ //! This describes a plugin with a key-value pair configuration use std::fmt; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_json::Map; -#[derive(Debug, PartialEq, Deserialize, Clone)] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Plugin { pub name: String, pub config: Option>, diff --git a/crates/oauth_authorization_server/src/realm.rs b/crates/oauth_authorization_server/src/realm.rs index cbf619f..1d6576e 100644 --- a/crates/oauth_authorization_server/src/realm.rs +++ b/crates/oauth_authorization_server/src/realm.rs @@ -17,15 +17,13 @@ use openidconnect::core::{ CoreClaimName, CoreJsonWebKeySet, CoreJwsSigningAlgorithm, CoreProviderMetadata, - CoreResponseType, CoreRsaPrivateSigningKey, CoreSubjectIdentifierType, + CoreResponseType, CoreSubjectIdentifierType, }; use openidconnect::{ - AuthUrl, EmptyAdditionalProviderMetadata, IssuerUrl, JsonWebKeyId, JsonWebKeySetUrl, - PrivateSigningKey, ResponseTypes, TokenUrl, UserInfoUrl, + AuthUrl, EmptyAdditionalProviderMetadata, IssuerUrl, JsonWebKeySetUrl, ResponseTypes, TokenUrl, + UserInfoUrl, }; -use std::fs::File; -use std::io::Read; use std::path::Path; use crate::client::Client; @@ -49,26 +47,32 @@ impl Realm { domain: &str, scheme: &str, clients: Vec, - realm_keys_base_path: P, + _realm_keys_base_path: P, ) -> Result { - let mut realm_key_file = File::open( - realm_keys_base_path - .as_ref() - .join(name) - .with_extension("pem"), - ) - .unwrap_or_else(|_| { - panic!( - "key ({}) not found in directory ({})!", - name, - realm_keys_base_path.as_ref().display() - ) - }); - let mut realm_key_str = String::new(); - realm_key_file - .read_to_string(&mut realm_key_str) - .map_err(|_| Error::CouldNotOpenRealmKey(name.to_owned()))?; - + /* + let mut realm_key_file = File::open( + realm_keys_base_path + .as_ref() + .join(name) + .with_extension("pem"), + ) + .unwrap_or_else(|_| { + error!( + "key ({}) not found in directory ({})!", + name, + realm_keys_base_path.as_ref().display() + ); + // TODO: create default key file + let mut default_key_file = + File::create(format!("{}/{}.pem", realm_keys_base_path, name)).unwrap(); + let default_key = CoreRsaPrivateSigningKey::into() //CoreJsonWebKey::new_rsa(); + default_key_file.write(default_key + }); + let mut realm_key_str = String::new(); + realm_key_file + .read_to_string(&mut realm_key_str) + .map_err(|_| Error::CouldNotOpenRealmKey(name.to_owned()))?; + */ Ok(Self { name: name.to_owned(), domain: domain.to_owned(), @@ -140,15 +144,15 @@ impl Realm { CoreClaimName::new("locale".to_string()), ])), jwks: CoreJsonWebKeySet::new(vec![ - // RSA keys may also be constructed directly using CoreJsonWebKey::new_rsa(). Providers - // aiming to support other key types may provide their own implementation of the - // JsonWebKey trait or submit a PR to add the desired support to this crate. - CoreRsaPrivateSigningKey::from_pem( - &realm_key_str, - Some(JsonWebKeyId::new(format!("{}_key", name))), - ) - .expect("Invalid RSA private key") - .as_verification_key(), + // RSA keys may also be constructed directly using CoreJsonWebKey::new_rsa(). Providers + // aiming to support other key types may provide their own implementation of the + // JsonWebKey trait or submit a PR to add the desired support to this crate. + // CoreRsaPrivateSigningKey::from_pem( + // &realm_key_str, + // Some(JsonWebKeyId::new(format!("{}_key", name))), + // ) + // .expect("Invalid RSA private key") + // .as_verification_key(), ]), }) }