ockam_identity/secure_channels/
secure_channels_builder.rs

1use ockam_core::compat::sync::Arc;
2#[cfg(feature = "storage")]
3use ockam_core::Result;
4use ockam_vault::storage::SecretsRepository;
5
6use crate::identities::ChangeHistoryRepository;
7use crate::purpose_keys::storage::PurposeKeysRepository;
8use crate::secure_channel::SecureChannelRegistry;
9use crate::secure_channels::SecureChannels;
10use crate::{
11    CredentialRepository, IdentitiesBuilder, IdentityAttributesRepository, SecureChannelRepository,
12    Vault,
13};
14
15/// This struct supports all the services related to secure channels
16#[derive(Clone)]
17pub struct SecureChannelsBuilder {
18    // FIXME: This is very strange dependency
19    pub(crate) identities_builder: IdentitiesBuilder,
20    pub(crate) registry: SecureChannelRegistry,
21    pub(crate) secure_channel_repository: Arc<dyn SecureChannelRepository>,
22}
23
24/// Create default, in-memory, secure channels (mostly for examples and testing)
25#[cfg(feature = "storage")]
26pub async fn secure_channels() -> Result<Arc<SecureChannels>> {
27    Ok(SecureChannels::builder().await?.build())
28}
29
30impl SecureChannelsBuilder {
31    /// With Software Vault with given secrets repository
32    pub fn with_secrets_repository(mut self, repository: Arc<dyn SecretsRepository>) -> Self {
33        self.identities_builder = self.identities_builder.with_secrets_repository(repository);
34        self
35    }
36
37    /// Set [`Vault`]
38    pub fn with_vault(mut self, vault: Vault) -> Self {
39        self.identities_builder = self.identities_builder.with_vault(vault);
40        self
41    }
42
43    /// Set a specific identities repository
44    pub fn with_change_history_repository(
45        mut self,
46        repository: Arc<dyn ChangeHistoryRepository>,
47    ) -> Self {
48        self.identities_builder = self
49            .identities_builder
50            .with_change_history_repository(repository);
51        self
52    }
53
54    /// Set a specific identity attributes repository
55    pub fn with_identity_attributes_repository(
56        mut self,
57        repository: Arc<dyn IdentityAttributesRepository>,
58    ) -> Self {
59        self.identities_builder = self
60            .identities_builder
61            .with_identity_attributes_repository(repository);
62        self
63    }
64
65    /// Set a specific purpose keys repository
66    pub fn with_purpose_keys_repository(
67        mut self,
68        repository: Arc<dyn PurposeKeysRepository>,
69    ) -> Self {
70        self.identities_builder = self
71            .identities_builder
72            .with_purpose_keys_repository(repository);
73        self
74    }
75
76    /// Set a specific cached credentials repository
77    pub fn with_cached_credential_repository(
78        mut self,
79        repository: Arc<dyn CredentialRepository>,
80    ) -> Self {
81        self.identities_builder = self
82            .identities_builder
83            .with_cached_credential_repository(repository);
84        self
85    }
86
87    /// Set a specific secure channels repository
88    pub fn with_secure_channel_repository(
89        mut self,
90        repository: Arc<dyn SecureChannelRepository>,
91    ) -> Self {
92        self.secure_channel_repository = repository;
93        self
94    }
95
96    /// Set a specific channel registry
97    pub fn with_secure_channels_registry(mut self, registry: SecureChannelRegistry) -> Self {
98        self.registry = registry;
99        self
100    }
101
102    /// Return the vault used by this builder
103    /// Build secure channels
104    pub fn build(self) -> Arc<SecureChannels> {
105        let identities = self.identities_builder.build();
106        Arc::new(SecureChannels::new(
107            identities,
108            self.registry,
109            self.secure_channel_repository,
110        ))
111    }
112}