use crate::bootstrapped_identities_store::PreTrustedIdentities;
use crate::DefaultAddress;
use ockam::identity::utils::now;
use ockam::identity::{AttributesEntry, Identifier, TRUST_CONTEXT_ID};
use ockam_core::compat::collections::HashMap;
use ockam_core::compat::fmt;
use ockam_core::compat::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Configuration {
pub identifier: Identifier,
pub storage_path: PathBuf,
pub vault_path: PathBuf,
pub project_identifier: String,
pub tcp_listener_address: String,
pub secure_channel_listener_name: Option<String>,
pub authenticator_name: Option<String>,
pub trusted_identities: PreTrustedIdentities,
pub no_direct_authentication: bool,
pub no_token_enrollment: bool,
pub okta: Option<OktaConfiguration>,
}
impl Configuration {
pub(crate) fn identifier(&self) -> Identifier {
self.identifier.clone()
}
pub(crate) fn project_identifier(&self) -> String {
self.project_identifier.clone()
}
pub(crate) fn tcp_listener_address(&self) -> String {
self.tcp_listener_address.clone()
}
pub(crate) fn secure_channel_listener_name(&self) -> String {
self.secure_channel_listener_name
.clone()
.unwrap_or(DefaultAddress::SECURE_CHANNEL_LISTENER.into())
}
pub(crate) fn authenticator_name(&self) -> String {
self.authenticator_name
.clone()
.unwrap_or(DefaultAddress::DIRECT_AUTHENTICATOR.to_string())
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct OktaConfiguration {
pub address: String,
pub tenant_base_url: String,
pub certificate: String,
pub attributes: Vec<String>,
}
impl OktaConfiguration {
pub(crate) fn tenant_base_url(&self) -> &str {
self.tenant_base_url.as_str()
}
pub(crate) fn certificate(&self) -> &str {
self.certificate.as_str()
}
pub(crate) fn attributes(&self) -> Vec<String> {
self.attributes.clone()
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct TrustedIdentity {
identifier: Identifier,
attributes: HashMap<String, String>,
}
impl Display for TrustedIdentity {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(
serde_json::to_string(self)
.map_err(|_| fmt::Error)?
.as_str(),
)
}
}
impl TrustedIdentity {
pub fn new(identifier: &Identifier, attributes: &HashMap<String, String>) -> TrustedIdentity {
TrustedIdentity {
identifier: identifier.clone(),
attributes: attributes.clone(),
}
}
pub fn identifier(&self) -> Identifier {
self.identifier.clone()
}
pub fn attributes_entry(
&self,
project_identifier: String,
authority_identifier: &Identifier,
) -> AttributesEntry {
let mut map: BTreeMap<Vec<u8>, Vec<u8>> = BTreeMap::new();
for (name, value) in self.attributes.clone().iter() {
map.insert(name.as_bytes().to_vec(), value.as_bytes().to_vec());
}
map.insert(
TRUST_CONTEXT_ID.to_vec(),
project_identifier.as_bytes().to_vec(),
);
AttributesEntry::new(
map,
now().unwrap(),
None,
Some(authority_identifier.clone()),
)
}
}