From 1b411fc62268497ff4c73886dbe46b90493ed322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 19 Feb 2022 14:13:31 +0100 Subject: [PATCH] identifiers: Add random ClientSecret generator Requires the rand feature. --- crates/ruma-identifiers/CHANGELOG.md | 1 + crates/ruma-identifiers/src/client_secret.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/ruma-identifiers/CHANGELOG.md b/crates/ruma-identifiers/CHANGELOG.md index 5df785b0..59d1c4c1 100644 --- a/crates/ruma-identifiers/CHANGELOG.md +++ b/crates/ruma-identifiers/CHANGELOG.md @@ -7,6 +7,7 @@ Breaking changes: Improvements: * Add `MatrixUri` to build `matrix:` URIs +* Add `ClientSecret::new()` with the `rand` feature to generate a random client secret # 0.21.0 diff --git a/crates/ruma-identifiers/src/client_secret.rs b/crates/ruma-identifiers/src/client_secret.rs index 57cd1168..e3a0790d 100644 --- a/crates/ruma-identifiers/src/client_secret.rs +++ b/crates/ruma-identifiers/src/client_secret.rs @@ -4,10 +4,26 @@ /// /// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must /// must not exceed 255 characters. +/// +/// You can create one from a string (using `ClientSecret::parse()`) but the recommended way is to +/// use `ClientSecret::new()` to generate a random one. If that function is not available for you, +/// you need to activate this crate's `rand` Cargo feature. #[repr(transparent)] #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ClientSecret(str); +impl ClientSecret { + /// Creates a random client secret. + /// + /// This will currently be a UUID without hyphens, but no guarantees are made about the + /// structure of client secrets generated from this function. + #[cfg(feature = "rand")] + pub fn new() -> Box { + let id = uuid::Uuid::new_v4(); + Self::from_owned(id.to_simple().to_string().into_boxed_str()) + } +} + opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate); #[cfg(test)]