v1.0.1 changes #11

Manually merged
Raven merged 54 commits from betterauth into main 2025-03-21 23:36:08 -04:00
7 changed files with 81 additions and 67 deletions
Showing only changes of commit 14c6aa485a - Show all commits

View File

@@ -1,8 +1,8 @@
use crate::auth::auth_service_server::AuthService; use crate::auth::auth_service_server::AuthService;
use crate::auth::{ use crate::auth::{
LoginRequest, LoginResponse, LogoutRequest, PasswordResetRequest, PasswordResetResponse, LoginRequest, LoginResponse, LogoutRequest, PasswordResetRequest, PasswordResetResponse,
RegisterRequest, RegisterResponse, ResetPasswordRequest, ResetPasswordResponse, RefreshSessionResponse, RegisterRequest, RegisterResponse, ResetPasswordRequest,
ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse, ResetPasswordResponse, ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse,
}; };
use crate::common::Empty; use crate::common::Empty;
use crate::database_client::{DatabaseClient, DatabaseClientTrait}; use crate::database_client::{DatabaseClient, DatabaseClientTrait};
@@ -160,7 +160,7 @@ impl AuthService for MyAuthService {
async fn refresh_session( async fn refresh_session(
&self, &self,
request: Request<ValidateSessionRequest>, request: Request<ValidateSessionRequest>,
) -> Result<Response<ValidateSessionResponse>, Status> { ) -> Result<Response<RefreshSessionResponse>, Status> {
let req = request.into_inner(); let req = request.into_inner();
let response = self let response = self
.session_client .session_client
@@ -175,11 +175,11 @@ impl AuthService for MyAuthService {
Ok(res) => { Ok(res) => {
let res = res.into_inner(); let res = res.into_inner();
debug!("Session valid: {:?}", res); debug!("Session valid: {:?}", res);
Ok(Response::new(ValidateSessionResponse { valid: true, session_id: res.session_id.to_string(), user_id: res.user_id.to_string() })) Ok(Response::new(RefreshSessionResponse { valid: res.valid }))
} }
Err(_) => { Err(_) => {
debug!("Session invalid or not found"); debug!("Session invalid or not found");
Ok(Response::new(ValidateSessionResponse { valid: false, session_id: "".to_string(), user_id: "".to_string() })) Ok(Response::new(RefreshSessionResponse { valid: false }))
} }
} }
} }

View File

@@ -1,7 +1,7 @@
use crate::auth::auth_service_client::AuthServiceClient; use crate::auth::auth_service_client::AuthServiceClient;
use crate::auth::{ use crate::auth::{
LoginRequest, LoginResponse, LogoutRequest, ValidateSessionRequest, ValidateSessionResponse, LoginRequest, LoginResponse, LogoutRequest, RefreshSessionResponse, ValidateSessionRequest,
ValidateTokenRequest, ValidateTokenResponse, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse,
}; };
use crate::common::Empty; use crate::common::Empty;
use tonic::transport::Channel; use tonic::transport::Channel;
@@ -60,7 +60,7 @@ impl AuthClient {
pub async fn refresh_session( pub async fn refresh_session(
&mut self, &mut self,
session_id: &str, session_id: &str,
) -> Result<ValidateSessionResponse, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<RefreshSessionResponse, Box<dyn std::error::Error + Send + Sync>> {
let request = ValidateSessionRequest { let request = ValidateSessionRequest {
session_id: session_id.to_string(), session_id: session_id.to_string(),
}; };

View File

@@ -9,7 +9,7 @@ service AuthService {
rpc Logout(LogoutRequest) returns (common.Empty); rpc Logout(LogoutRequest) returns (common.Empty);
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse); rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse);
rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse); rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse);
rpc RefreshSession(ValidateSessionRequest) returns (ValidateSessionResponse); rpc RefreshSession(ValidateSessionRequest) returns (RefreshSessionResponse);
rpc Register (RegisterRequest) returns (RegisterResponse); rpc Register (RegisterRequest) returns (RegisterResponse);
rpc RequestPasswordReset (PasswordResetRequest) returns (PasswordResetResponse); rpc RequestPasswordReset (PasswordResetRequest) returns (PasswordResetResponse);
rpc ResetPassword (ResetPasswordRequest) returns (ResetPasswordResponse); rpc ResetPassword (ResetPasswordRequest) returns (ResetPasswordResponse);
@@ -51,6 +51,10 @@ message ValidateSessionResponse {
string user_id = 3; string user_id = 3;
} }
message RefreshSessionResponse {
bool valid = 1;
}
message RegisterRequest { message RegisterRequest {
string username = 1; string username = 1;
string email = 2; string email = 2;

View File

@@ -7,7 +7,7 @@ import "common.proto";
service SessionService { service SessionService {
rpc CreateSession (CreateSessionRequest) returns (SessionResponse); rpc CreateSession (CreateSessionRequest) returns (SessionResponse);
rpc GetSession (GetSessionRequest) returns (SessionResponse); rpc GetSession (GetSessionRequest) returns (SessionResponse);
rpc RefreshSession (GetSessionRequest) returns (SessionResponse); rpc RefreshSession (GetSessionRequest) returns (RefreshSessionResponse);
rpc DeleteSession (DeleteSessionRequest) returns (common.Empty); rpc DeleteSession (DeleteSessionRequest) returns (common.Empty);
} }
@@ -36,3 +36,7 @@ message SessionResponse {
string ip_address = 6; string ip_address = 6;
} }
message RefreshSessionResponse {
bool valid = 1;
}

View File

@@ -3,65 +3,66 @@ syntax = "proto3";
package world; package world;
service WorldService { service WorldService {
rpc GetCharacter(CharacterRequest) returns (CharacterResponse); rpc GetCharacter(CharacterRequest) returns (CharacterResponse);
rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse); rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse);
rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse); rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse);
rpc GetTargetHp(ObjectHpRequest) returns (ObjectHpResponse);
} }
message CharacterRequest { message CharacterRequest {
string token = 1; string token = 1;
string user_id = 2; string user_id = 2;
string char_id = 3; string char_id = 3;
string session_id = 4; string session_id = 4;
} }
message CharacterResponse { message CharacterResponse {
int32 count = 1; int32 count = 1;
} }
message CharacterMoveRequest { message CharacterMoveRequest {
string session_id = 1; string session_id = 1;
uint32 target_id = 2; uint32 target_id = 2;
float x = 3; float x = 3;
float y = 4; float y = 4;
float z = 5; float z = 5;
} }
message CharacterMoveResponse { message CharacterMoveResponse {
int32 id = 1; int32 id = 1;
int32 target_id = 2; int32 target_id = 2;
int32 distance = 3; int32 distance = 3;
float x = 4; float x = 4;
float y = 5; float y = 5;
float z = 6; float z = 6;
} }
message ChangeMapRequest { message ChangeMapRequest {
int32 id = 1; int32 id = 1;
float x = 2; float x = 2;
float y = 3; float y = 3;
} }
message ChangeMapResponse { message ChangeMapResponse {
int32 id = 1; int32 id = 1;
int32 map_id = 2; int32 map_id = 2;
float x = 3; float x = 3;
float y = 4; float y = 4;
int32 move_mode = 5; int32 move_mode = 5;
int32 ride_mode = 6; int32 ride_mode = 6;
} }
message AttackRequest { message AttackRequest {
string session_id = 1; string session_id = 1;
uint32 target_id = 2; uint32 target_id = 2;
} }
message ObjectHpRequest { message ObjectHpRequest {
string session_id = 1; string session_id = 1;
uint32 target_id = 2; uint32 target_id = 2;
} }
message ObjectHpResponse { message ObjectHpResponse {
uint32 target_id = 1; uint32 target_id = 1;
int32 hp = 2; int32 hp = 2;
} }

View File

@@ -1,10 +1,11 @@
use crate::api::session_service_server::SessionService; use crate::api::session_service_server::SessionService;
use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, SessionResponse}; use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, RefreshSessionResponse, SessionResponse};
use crate::common::Empty; use crate::common::Empty;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tonic::{Request, Response, Status}; use tonic::{Request, Response, Status};
use tracing::debug;
use utils::redis_cache::{Cache, RedisCache}; use utils::redis_cache::{Cache, RedisCache};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@@ -100,34 +101,25 @@ impl SessionService for SessionServiceImpl {
Ok(Response::new(Empty {})) Ok(Response::new(Empty {}))
} }
async fn refresh_session( async fn refresh_session(
&self, &self,
request: Request<GetSessionRequest>, request: Request<GetSessionRequest>,
) -> Result<Response<SessionResponse>, Status> { ) -> Result<Response<RefreshSessionResponse>, Status> {
let req = request.into_inner(); let req = request.into_inner();
let conn = self.redis.lock().await; let conn = self.redis.lock().await;
match conn.refresh(&req.session_id, 300).await {
Ok(Response) => {
let response = RefreshSessionResponse {
valid: true
};
if let Some(session_data) = conn debug!("Session refreshed: {:?}", response);
.get::<String>(&req.session_id) Ok(Response::new(response))
.await }
.map_err(|_| Status::internal("Failed to fetch session from Redis"))? Err(e) => {
{ Err(Status::unknown(e.to_string()))
let _ = conn.update(&req.session_id, Some(&session_data), Some(300)); }
let session: Session = serde_json::from_str(&session_data)
.map_err(|_| Status::internal("Failed to deserialize session"))?;
let response = SessionResponse {
session_id: req.session_id,
user_id: session.user_id,
username: session.username,
character_id: session.character_id,
login_time: session.login_time,
ip_address: session.ip_address,
};
Ok(Response::new(response))
} else {
Err(Status::not_found("Session not found"))
} }
} }
} }

View File

@@ -25,6 +25,8 @@ pub trait Cache {
) -> Result<Option<T>, redis::RedisError>; ) -> Result<Option<T>, redis::RedisError>;
async fn delete(&mut self, key: &str) -> redis::RedisResult<()>; async fn delete(&mut self, key: &str) -> redis::RedisResult<()>;
async fn refresh(&self, key: &str, ttl: i64) -> redis::RedisResult<()>;
} }
pub struct RedisCache { pub struct RedisCache {
@@ -137,4 +139,15 @@ impl Cache for RedisCache {
})?; })?;
conn.del(key).await conn.del(key).await
} }
async fn refresh(&self, key: &str, ttl: i64) -> redis::RedisResult<()> {
let mut conn = self.pool.get().await.map_err(|err| {
redis::RedisError::from((
redis::ErrorKind::IoError,
"Failed to get Redis connection",
format!("{:?}", err),
))
})?;
conn.expire(key, ttl).await
}
} }