|
6 | 6 |
|
7 | 7 | use anyhow::Context as _; |
8 | 8 | use async_graphql::{Context, Description, Enum, ID, InputObject, Object}; |
9 | | -use mas_data_model::SiteConfig; |
10 | 9 | use mas_i18n::DataLocale; |
11 | 10 | use mas_storage::{ |
12 | | - BoxRepository, RepositoryAccess, |
| 11 | + RepositoryAccess, |
13 | 12 | queue::{ProvisionUserJob, QueueJobRepositoryExt as _, SendEmailAuthenticationCodeJob}, |
14 | 13 | user::{UserEmailFilter, UserEmailRepository, UserRepository}, |
15 | 14 | }; |
16 | | -use zeroize::Zeroizing; |
17 | 15 |
|
18 | | -use crate::{ |
19 | | - graphql::{ |
20 | | - Requester, |
21 | | - model::{NodeType, User, UserEmail, UserEmailAuthentication}, |
22 | | - state::ContextExt, |
23 | | - }, |
24 | | - passwords::PasswordManager, |
| 16 | +use super::verify_password_if_needed; |
| 17 | +use crate::graphql::{ |
| 18 | + model::{NodeType, User, UserEmail, UserEmailAuthentication}, |
| 19 | + state::ContextExt, |
25 | 20 | }; |
26 | 21 |
|
27 | | -/// Check the password if neeed |
28 | | -/// |
29 | | -/// Returns true if password verification is not needed, or if the password is |
30 | | -/// correct. Returns false if the password is incorrect or missing. |
31 | | -async fn verify_password_if_needed( |
32 | | - requester: &Requester, |
33 | | - config: &SiteConfig, |
34 | | - password_manager: &PasswordManager, |
35 | | - password: Option<String>, |
36 | | - user: &mas_data_model::User, |
37 | | - repo: &mut BoxRepository, |
38 | | -) -> Result<bool, async_graphql::Error> { |
39 | | - // If the requester is admin, they don't need to provide a password |
40 | | - if requester.is_admin() { |
41 | | - return Ok(true); |
42 | | - } |
43 | | - |
44 | | - // If password login is disabled, assume we don't want the user to reauth |
45 | | - if !config.password_login_enabled { |
46 | | - return Ok(true); |
47 | | - } |
48 | | - |
49 | | - // Else we need to check if the user has a password |
50 | | - let Some(user_password) = repo |
51 | | - .user_password() |
52 | | - .active(user) |
53 | | - .await |
54 | | - .context("Failed to load user password")? |
55 | | - else { |
56 | | - // User has no password, so we don't need to verify the password |
57 | | - return Ok(true); |
58 | | - }; |
59 | | - |
60 | | - let Some(password) = password else { |
61 | | - // There is a password on the user, but not provided in the input |
62 | | - return Ok(false); |
63 | | - }; |
64 | | - |
65 | | - let password = Zeroizing::new(password.into_bytes()); |
66 | | - |
67 | | - let res = password_manager |
68 | | - .verify( |
69 | | - user_password.version, |
70 | | - password, |
71 | | - user_password.hashed_password, |
72 | | - ) |
73 | | - .await; |
74 | | - |
75 | | - Ok(res.is_ok()) |
76 | | -} |
77 | | - |
78 | 22 | #[derive(Default)] |
79 | 23 | pub struct UserEmailMutations { |
80 | 24 | _private: (), |
|
0 commit comments