core_crypto/mls/session/
config.rs

1use core_crypto_keystore::Database;
2use typed_builder::TypedBuilder;
3
4use crate::{
5    ClientId,
6    mls::{
7        ciphersuite::MlsCiphersuite,
8        error::{Error, Result},
9    },
10};
11
12/// Configuration parameters for [Session][crate::mls::session::Session]
13///
14/// These can be set directly or via the [builder][SessionConfig::builder].
15#[derive(Debug, Clone, TypedBuilder)]
16pub struct SessionConfig {
17    /// Database to be used to instantiate the [MlsCryptoProvider][mls_crypto_provider::MlsCryptoProvider]
18    pub database: Database,
19    /// Identifier for the client to be used by [Session][crate::mls::session::Session]
20    ///
21    /// If set, this initializes the MLS session. Otherwise, the session is left uninitialized.
22    #[builder(default, setter(strip_option(fallback = client_id_opt)))]
23    pub client_id: Option<ClientId>,
24    /// All supported ciphersuites in this session
25    #[builder(default, setter(transform = |iter: impl IntoIterator<Item = MlsCiphersuite>| iter.into_iter().collect()))]
26    pub ciphersuites: Vec<MlsCiphersuite>,
27}
28
29/// Validated configuration parameters for [Session][crate::mls::session::Session].
30///
31/// These can not be constructed directly, only via [SessionConfig].
32#[derive(Debug)]
33pub struct ValidatedSessionConfig {
34    pub(super) database: Database,
35    pub(super) client_id: Option<ClientId>,
36    pub(super) ciphersuites: Vec<MlsCiphersuite>,
37}
38
39impl SessionConfig {
40    /// Validate this configuration to produce a validated configuration.
41    ///
42    /// This can then be passed to [Session::try_new][crate::mls::session::Session::try_new].
43    pub fn validate(self) -> Result<ValidatedSessionConfig> {
44        let Self {
45            database,
46            client_id,
47            ciphersuites,
48        } = self;
49        if let Some(client_id) = &client_id
50            && client_id.is_empty()
51        {
52            return Err(Error::MalformedIdentifier("client_id"));
53        }
54
55        if client_id.is_some() && ciphersuites.is_empty() {
56            return Err(Error::MalformedIdentifier(
57                "ciphersuites must be non-empty if initializing (i.e. client_id is set)",
58            ));
59        }
60
61        Ok(ValidatedSessionConfig {
62            database,
63            client_id,
64            ciphersuites,
65        })
66    }
67}
68
69impl TryFrom<SessionConfig> for ValidatedSessionConfig {
70    type Error = Error;
71
72    fn try_from(value: SessionConfig) -> std::result::Result<Self, Self::Error> {
73        value.validate()
74    }
75}