Skip to content

Commit

Permalink
create a default config is not exist and disable realm key for auth p…
Browse files Browse the repository at this point in the history
…rovider
  • Loading branch information
thebino committed Oct 25, 2023
1 parent 1434cd4 commit d910473
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 45 deletions.
38 changes: 28 additions & 10 deletions crates/common/src/config/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,19 +40,33 @@ pub struct Configuration {
impl Configuration {
pub fn new(path: &str) -> Option<Self> {
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![],
}
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/config/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, serde_json::Value>>,
Expand Down
70 changes: 37 additions & 33 deletions crates/oauth_authorization_server/src/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,26 +47,32 @@ impl Realm {
domain: &str,
scheme: &str,
clients: Vec<Client>,
realm_keys_base_path: P,
_realm_keys_base_path: P,
) -> Result<Self, Error> {
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(),
Expand Down Expand Up @@ -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(),
]),
})
}
Expand Down

0 comments on commit d910473

Please sign in to comment.