// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // Copyright by contributors to this project. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
//! Definitions to build an [`ExternalClient`]. //! //! See [`ExternalClientBuilder`].
/// Base client configuration type when instantiating `ExternalClientBuilder` pubtype ExternalBaseConfig = Config<Missing, DefaultMlsRules, Missing>;
/// Builder for [`ExternalClient`] /// /// This is returned by [`ExternalClient::builder`] and allows to tweak settings the /// `ExternalClient` will use. At a minimum, the builder must be told the [`CryptoProvider`] /// and [`IdentityProvider`] to use. Other settings have default values. This /// means that the following methods must be called before [`ExternalClientBuilder::build`]: /// /// - To specify the [`CryptoProvider`]: [`ExternalClientBuilder::crypto_provider`] /// - To specify the [`IdentityProvider`]: [`ExternalClientBuilder::identity_provider`] /// /// # Example /// /// ``` /// use mls_rs::{ /// external_client::ExternalClient, /// identity::basic::BasicIdentityProvider, /// }; /// /// use mls_rs_crypto_openssl::OpensslCryptoProvider; /// /// let _client = ExternalClient::builder() /// .crypto_provider(OpensslCryptoProvider::default()) /// .identity_provider(BasicIdentityProvider::new()) /// .build(); /// ``` /// /// # Spelling out an `ExternalClient` type /// /// There are two main ways to spell out an `ExternalClient` type if needed (e.g. function return type). /// /// The first option uses `impl MlsConfig`: /// ``` /// use mls_rs::{ /// external_client::{ExternalClient, builder::MlsConfig}, /// identity::basic::BasicIdentityProvider, /// }; /// /// use mls_rs_crypto_openssl::OpensslCryptoProvider; /// /// fn make_client() -> ExternalClient<impl MlsConfig> { /// ExternalClient::builder() /// .crypto_provider(OpensslCryptoProvider::default()) /// .identity_provider(BasicIdentityProvider::new()) /// .build() /// } ///``` /// /// The second option is more verbose and consists in writing the full `ExternalClient` type: /// ``` /// use mls_rs::{ /// external_client::{ExternalClient, builder::{ExternalBaseConfig, WithIdentityProvider, WithCryptoProvider}}, /// identity::basic::BasicIdentityProvider, /// }; /// /// use mls_rs_crypto_openssl::OpensslCryptoProvider; /// /// type MlsClient = ExternalClient<WithIdentityProvider< /// BasicIdentityProvider, /// WithCryptoProvider<OpensslCryptoProvider, ExternalBaseConfig>, /// >>; /// /// fn make_client_2() -> MlsClient { /// ExternalClient::builder() /// .crypto_provider(OpensslCryptoProvider::new()) /// .identity_provider(BasicIdentityProvider::new()) /// .build() /// } /// /// ``` #[derive(Debug)] pubstruct ExternalClientBuilder<C>(C);
impl<C: IntoConfig> ExternalClientBuilder<C> { /// Add an extension type to the list of extension types supported by the client. pubfn extension_type( self,
type_: ExtensionType,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { self.extension_types(Some(type_))
}
/// Add multiple extension types to the list of extension types supported by the client. pubfn extension_types<I>(self, types: I) -> ExternalClientBuilder<IntoConfigOutput<C>> where
I: IntoIterator<Item = ExtensionType>,
{ letmut c = self.0.into_config();
c.0.settings.extension_types.extend(types);
ExternalClientBuilder(c)
}
/// Add a custom proposal type to the list of proposals types supported by the client. pubfn custom_proposal_type( self,
type_: ProposalType,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { self.custom_proposal_types(Some(type_))
}
/// Add multiple custom proposal types to the list of proposal types supported by the client. pubfn custom_proposal_types<I>(self, types: I) -> ExternalClientBuilder<IntoConfigOutput<C>> where
I: IntoIterator<Item = ProposalType>,
{ letmut c = self.0.into_config();
c.0.settings.custom_proposal_types.extend(types);
ExternalClientBuilder(c)
}
/// Add a protocol version to the list of protocol versions supported by the client. /// /// If no protocol version is explicitly added, the client will support all protocol versions /// supported by this crate. pubfn protocol_version( self,
version: ProtocolVersion,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { self.protocol_versions(Some(version))
}
/// Add multiple protocol versions to the list of protocol versions supported by the client. /// /// If no protocol version is explicitly added, the client will support all protocol versions /// supported by this crate. pubfn protocol_versions<I>(self, versions: I) -> ExternalClientBuilder<IntoConfigOutput<C>> where
I: IntoIterator<Item = ProtocolVersion>,
{ letmut c = self.0.into_config();
c.0.settings.protocol_versions.extend(versions);
ExternalClientBuilder(c)
}
/// Add an external signing key to be used by the client. pubfn external_signing_key( self,
id: Vec<u8>,
key: SignaturePublicKey,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { letmut c = self.0.into_config();
c.0.settings.external_signing_keys.insert(id, key);
ExternalClientBuilder(c)
}
/// Specify the number of epochs before the current one to keep. /// /// By default, all epochs are kept. pubfn max_epoch_jitter(self, max_jitter: u64) -> ExternalClientBuilder<IntoConfigOutput<C>> { letmut c = self.0.into_config();
c.0.settings.max_epoch_jitter = Some(max_jitter);
ExternalClientBuilder(c)
}
/// Specify whether processed proposals should be cached by the external group. In case they /// are not cached by the group, they should be cached externally and inserted using /// `ExternalGroup::insert_proposal` before processing the next commit. pubfn cache_proposals( self,
cache_proposals: bool,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { letmut c = self.0.into_config();
c.0.settings.cache_proposals = cache_proposals;
ExternalClientBuilder(c)
}
/// Set the identity validator to be used by the client. pubfn identity_provider<I>( self,
identity_provider: I,
) -> ExternalClientBuilder<WithIdentityProvider<I, C>> where
I: IdentityProvider,
{ let Config(c) = self.0.into_config();
ExternalClientBuilder(Config(ConfigInner {
settings: c.settings,
identity_provider,
mls_rules: c.mls_rules,
crypto_provider: c.crypto_provider,
signing_data: c.signing_data,
}))
}
/// Set the crypto provider to be used by the client. /// // TODO add a comment once we have a default provider pubfn crypto_provider<Cp>( self,
crypto_provider: Cp,
) -> ExternalClientBuilder<WithCryptoProvider<Cp, C>> where
Cp: CryptoProvider,
{ let Config(c) = self.0.into_config();
ExternalClientBuilder(Config(ConfigInner {
settings: c.settings,
identity_provider: c.identity_provider,
mls_rules: c.mls_rules,
crypto_provider,
signing_data: c.signing_data,
}))
}
/// Set the user-defined proposal rules to be used by the client. /// /// User-defined rules are used when sending and receiving commits before /// enforcing general MLS protocol rules. If the rule set returns an error when /// receiving a commit, the entire commit is considered invalid. If the /// rule set would return an error when sending a commit, individual proposals /// may be filtered out to compensate. pubfn mls_rules<Pr>(self, mls_rules: Pr) -> ExternalClientBuilder<WithMlsRules<Pr, C>> where
Pr: MlsRules,
{ let Config(c) = self.0.into_config();
ExternalClientBuilder(Config(ConfigInner {
settings: c.settings,
identity_provider: c.identity_provider,
mls_rules,
crypto_provider: c.crypto_provider,
signing_data: c.signing_data,
}))
}
/// Set the signature secret key used by the client to send external proposals. pubfn signer( self,
signer: SignatureSecretKey,
signing_identity: SigningIdentity,
) -> ExternalClientBuilder<IntoConfigOutput<C>> { letmut c = self.0.into_config();
c.0.signing_data = Some((signer, signing_identity));
ExternalClientBuilder(c)
}
}
if c.0.settings.protocol_versions.is_empty() {
c.0.settings.protocol_versions = ProtocolVersion::all().collect();
}
c
}
/// Build an external client. /// /// See [`ExternalClientBuilder`] documentation if the return type of this function needs to be /// spelled out. pubfn build(self) -> ExternalClient<IntoConfigOutput<C>> { letmut c = self.build_config(); let signing_data = c.0.signing_data.take();
ExternalClient::new(c, signing_data)
}
}
/// Marker type for required `ExternalClientBuilder` services that have not been specified yet. #[derive(Debug)] pubstruct Missing;
/// Change the identity validator used by a client configuration. /// /// See [`ExternalClientBuilder::identity_provider`]. pubtype WithIdentityProvider<I, C> =
Config<I, <C as IntoConfig>::MlsRules, <C as IntoConfig>::CryptoProvider>;
/// Change the proposal filter used by a client configuration. /// /// See [`ExternalClientBuilder::mls_rules`]. pubtype WithMlsRules<Pr, C> =
Config<<C as IntoConfig>::IdentityProvider, Pr, <C as IntoConfig>::CryptoProvider>;
/// Change the crypto provider used by a client configuration. /// /// See [`ExternalClientBuilder::crypto_provider`]. pubtype WithCryptoProvider<Cp, C> =
Config<<C as IntoConfig>::IdentityProvider, <C as IntoConfig>::MlsRules, Cp>;
/// Helper alias for `Config`. pubtype IntoConfigOutput<C> = Config<
<C as IntoConfig>::IdentityProvider,
<C as IntoConfig>::MlsRules,
<C as IntoConfig>::CryptoProvider,
>;
impl<Ip, Pr, Cp> ExternalClientConfig for ConfigInner<Ip, Pr, Cp> where
Ip: IdentityProvider + Clone,
Pr: MlsRules + Clone,
Cp: CryptoProvider + Clone,
{ type IdentityProvider = Ip; type MlsRules = Pr; type CryptoProvider = Cp;
impl<Ip, Mpf, Cp> Sealed for Config<Ip, Mpf, Cp> {}
impl<Ip, Pr, Cp> MlsConfig for Config<Ip, Pr, Cp> where
Ip: IdentityProvider + Clone,
Pr: MlsRules + Clone,
Cp: CryptoProvider + Clone,
{ type Output = ConfigInner<Ip, Pr, Cp>;
fn get(&self) -> &Self::Output {
&self.0
}
}
/// Helper trait to allow consuming crates to easily write an external client type as /// `ExternalClient<impl MlsConfig>` /// /// It is not meant to be implemented by consuming crates. `T: MlsConfig` implies /// `T: ExternalClientConfig`. pubtrait MlsConfig: Send + Sync + Clone + Sealed { #[doc(hidden)] type Output: ExternalClientConfig;
#[doc(hidden)] fn get(&self) -> &Self::Output;
}
/// Blanket implementation so that `T: MlsConfig` implies `T: ExternalClientConfig` impl<T: MlsConfig> ExternalClientConfig for T { type IdentityProvider = <T::Output as ExternalClientConfig>::IdentityProvider; type MlsRules = <T::Output as ExternalClientConfig>::MlsRules; type CryptoProvider = <T::Output as ExternalClientConfig>::CryptoProvider;
/// Definitions meant to be private that are inaccessible outside this crate. They need to be marked /// `pub` because they appear in public definitions. mod private { use mls_rs_core::{crypto::SignatureSecretKey, identity::SigningIdentity};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.