use crate::delegation::types::DelegationTargets;
use crate::openid::types::provider::OpenIdDelegationProvider;
use crate::state::get_config;
use crate::strategies::AuthHeapStrategy;
use junobuild_shared::ic::api::id;
pub fn build_targets(
provider: &OpenIdDelegationProvider,
auth_heap: &impl AuthHeapStrategy,
) -> Option<DelegationTargets> {
get_config(auth_heap)
.as_ref()
.and_then(|config| config.openid.as_ref())
.and_then(|openid| openid.providers.get(provider))
.and_then(|provider| provider.delegation.as_ref())
.map_or(Some(Vec::from([id()])), |delegation| {
match &delegation.targets {
None => None,
Some(targets) => {
if targets.is_empty() {
Some(vec![id()])
} else {
Some(targets.clone())
}
}
}
})
}
pub fn targets_to_bytes(targets: &Option<DelegationTargets>) -> Option<Vec<Vec<u8>>> {
targets.as_ref().map(|target| {
target
.iter()
.map(|principal| principal.as_slice().to_vec())
.collect()
})
}