From 6899c89da7aac13d6f8b092419fab3ae542d48e2 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 4 Aug 2024 22:00:26 +0300 Subject: kittybox-indieauth: allow using custom RNGs for state --- indieauth/src/lib.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'indieauth') diff --git a/indieauth/src/lib.rs b/indieauth/src/lib.rs index 741eb81..bbabe1f 100644 --- a/indieauth/src/lib.rs +++ b/indieauth/src/lib.rs @@ -28,10 +28,14 @@ pub use self::scopes::{Scope, Scopes}; mod pkce; pub use self::pkce::{PKCEMethod, PKCEVerifier, PKCEChallenge}; +// Re-export rand crate just to be sure. +pub use rand; + /// Authentication methods supported by the introspection endpoint. /// Note that authentication at the introspection endpoint is /// mandatory. #[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[non_exhaustive] pub enum IntrospectionEndpointAuthMethod { /// `Authorization` header with a `Bearer` token. Bearer, @@ -58,6 +62,7 @@ pub enum IntrospectionEndpointAuthMethod { /// disturbing anyone. #[derive(Copy, Clone, Debug, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub enum RevocationEndpointAuthMethod { /// No authentication is required to access an endpoint declaring /// this value. @@ -263,12 +268,19 @@ pub struct State(String); impl State { /// Generate a random state string of 128 bytes in length. pub fn new() -> Self { + Self::from_rng(&mut rand::thread_rng()) + } + + /// Generate a random state string of 128 bytes in length, using + /// the provided random number generator. + pub fn from_rng(rng: &mut (impl rand::CryptoRng + rand::Rng)) -> Self { use rand::{Rng, distributions::Alphanumeric}; - let bytes = rand::thread_rng() - .sample_iter(&Alphanumeric) + + let bytes = rng.sample_iter(&Alphanumeric) .take(128) .collect::>(); Self(String::from_utf8(bytes).unwrap()) + } } impl AsRef for State { -- cgit 1.4.1