Skip to content

Commit

Permalink
[ABW-3969] Shield Setup Select Factors - utility functions (#297)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* bump version

* wip

* address feedback
  • Loading branch information
danvleju-rdx authored Dec 11, 2024
1 parent 217f10a commit a2b1692
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import SargonUniFFI

extension SecurityProblemKind: CaseIterable {
public static var allCases: [SecurityProblemKind] {
[.configurationBackup, .securityFactors]
[.securityShields, .securityFactors, .configurationBackup]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ struct ShieldTests {
#expect(shield.matrixOfFactors.confirmationRole.overrideFactors == [.sampleDevice])
#expect(shield.matrixOfFactors.confirmationRole.thresholdFactors == [])
}

@Test("selected factor sources for role status")
func selectedFactorSourcesForRoleStatus() {
let builder = SecurityShieldBuilder()
builder.addFactorSourceToPrimaryThreshold(factorSourceId: .samplePassword)
builder.addFactorSourceToRecoveryOverride(factorSourceId: .sampleLedger)

#expect(builder.selectedFactorSourcesForRoleStatus(role: .primary) == .invalid)
#expect(builder.selectedFactorSourcesForRoleStatus(role: .recovery) == .optimal)
#expect(builder.selectedFactorSourcesForRoleStatus(role: .confirmation) == .insufficient)
}
}

#if DEBUG
Expand Down
2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.81"
version = "1.1.82"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod security_shield_prerequisites_status;
mod security_structure_id;
mod security_structure_metadata;
mod security_structures;
mod selected_factor_sources_status;

pub use matrices::*;
pub use models::*;
Expand All @@ -16,3 +17,4 @@ pub use security_shield_prerequisites_status::*;
pub use security_structure_id::*;
pub use security_structure_metadata::*;
pub use security_structures::*;
pub use selected_factor_sources_status::*;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
sync::{Arc, RwLock},
};

use sargon::SecurityShieldBuilder as InternalSecurityShieldBuilder;
use sargon::SelectedFactorSourcesForRoleStatus as InternalSelectedFactorSourcesForRoleStatus;
use sargon::{IndexSet, MatrixBuilder};

use crate::prelude::*;
Expand Down Expand Up @@ -382,6 +384,41 @@ impl SecurityShieldBuilder {
self.get(|builder| builder.validate().map(|x| x.into()))
}

pub fn validate_role_in_isolation(
&self,
role: RoleKind,
) -> Option<SecurityShieldBuilderInvalidReason> {
self.get(|builder| {
builder
.validate_role_in_isolation(role.into_internal())
.map(|x| x.into())
})
}

pub fn selected_factor_sources_for_role_status(
&self,
role: RoleKind,
) -> SelectedFactorSourcesForRoleStatus {
self.get(|builder| {
builder
.selected_factor_sources_for_role_status(role.into_internal())
.into()
})
}

pub fn sorted_factor_sources_for_primary_threshold_selection(
&self,
factor_sources: Vec<FactorSource>,
) -> Vec<FactorSource> {
self.get(|builder| {
builder
.sorted_factor_sources_for_primary_threshold_selection(
factor_sources.clone().into_internal(),
)
.into_type()
})
}

pub fn build(
&self,
) -> Result<
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::prelude::*;
use sargon::SelectedFactorSourcesForRoleStatus as InternalSelectedFactorSourcesForRoleStatus;

/// Represents the status of selected factor sources for a specific role in the Security Shield building process.
/// Primarily used for UI logic representation in host applications.
#[derive(
Clone, Copy, Debug, PartialEq, Eq, InternalConversion, uniffi::Enum,
)]
pub enum SelectedFactorSourcesForRoleStatus {
/// The selected factor sources are optimal for the specified role
/// in the Security Shield building process.
Optimal,

/// The selected factor sources are suboptimal for the specified role
/// in the Security Shield building process.
///
/// Note: Typically used in hosts as a warning message.
Suboptimal,

/// The selected factor sources are insufficient for the specified role
/// in the Security Shield building process.
Insufficient,

/// The selected factor sources form an invalid combination for the specified role
/// in the Security Shield building process.
///
/// Example: A Password factor source cannot be used alone for the Primary role.
Invalid,
}
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.81"
version = "1.1.82"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,40 @@ impl MatrixBuilder {
)
}

fn validate_each_role_in_isolation(&self) -> MatrixBuilderMutateResult {
pub fn validate_primary_role_in_isolation(
&self,
) -> MatrixBuilderMutateResult {
self.primary_role
.validate()
.into_matrix_err(RoleKind::Primary)?;
Ok(())
}

pub fn validate_recovery_role_in_isolation(
&self,
) -> MatrixBuilderMutateResult {
self.recovery_role
.validate()
.into_matrix_err(RoleKind::Recovery)?;
Ok(())
}

pub fn validate_confirmation_role_in_isolation(
&self,
) -> MatrixBuilderMutateResult {
self.confirmation_role
.validate()
.into_matrix_err(RoleKind::Confirmation)?;
Ok(())
}

fn validate_each_role_in_isolation(&self) -> MatrixBuilderMutateResult {
self.validate_primary_role_in_isolation()?;
self.validate_recovery_role_in_isolation()?;
self.validate_confirmation_role_in_isolation()?;
Ok(())
}

pub fn validate(&self) -> MatrixBuilderMutateResult {
self.validate_each_role_in_isolation()?;
self.validate_combination()?;
Expand Down
2 changes: 2 additions & 0 deletions crates/sargon/src/profile/mfa/security_structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod security_shield_prerequisites_status;
mod security_structure_id;
mod security_structure_metadata;
mod security_structure_of_factors;
mod selected_factor_sources_status;

pub use has_role_kind::*;
pub use matrices::*;
Expand All @@ -17,3 +18,4 @@ pub use security_shield_prerequisites_status::*;
pub use security_structure_id::*;
pub use security_structure_metadata::*;
pub use security_structure_of_factors::*;
pub use selected_factor_sources_status::*;
Loading

0 comments on commit a2b1692

Please sign in to comment.