use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::ProviderKind;
use crate::error::{Validate, ValidationError};
use crate::external_id::ExternalId;
use crate::id::Id;
use crate::time::Timestamp;
pub const FEATURE_PRINCIPAL_RELINK: &str = "principal_relink";
pub fn is_relink_target(e: &ExternalId) -> bool {
matches!(
e,
ExternalId::K2Principal(_) | ExternalId::K2Owner | ExternalId::K2Connect(_)
)
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PrincipalRelinkRequest {
pub from: ExternalId,
pub to: ExternalId,
}
impl Validate for PrincipalRelinkRequest {
fn validate(&self) -> Result<(), ValidationError> {
if !matches!(self.from, ExternalId::Local(_)) {
return Err(ValidationError::invalid(
"from",
"from must be a local user: local:<handle>",
));
}
if !is_relink_target(&self.to) {
return Err(ValidationError::invalid(
"to",
"to must be a K2 person: k2:principal:<uuid>, k2:owner or k2:connect:<uuid>",
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct PrincipalRelinkResponse {
pub member_id: Id,
pub from: ExternalId,
pub to: ExternalId,
pub changed: bool,
pub sessions_revoked: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct IdentitySwitchRequest {
pub to: ProviderKind,
pub confirm: bool,
}
impl Validate for IdentitySwitchRequest {
fn validate(&self) -> Result<(), ValidationError> {
if self.to != ProviderKind::K2 {
return Err(ValidationError::invalid(
"to",
"an install switches only from standalone to k2",
));
}
if !self.confirm {
return Err(ValidationError::invalid(
"confirm",
"the switch cannot be undone; send confirm: true",
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct IdentitySwitchResponse {
pub mode: ProviderKind,
pub switched_at: Timestamp,
pub restart_required: bool,
pub unlinked_local_members: u64,
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
use crate::addr::Handle;
#[test]
fn features_sorted_unique_and_listed() {
let mut v = super::super::FEATURES.to_vec();
v.sort_unstable();
v.dedup();
assert_eq!(v, super::super::FEATURES);
assert!(super::super::FEATURES.contains(&FEATURE_PRINCIPAL_RELINK));
}
#[test]
fn relink_validation() {
let local = ExternalId::Local(Handle::new("bob").expect("handle"));
let k2 = ExternalId::K2Principal("8f14e45f-ceea-4e7a-9c3b-0123456789ab".into());
let ok = PrincipalRelinkRequest {
from: local.clone(),
to: k2.clone(),
};
assert!(ok.validate().is_ok());
let bad_from = PrincipalRelinkRequest {
from: k2.clone(),
to: k2.clone(),
};
assert_eq!(bad_from.validate().map_err(|e| e.field), Err("from".into()));
let bad_to = PrincipalRelinkRequest {
from: local.clone(),
to: ExternalId::K2Workspace("8f14e45f-ceea-4e7a-9c3b-0123456789ab".into()),
};
assert_eq!(bad_to.validate().map_err(|e| e.field), Err("to".into()));
let local_to = PrincipalRelinkRequest {
from: local.clone(),
to: local,
};
assert_eq!(local_to.validate().map_err(|e| e.field), Err("to".into()));
}
#[test]
fn switch_validation() {
let ok = IdentitySwitchRequest {
to: ProviderKind::K2,
confirm: true,
};
assert!(ok.validate().is_ok());
let back = IdentitySwitchRequest {
to: ProviderKind::Standalone,
confirm: true,
};
assert_eq!(back.validate().map_err(|e| e.field), Err("to".into()));
let unconfirmed = IdentitySwitchRequest {
to: ProviderKind::K2,
confirm: false,
};
assert_eq!(
unconfirmed.validate().map_err(|e| e.field),
Err("confirm".into())
);
}
}