Skip to content

Commit

Permalink
feat: add PlainLocalpart feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki-f committed Nov 4, 2024
1 parent 123b271 commit 94261a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ pub enum FeatureFlag {
DryRun,
/// If only deactivated users should be synced
DeactivateOnly,
/// Use plain localpart
PlainLocalpart,
}

#[derive(Debug, Clone, Deserialize, PartialEq, Default)]
Expand Down Expand Up @@ -356,7 +358,10 @@ mod tests {
let file_path = create_config_file(tempdir.path());

let env_var_name = format!("{ENV_VAR_CONFIG_PREFIX}__FEATURE_FLAGS");
env::set_var(&env_var_name, "sso_login verify_email verify_phone dry_run deactivate_only");
env::set_var(
&env_var_name,
"sso_login verify_email verify_phone dry_run deactivate_only plain_localpart",
);

let loaded_config =
Config::new(file_path.as_path()).expect("Failed to create config object");
Expand All @@ -367,6 +372,7 @@ mod tests {
sample_config.feature_flags.push(FeatureFlag::VerifyPhone);
sample_config.feature_flags.push(FeatureFlag::DryRun);
sample_config.feature_flags.push(FeatureFlag::DeactivateOnly);
sample_config.feature_flags.push(FeatureFlag::PlainLocalpart);

env::remove_var(env_var_name);

Expand Down
28 changes: 22 additions & 6 deletions src/zitadel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,27 @@ impl Zitadel {
return Ok(());
}

let id = match &user.user_data.external_user_id {
StringOrBytes::String(value) => value.as_bytes(),
StringOrBytes::Bytes(value) => value,
};

let uuid;
let localpart = if self.feature_flags.contains(&FeatureFlag::PlainLocalpart) {
match &user.user_data.external_user_id {
StringOrBytes::String(value) => value,
StringOrBytes::Bytes(_) => {
bail!(
"Unsupported binary external ID for user using plain localparts: {:?}",
user
);
}
}
} else {
uuid = Uuid::new_v5(&FAMEDLY_NAMESPACE, id).to_string();
&uuid
};

let new_user_id = self
.zitadel_client
.create_human_user(&self.zitadel_config.organization_id, user.clone().into())
Expand All @@ -422,17 +443,12 @@ impl Zitadel {
)
.await?;

let id = match &user.user_data.external_user_id {
StringOrBytes::String(value) => value.as_bytes(),
StringOrBytes::Bytes(value) => value,
};

self.zitadel_client
.set_user_metadata(
Some(&self.zitadel_config.organization_id),
new_user_id.clone(),
"localpart".to_owned(),
&Uuid::new_v5(&FAMEDLY_NAMESPACE, id).to_string(),
localpart,
)
.await?;

Expand Down

0 comments on commit 94261a7

Please sign in to comment.