Skip to main content

openauth_core/plugin/
password.rs

1//! Password validation plugin contracts.
2
3use crate::context::AuthContext;
4use http::StatusCode;
5use std::future::Future;
6use std::pin::Pin;
7use std::sync::Arc;
8
9pub type PluginPasswordValidatorFuture<'a> =
10    Pin<Box<dyn Future<Output = Result<(), PluginPasswordValidationRejection>> + Send + 'a>>;
11
12pub type PluginPasswordValidatorHandler = Arc<
13    dyn for<'a> Fn(
14            &'a AuthContext,
15            PluginPasswordValidationInput,
16        ) -> PluginPasswordValidatorFuture<'a>
17        + Send
18        + Sync,
19>;
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct PluginPasswordValidationInput {
23    pub path: String,
24    pub password: String,
25}
26
27impl PluginPasswordValidationInput {
28    pub fn new(path: impl Into<String>, password: impl Into<String>) -> Self {
29        Self {
30            path: path.into(),
31            password: password.into(),
32        }
33    }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct PluginPasswordValidationRejection {
38    pub status: StatusCode,
39    pub code: String,
40    pub message: String,
41}
42
43impl PluginPasswordValidationRejection {
44    pub fn bad_request(code: impl Into<String>, message: impl Into<String>) -> Self {
45        Self {
46            status: StatusCode::BAD_REQUEST,
47            code: code.into(),
48            message: message.into(),
49        }
50    }
51
52    pub fn internal_server_error(message: impl Into<String>) -> Self {
53        Self {
54            status: StatusCode::INTERNAL_SERVER_ERROR,
55            code: "INTERNAL_SERVER_ERROR".to_owned(),
56            message: message.into(),
57        }
58    }
59}
60
61#[derive(Clone)]
62pub struct PluginPasswordValidator {
63    pub handler: PluginPasswordValidatorHandler,
64}
65
66impl std::fmt::Debug for PluginPasswordValidator {
67    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        formatter
69            .debug_struct("PluginPasswordValidator")
70            .field("handler", &"<password-validator>")
71            .finish()
72    }
73}