forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webserver): Add authenticated endpoint to update password (Tabby…
…ML#1553) * feat(webserver): Add authenticated endpoint to update password * [autofix.ci] apply automated fixes * Apply suggestions * [autofix.ci] apply automated fixes * Make password optional * Add todo * [autofix.ci] apply automated fixes * switch to id --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Meng Zhang <[email protected]>
- Loading branch information
1 parent
0e6eec4
commit d0836db
Showing
5 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,35 @@ impl AuthenticationService for AuthenticationServiceImpl { | |
Ok(()) | ||
} | ||
|
||
async fn update_user_password( | ||
&self, | ||
id: &ID, | ||
old_password: Option<&str>, | ||
new_password: &str, | ||
) -> Result<()> { | ||
let user = self | ||
.db | ||
.get_user(id.as_rowid()?) | ||
.await? | ||
.ok_or_else(|| anyhow!("Invalid user"))?; | ||
|
||
let password_verified = match (user.password_encrypted.is_empty(), old_password) { | ||
(true, _) => true, | ||
(false, None) => false, | ||
(false, Some(old_password)) => password_verify(old_password, &user.password_encrypted), | ||
}; | ||
if !password_verified { | ||
return Err(anyhow!("Password is incorrect").into()); | ||
} | ||
|
||
let new_password_encrypted = | ||
password_hash(new_password).map_err(|_| anyhow!("Unknown error"))?; | ||
self.db | ||
.update_user_password(user.id, new_password_encrypted) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
async fn token_auth(&self, email: String, password: String) -> Result<TokenAuthResponse> { | ||
let Some(user) = self.db.get_user_by_email(&email).await? else { | ||
return Err(anyhow!("User not found").into()); | ||
|
@@ -1165,4 +1194,31 @@ mod tests { | |
Err(CoreError::InvalidLicense(_)) | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_update_password() { | ||
let service = test_authentication_service().await; | ||
let id = service | ||
.db | ||
.create_user("[email protected]".into(), "".into(), true) | ||
.await | ||
.unwrap(); | ||
|
||
let id = id.as_id(); | ||
|
||
assert!(service | ||
.update_user_password(&id, None, "newpass") | ||
.await | ||
.is_ok()); | ||
|
||
assert!(service | ||
.update_user_password(&id, None, "newpass2") | ||
.await | ||
.is_err()); | ||
|
||
assert!(service | ||
.update_user_password(&id, Some("newpass"), "newpass2") | ||
.await | ||
.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters