feat: Function new_captcha for CaptchaStorage trait to create and store captcha

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-12 20:13:18 +00:00
parent c6956ad729
commit 4240da2409
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -51,6 +51,24 @@ pub trait CaptchaStorage: Send + Sync + 'static {
&self, &self,
token: &str, token: &str,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send; ) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
/// Create a new captcha image and return the answer and the image encoded as png.
///
/// This method will store the answer in the storage.
fn new_captcha<G: crate::CaptchaGenerator>(
&self,
generator: G,
) -> impl std::future::Future<
Output = Result<(String, Vec<u8>), either::Either<Self::Error, G::Error>>,
> + Send {
async move {
let (answer, image) = generator.new_captcha().await.map_err(either::Right)?;
Ok((
self.store_answer(answer).await.map_err(either::Left)?,
image,
))
}
}
} }
impl<T> CaptchaStorage for Arc<T> impl<T> CaptchaStorage for Arc<T>