BasicOIDC/src/data/client.rs

70 lines
2.0 KiB
Rust
Raw Normal View History

2022-04-06 15:18:06 +00:00
use crate::data::entity_manager::EntityManager;
use crate::utils::string_utils::apply_env_vars;
2022-04-06 15:18:06 +00:00
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
pub struct ClientID(pub String);
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Client {
2023-04-15 08:31:11 +00:00
/// The ID of the client
2022-04-06 15:18:06 +00:00
pub id: ClientID,
2023-04-15 08:31:11 +00:00
/// The human-readable name of the client
2022-04-06 15:18:06 +00:00
pub name: String,
2023-04-15 08:31:11 +00:00
/// A short description of the service provided by the client
2022-04-06 15:18:06 +00:00
pub description: String,
2023-04-15 08:31:11 +00:00
/// The secret used by the client to retrieve authenticated users information
2022-04-09 09:30:23 +00:00
pub secret: String,
2023-04-15 08:31:11 +00:00
/// The URI where the users should be redirected once authenticated
2022-04-09 09:30:23 +00:00
pub redirect_uri: String,
2023-04-15 08:31:11 +00:00
/// Specify if the client must be allowed by default for new account
#[serde(default = "bool::default")]
pub default: bool,
2023-04-15 08:39:22 +00:00
/// Specify whether a client is granted to all users
#[serde(default = "bool::default")]
pub granted_to_all_users: bool,
2022-04-06 15:18:06 +00:00
}
impl PartialEq for Client {
fn eq(&self, other: &Self) -> bool {
self.id.eq(&other.id)
}
}
impl Eq for Client {}
pub type ClientManager = EntityManager<Client>;
impl EntityManager<Client> {
pub fn find_by_id(&self, u: &ClientID) -> Option<Client> {
for entry in self.iter() {
if entry.id.eq(u) {
return Some(entry.clone());
}
}
None
}
2023-04-15 08:31:11 +00:00
/// Get the list of default clients.
///
/// i.e. the clients that are granted to new accounts by default
pub fn get_default_clients(&self) -> Vec<&Client> {
self.iter().filter(|u| u.default).collect()
}
pub fn apply_environment_variables(&mut self) {
for c in self.iter_mut() {
c.id = ClientID(apply_env_vars(&c.id.0));
c.name = apply_env_vars(&c.name);
c.description = apply_env_vars(&c.description);
c.secret = apply_env_vars(&c.secret);
c.redirect_uri = apply_env_vars(&c.redirect_uri);
}
}
2022-11-11 11:26:02 +00:00
}