Skip to content

Commit b8c7a2d

Browse files
committed
Move the verify_password_if_needed helper up one level
1 parent 1130b4b commit b8c7a2d

File tree

2 files changed

+63
-61
lines changed

2 files changed

+63
-61
lines changed

crates/handlers/src/graphql/mutations/mod.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ mod oauth2_session;
1111
mod user;
1212
mod user_email;
1313

14+
use anyhow::Context as _;
1415
use async_graphql::MergedObject;
16+
use mas_data_model::SiteConfig;
17+
use mas_storage::BoxRepository;
18+
use zeroize::Zeroizing;
19+
20+
use super::Requester;
21+
use crate::passwords::PasswordManager;
1522

1623
/// The mutations root of the GraphQL interface.
1724
#[derive(Default, MergedObject)]
@@ -30,3 +37,54 @@ impl Mutation {
3037
Self::default()
3138
}
3239
}
40+
41+
/// Check the password if neeed
42+
///
43+
/// Returns true if password verification is not needed, or if the password is
44+
/// correct. Returns false if the password is incorrect or missing.
45+
async fn verify_password_if_needed(
46+
requester: &Requester,
47+
config: &SiteConfig,
48+
password_manager: &PasswordManager,
49+
password: Option<String>,
50+
user: &mas_data_model::User,
51+
repo: &mut BoxRepository,
52+
) -> Result<bool, async_graphql::Error> {
53+
// If the requester is admin, they don't need to provide a password
54+
if requester.is_admin() {
55+
return Ok(true);
56+
}
57+
58+
// If password login is disabled, assume we don't want the user to reauth
59+
if !config.password_login_enabled {
60+
return Ok(true);
61+
}
62+
63+
// Else we need to check if the user has a password
64+
let Some(user_password) = repo
65+
.user_password()
66+
.active(user)
67+
.await
68+
.context("Failed to load user password")?
69+
else {
70+
// User has no password, so we don't need to verify the password
71+
return Ok(true);
72+
};
73+
74+
let Some(password) = password else {
75+
// There is a password on the user, but not provided in the input
76+
return Ok(false);
77+
};
78+
79+
let password = Zeroizing::new(password.into_bytes());
80+
81+
let res = password_manager
82+
.verify(
83+
user_password.version,
84+
password,
85+
user_password.hashed_password,
86+
)
87+
.await;
88+
89+
Ok(res.is_ok())
90+
}

crates/handlers/src/graphql/mutations/user_email.rs

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,19 @@
66

77
use anyhow::Context as _;
88
use async_graphql::{Context, Description, Enum, ID, InputObject, Object};
9-
use mas_data_model::SiteConfig;
109
use mas_i18n::DataLocale;
1110
use mas_storage::{
12-
BoxRepository, RepositoryAccess,
11+
RepositoryAccess,
1312
queue::{ProvisionUserJob, QueueJobRepositoryExt as _, SendEmailAuthenticationCodeJob},
1413
user::{UserEmailFilter, UserEmailRepository, UserRepository},
1514
};
16-
use zeroize::Zeroizing;
1715

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,
2520
};
2621

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-
7822
#[derive(Default)]
7923
pub struct UserEmailMutations {
8024
_private: (),

0 commit comments

Comments
 (0)