|
| 1 | +use std::{pin::Pin, sync::Arc}; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use serde::Deserialize; |
| 5 | +use shield::{Form, Input, InputType, ShieldError, User}; |
| 6 | + |
| 7 | +use crate::Credentials; |
| 8 | + |
| 9 | +#[derive(Debug, Deserialize)] |
| 10 | +pub struct EmailPasswordData { |
| 11 | + pub email: String, |
| 12 | + pub password: String, |
| 13 | +} |
| 14 | + |
| 15 | +type SignInFn<U> = dyn Fn(EmailPasswordData) -> Pin<Box<dyn Future<Output = Result<U, ShieldError>> + Send + Sync>> |
| 16 | + + Send |
| 17 | + + Sync; |
| 18 | + |
| 19 | +pub struct EmailPasswordCredentials<U: User> { |
| 20 | + sign_in_fn: Arc<SignInFn<U>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl<U: User> EmailPasswordCredentials<U> { |
| 24 | + pub fn new( |
| 25 | + sign_in_fn: impl Fn( |
| 26 | + EmailPasswordData, |
| 27 | + ) |
| 28 | + -> Pin<Box<dyn Future<Output = Result<U, ShieldError>> + Send + Sync>> |
| 29 | + + Send |
| 30 | + + Sync |
| 31 | + + 'static, |
| 32 | + ) -> Self { |
| 33 | + Self { |
| 34 | + sign_in_fn: Arc::new(sign_in_fn), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[async_trait] |
| 40 | +impl<U: User> Credentials<U, EmailPasswordData> for EmailPasswordCredentials<U> { |
| 41 | + fn form(&self) -> Form { |
| 42 | + Form { |
| 43 | + inputs: vec![ |
| 44 | + Input { |
| 45 | + name: "email".to_owned(), |
| 46 | + label: Some("Email address".to_owned()), |
| 47 | + r#type: InputType::Email { |
| 48 | + autocomplete: Some("email".to_owned()), |
| 49 | + dirname: None, |
| 50 | + list: None, |
| 51 | + maxlength: None, |
| 52 | + minlength: None, |
| 53 | + multiple: None, |
| 54 | + pattern: None, |
| 55 | + placeholder: Some("Email address".to_owned()), |
| 56 | + readonly: None, |
| 57 | + required: Some(true), |
| 58 | + size: None, |
| 59 | + }, |
| 60 | + value: None, |
| 61 | + attributes: None, |
| 62 | + }, |
| 63 | + Input { |
| 64 | + name: "password".to_owned(), |
| 65 | + label: Some("Password".to_owned()), |
| 66 | + r#type: InputType::Password { |
| 67 | + autocomplete: Some("current-password".to_owned()), |
| 68 | + dirname: None, |
| 69 | + maxlength: None, |
| 70 | + minlength: None, |
| 71 | + pattern: None, |
| 72 | + placeholder: Some("Password".to_owned()), |
| 73 | + readonly: None, |
| 74 | + required: Some(true), |
| 75 | + size: None, |
| 76 | + }, |
| 77 | + value: None, |
| 78 | + attributes: None, |
| 79 | + }, |
| 80 | + ], |
| 81 | + attributes: None, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + async fn sign_in(&self, data: EmailPasswordData) -> Result<U, ShieldError> { |
| 86 | + (self.sign_in_fn)(data).await |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use async_trait::async_trait; |
| 93 | + use serde::{Deserialize, Serialize}; |
| 94 | + use shield::{EmailAddress, ShieldError, StorageError, User}; |
| 95 | + |
| 96 | + use crate::Credentials; |
| 97 | + |
| 98 | + use super::{EmailPasswordCredentials, EmailPasswordData}; |
| 99 | + |
| 100 | + #[derive(Clone, Debug, Deserialize, Serialize)] |
| 101 | + pub struct TestUser { |
| 102 | + id: String, |
| 103 | + name: Option<String>, |
| 104 | + } |
| 105 | + |
| 106 | + #[async_trait] |
| 107 | + impl User for TestUser { |
| 108 | + fn id(&self) -> String { |
| 109 | + self.id.clone() |
| 110 | + } |
| 111 | + |
| 112 | + fn name(&self) -> Option<String> { |
| 113 | + self.name.clone() |
| 114 | + } |
| 115 | + |
| 116 | + async fn email_addresses(&self) -> Result<Vec<EmailAddress>, StorageError> { |
| 117 | + Ok(vec![]) |
| 118 | + } |
| 119 | + |
| 120 | + fn additional(&self) -> Option<impl Serialize> { |
| 121 | + None::<()> |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + #[tokio::test] |
| 126 | + async fn email_password_credentials() -> Result<(), ShieldError> { |
| 127 | + let credentials = EmailPasswordCredentials::new(|data: EmailPasswordData| { |
| 128 | + Box::pin(async move { |
| 129 | + if data.email == "test@example.com" && data.password == "test" { |
| 130 | + Ok(TestUser { |
| 131 | + id: "1".to_owned(), |
| 132 | + name: Some("Test".to_owned()), |
| 133 | + }) |
| 134 | + } else { |
| 135 | + Err(ShieldError::Validation( |
| 136 | + "Incorrect email and password combination.".to_owned(), |
| 137 | + )) |
| 138 | + } |
| 139 | + }) |
| 140 | + }); |
| 141 | + |
| 142 | + assert!( |
| 143 | + credentials |
| 144 | + .sign_in(EmailPasswordData { |
| 145 | + email: "test@example.com".to_owned(), |
| 146 | + password: "incorrect".to_owned(), |
| 147 | + }) |
| 148 | + .await |
| 149 | + .is_err_and(|err| err |
| 150 | + .to_string() |
| 151 | + .contains("Incorrect email and password combination.")) |
| 152 | + ); |
| 153 | + |
| 154 | + let user = credentials |
| 155 | + .sign_in(EmailPasswordData { |
| 156 | + email: "test@example.com".to_owned(), |
| 157 | + password: "test".to_owned(), |
| 158 | + }) |
| 159 | + .await?; |
| 160 | + |
| 161 | + assert_eq!(user.name, Some("Test".to_owned())); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | +} |
0 commit comments