ruma_identifiers/
client_secret.rs

1//! Client secret identifier.
2
3/// A client secret.
4///
5/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
6/// must not exceed 255 characters.
7///
8/// You can create one from a string (using `ClientSecret::parse()`) but the recommended way is to
9/// use `ClientSecret::new()` to generate a random one. If that function is not available for you,
10/// you need to activate this crate's `rand` Cargo feature.
11#[repr(transparent)]
12#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct ClientSecret(str);
14
15impl ClientSecret {
16    /// Creates a random client secret.
17    ///
18    /// This will currently be a UUID without hyphens, but no guarantees are made about the
19    /// structure of client secrets generated from this function.
20    #[cfg(feature = "rand")]
21    pub fn new() -> Box<Self> {
22        let id = uuid::Uuid::new_v4();
23        Self::from_owned(id.to_simple().to_string().into_boxed_str())
24    }
25}
26
27opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);
28
29#[cfg(test)]
30mod tests {
31    use std::convert::TryFrom;
32
33    use super::ClientSecret;
34
35    #[test]
36    fn valid_secret() {
37        assert!(<&ClientSecret>::try_from("this_=_a_valid_secret_1337").is_ok())
38    }
39}