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