- add: login via token
- update: login request packet uses token login instead of user & pass
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::auth::auth_service_client::AuthServiceClient;
|
||||
use crate::auth::{LoginRequest, LoginResponse};
|
||||
use crate::auth::{LoginRequest, LoginResponse, ValidateTokenRequest, ValidateTokenResponse};
|
||||
use tonic::transport::Channel;
|
||||
|
||||
pub struct AuthClient {
|
||||
@@ -21,4 +21,13 @@ impl AuthClient {
|
||||
let response = self.client.login(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn login_token(&mut self, token: &str) -> Result<ValidateTokenResponse, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let request = ValidateTokenRequest {
|
||||
token: token.to_string(),
|
||||
};
|
||||
|
||||
let response = self.client.validate_token(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,19 +40,27 @@ pub(crate) async fn handle_login_req(stream: &mut TcpStream, packet: Packet, aut
|
||||
debug!("{:?}", data);
|
||||
|
||||
let mut auth_client = auth_client.lock().await;
|
||||
match auth_client.login(&data.username.0, &data.password.password).await {
|
||||
match auth_client.login_token(&data.token.0).await {
|
||||
Ok(response) => {
|
||||
debug!("successfully logged in");
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::Ok, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
if response.valid == false {
|
||||
info!("Login failed: Invalid credentials");
|
||||
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::UnknownAccount, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
} else {
|
||||
debug!("Successfully logged in");
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::Ok, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
}
|
||||
}
|
||||
Err(status) => {
|
||||
if let Some(tonic_status) = status.downcast_ref::<Status>() {
|
||||
match tonic_status.code() {
|
||||
Code::Unauthenticated => {
|
||||
info!("Login failed: Invalid credentials");
|
||||
|
||||
|
||||
let data = SrvLoginReply { result: srv_login_reply::Result::UnknownAccount, right: 0, type_: 0, servers_info: Vec::new() };
|
||||
let response_packet = Packet::new(PacketType::PaklcLoginReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
|
||||
Reference in New Issue
Block a user