Skip to content

Commit 37c14c3

Browse files
authored
More authrequest fixes (#5176)
1 parent d0581da commit 37c14c3

2 files changed

Lines changed: 47 additions & 36 deletions

File tree

src/api/core/accounts.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,15 +1136,15 @@ async fn post_auth_request(
11361136

11371137
#[get("/auth-requests/<uuid>")]
11381138
async fn get_auth_request(uuid: &str, headers: Headers, mut conn: DbConn) -> JsonResult {
1139-
if headers.user.uuid != uuid {
1140-
err!("AuthRequest doesn't exist", "User uuid's do not match")
1141-
}
1142-
11431139
let auth_request = match AuthRequest::find_by_uuid(uuid, &mut conn).await {
11441140
Some(auth_request) => auth_request,
11451141
None => err!("AuthRequest doesn't exist", "Record not found"),
11461142
};
11471143

1144+
if headers.user.uuid != auth_request.user_uuid {
1145+
err!("AuthRequest doesn't exist", "User uuid's do not match")
1146+
}
1147+
11481148
let response_date_utc = auth_request.response_date.map(|response_date| format_date(&response_date));
11491149

11501150
Ok(Json(json!({
@@ -1190,15 +1190,18 @@ async fn put_auth_request(
11901190
err!("AuthRequest doesn't exist", "User uuid's do not match")
11911191
}
11921192

1193-
auth_request.approved = Some(data.request_approved);
1194-
auth_request.enc_key = Some(data.key);
1195-
auth_request.master_password_hash = data.master_password_hash;
1196-
auth_request.response_device_id = Some(data.device_identifier.clone());
1197-
auth_request.save(&mut conn).await?;
1193+
if data.request_approved {
1194+
auth_request.approved = Some(data.request_approved);
1195+
auth_request.enc_key = Some(data.key);
1196+
auth_request.master_password_hash = data.master_password_hash;
1197+
auth_request.response_device_id = Some(data.device_identifier.clone());
1198+
auth_request.save(&mut conn).await?;
11981199

1199-
if auth_request.approved.unwrap_or(false) {
12001200
ant.send_auth_response(&auth_request.user_uuid, &auth_request.uuid).await;
12011201
nt.send_auth_response(&auth_request.user_uuid, &auth_request.uuid, data.device_identifier, &mut conn).await;
1202+
} else {
1203+
// If denied, there's no reason to keep the request
1204+
auth_request.delete(&mut conn).await?;
12021205
}
12031206

12041207
let response_date_utc = auth_request.response_date.map(|response_date| format_date(&response_date));

src/api/identity.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,27 +165,46 @@ async fn _password_login(
165165
// Set the user_uuid here to be passed back used for event logging.
166166
*user_uuid = Some(user.uuid.clone());
167167

168-
// Check password
169-
let password = data.password.as_ref().unwrap();
170-
if let Some(auth_request_uuid) = data.auth_request.clone() {
171-
if let Some(auth_request) = AuthRequest::find_by_uuid(auth_request_uuid.as_str(), conn).await {
172-
if !auth_request.check_access_code(password) {
173-
err!(
174-
"Username or access code is incorrect. Try again",
175-
format!("IP: {}. Username: {}.", ip.ip, username),
176-
ErrorEvent {
177-
event: EventType::UserFailedLogIn,
178-
}
179-
)
168+
// Check if the user is disabled
169+
if !user.enabled {
170+
err!(
171+
"This user has been disabled",
172+
format!("IP: {}. Username: {}.", ip.ip, username),
173+
ErrorEvent {
174+
event: EventType::UserFailedLogIn
180175
}
181-
} else {
176+
)
177+
}
178+
179+
let password = data.password.as_ref().unwrap();
180+
181+
// If we get an auth request, we don't check the user's password, but the access code of the auth request
182+
if let Some(ref auth_request_uuid) = data.auth_request {
183+
let Some(auth_request) = AuthRequest::find_by_uuid(auth_request_uuid.as_str(), conn).await else {
182184
err!(
183185
"Auth request not found. Try again.",
184186
format!("IP: {}. Username: {}.", ip.ip, username),
185187
ErrorEvent {
186188
event: EventType::UserFailedLogIn,
187189
}
188190
)
191+
};
192+
193+
// Delete the request after we used it
194+
auth_request.delete(conn).await?;
195+
196+
if auth_request.user_uuid != user.uuid
197+
|| !auth_request.approved.unwrap_or(false)
198+
|| ip.ip.to_string() != auth_request.request_ip
199+
|| !auth_request.check_access_code(password)
200+
{
201+
err!(
202+
"Username or access code is incorrect. Try again",
203+
format!("IP: {}. Username: {}.", ip.ip, username),
204+
ErrorEvent {
205+
event: EventType::UserFailedLogIn,
206+
}
207+
)
189208
}
190209
} else if !user.check_valid_password(password) {
191210
err!(
@@ -197,8 +216,8 @@ async fn _password_login(
197216
)
198217
}
199218

200-
// Change the KDF Iterations
201-
if user.password_iterations != CONFIG.password_iterations() {
219+
// Change the KDF Iterations (only when not logging in with an auth request)
220+
if data.auth_request.is_none() && user.password_iterations != CONFIG.password_iterations() {
202221
user.password_iterations = CONFIG.password_iterations();
203222
user.set_password(password, None, false, None);
204223

@@ -207,17 +226,6 @@ async fn _password_login(
207226
}
208227
}
209228

210-
// Check if the user is disabled
211-
if !user.enabled {
212-
err!(
213-
"This user has been disabled",
214-
format!("IP: {}. Username: {}.", ip.ip, username),
215-
ErrorEvent {
216-
event: EventType::UserFailedLogIn
217-
}
218-
)
219-
}
220-
221229
let now = Utc::now().naive_utc();
222230

223231
if user.verified_at.is_none() && CONFIG.mail_enabled() && CONFIG.signups_verify() {

0 commit comments

Comments
 (0)