Skip to content

Commit

Permalink
refactor(client)!: refactor AuthClient::user_change_password
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <[email protected]>
  • Loading branch information
lxl66566 committed Aug 1, 2024
1 parent 87f7fe7 commit eb7da44
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 55 deletions.
6 changes: 2 additions & 4 deletions crates/xline-client/examples/auth_user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use xline_client::{types::auth::AuthUserChangePasswordRequest, Client, ClientOptions};
use xline_client::{Client, ClientOptions};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -15,9 +15,7 @@ async fn main() -> Result<()> {
client.user_add("user2", "", true).await?;

// change user1's password to "123"
client
.user_change_password(AuthUserChangePasswordRequest::new("user1", "123"))
.await?;
client.user_change_password("user1", "123").await?;

// grant roles
client.user_grant_role("user1", "role1").await?;
Expand Down
27 changes: 16 additions & 11 deletions crates/xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{
types::auth::{
AuthRoleAddRequest, AuthRoleDeleteRequest, AuthRoleGetRequest,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
AuthUserChangePasswordRequest,
},
AuthService, CurpClient,
};
Expand Down Expand Up @@ -378,9 +377,7 @@ impl AuthClient {
/// # Examples
///
/// ```no_run
/// use xline_client::{
/// types::auth::AuthUserChangePasswordRequest, Client, ClientOptions,
/// };
/// use xline_client::{Client, ClientOptions};
/// use anyhow::Result;
///
/// #[tokio::main]
Expand All @@ -394,7 +391,7 @@ impl AuthClient {
/// // add the user
///
/// client
/// .user_change_password(AuthUserChangePasswordRequest::new("user", "123"))
/// .user_change_password("user", "123")
/// .await?;
///
/// Ok(())
Expand All @@ -403,19 +400,27 @@ impl AuthClient {
#[inline]
pub async fn user_change_password(
&self,
mut request: AuthUserChangePasswordRequest,
name: impl Into<String>,
password: impl AsRef<str>,
) -> Result<AuthUserChangePasswordResponse> {
if request.inner.password.is_empty() {
let password: &str = password.as_ref();
if password.is_empty() {
return Err(XlineClientError::InvalidArgs(String::from(
"role name is empty",
)));
}
let hashed_password = hash_password(request.inner.password.as_bytes()).map_err(|err| {
let hashed_password = hash_password(password.as_bytes()).map_err(|err| {
XlineClientError::InternalError(format!("Failed to hash password: {err}"))
})?;
request.inner.hashed_password = hashed_password;
request.inner.password = String::new();
self.handle_req(request.inner, false).await
self.handle_req(
xlineapi::AuthUserChangePasswordRequest {
name: name.into(),
hashed_password,
password: String::new(),
},
false,
)
.await
}

/// Grant role for an user.
Expand Down
28 changes: 0 additions & 28 deletions crates/xline-client/src/types/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,6 @@ pub use xlineapi::{
AuthenticateResponse, Type as PermissionType,
};

/// Request for `AuthUserChangePassword`
#[derive(Debug, PartialEq)]
pub struct AuthUserChangePasswordRequest {
/// Inner request
pub(crate) inner: xlineapi::AuthUserChangePasswordRequest,
}

impl AuthUserChangePasswordRequest {
/// Creates a new `AuthUserChangePasswordRequest`.
#[inline]
pub fn new(user_name: impl Into<String>, new_password: impl Into<String>) -> Self {
Self {
inner: xlineapi::AuthUserChangePasswordRequest {
name: user_name.into(),
password: new_password.into(),
hashed_password: String::new(),
},
}
}
}

impl From<AuthUserChangePasswordRequest> for xlineapi::AuthUserChangePasswordRequest {
#[inline]
fn from(req: AuthUserChangePasswordRequest) -> Self {
req.inner
}
}

/// Request for `AuthRoleAdd`
#[derive(Debug, PartialEq)]
pub struct AuthRoleAddRequest {
Expand Down
8 changes: 3 additions & 5 deletions crates/xline-client/tests/it/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use xline_client::{
error::Result,
types::auth::{
AuthRoleAddRequest, AuthRoleDeleteRequest, AuthRoleGetRequest,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
AuthUserChangePasswordRequest, Permission, PermissionType,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest, Permission,
PermissionType,
},
};

Expand Down Expand Up @@ -133,9 +133,7 @@ async fn user_operations_should_success_in_normal_path() -> Result<()> {
let user_list_resp = client.user_list().await?;
assert!(user_list_resp.users.contains(&name1.to_string()));

client
.user_change_password(AuthUserChangePasswordRequest::new(name1, password2))
.await?;
client.user_change_password(name1, password2).await?;

client.user_delete(name1).await?;
client.user_get(name1).await.unwrap_err();
Expand Down
17 changes: 10 additions & 7 deletions crates/xlinectl/src/command/user/passwd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use clap::{arg, ArgMatches, Command};
use xline_client::{error::Result, types::auth::AuthUserChangePasswordRequest, Client};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

/// Temporary request for changing password. 0 is name, 1 is password
type AuthUserChangePasswordRequest = (String, String);

/// Definition of `passwd` command
// TODO: interactive mode
pub(super) fn command() -> Command {
Expand All @@ -16,13 +19,16 @@ pub(super) fn command() -> Command {
pub(super) fn build_request(matches: &ArgMatches) -> AuthUserChangePasswordRequest {
let name = matches.get_one::<String>("name").expect("required");
let password = matches.get_one::<String>("password").expect("required");
AuthUserChangePasswordRequest::new(name, password)
(name.to_owned(), password.to_owned())
}

/// Execute the command
pub(super) async fn execute(client: &mut Client, matches: &ArgMatches) -> Result<()> {
let req = build_request(matches);
let resp = client.auth_client().user_change_password(req).await?;
let resp = client
.auth_client()
.user_change_password(req.0, req.1)
.await?;
resp.print();

Ok(())
Expand All @@ -39,10 +45,7 @@ mod tests {
fn command_parse_should_be_valid() {
let test_cases = vec![TestCase::new(
vec!["passwd", "JohnDoe", "new_password"],
Some(AuthUserChangePasswordRequest::new(
"JohnDoe",
"new_password",
)),
Some(("JohnDoe".into(), "new_password".into())),
)];

for case in test_cases {
Expand Down

0 comments on commit eb7da44

Please sign in to comment.