- add: Refresh Session call to actually refresh the cache session.
This commit is contained in:
@@ -7,7 +7,7 @@ use crate::auth::{
|
||||
use crate::common::Empty;
|
||||
use crate::database_client::{DatabaseClient, DatabaseClientTrait};
|
||||
use crate::session::session_service_client::SessionServiceClient;
|
||||
use crate::session::{GetSessionRequest};
|
||||
use crate::session::{GetSessionRequest, RefreshSessionRequest};
|
||||
use crate::users::{hash_password, verify_user};
|
||||
use chrono::{Duration, Utc};
|
||||
use rand::Rng;
|
||||
@@ -78,7 +78,7 @@ impl AuthService for MyAuthService {
|
||||
.session_client
|
||||
.as_ref()
|
||||
.clone()
|
||||
.get_session(GetSessionRequest {
|
||||
.refresh_session(RefreshSessionRequest {
|
||||
session_id: req.session_id,
|
||||
})
|
||||
.await;
|
||||
@@ -87,7 +87,7 @@ impl AuthService for MyAuthService {
|
||||
Ok(res) => {
|
||||
let res = res.into_inner();
|
||||
debug!("Session valid: {:?}", res);
|
||||
Ok(Response::new(RefreshSessionResponse { valid: true }))
|
||||
Ok(Response::new(RefreshSessionResponse { valid: res.valid }))
|
||||
}
|
||||
Err(_) => {
|
||||
debug!("Unable to refresh session");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::session::{session_service_client::SessionServiceClient, GetSessionRequest, GetSessionResponse};
|
||||
use crate::session::{session_service_client::SessionServiceClient, GetSessionRequest, GetSessionResponse, RefreshSessionRequest, RefreshSessionResponse};
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::error::Error;
|
||||
@@ -11,6 +11,10 @@ pub trait SessionClientTrait: Sized {
|
||||
&mut self,
|
||||
session_id: String,
|
||||
) -> Result<GetSessionResponse, Box<dyn std::error::Error>>;
|
||||
async fn refresh_session(
|
||||
&mut self,
|
||||
session_id: String,
|
||||
) -> Result<RefreshSessionResponse, Box<dyn std::error::Error>>;
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct SessionClient {
|
||||
@@ -31,4 +35,11 @@ impl SessionClientTrait for SessionClient {
|
||||
let response = self.client.get_session(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
async fn refresh_session(&mut self, session_id: String) -> Result<RefreshSessionResponse, Box<dyn Error>> {
|
||||
let request = tonic::Request::new(RefreshSessionRequest {
|
||||
session_id,
|
||||
});
|
||||
let response = self.client.refresh_session(request).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::grpc::database_service::MyDatabaseService;
|
||||
use crate::grpc::session_service_server::SessionService;
|
||||
use tonic::{Request, Response, Status};
|
||||
use tracing::debug;
|
||||
use crate::grpc::{GetSessionRequest, GetSessionResponse};
|
||||
use crate::grpc::{GetSessionRequest, GetSessionResponse, RefreshSessionRequest, RefreshSessionResponse};
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl SessionService for MyDatabaseService {
|
||||
@@ -22,4 +22,19 @@ impl SessionService for MyDatabaseService {
|
||||
user_id: session.user_id,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn refresh_session(&self, request: Request<RefreshSessionRequest>) -> Result<Response<RefreshSessionResponse>, Status> {
|
||||
let req = request.into_inner();
|
||||
debug!("get_session: {:?}", req);
|
||||
|
||||
let session = self.db.session_repo.refresh_session(&req.session_id).await
|
||||
.map_err(|_| Status::not_found("Session not found"))?;
|
||||
|
||||
let valid = true;
|
||||
|
||||
debug!("session: {:?}", session);
|
||||
Ok(Response::new(RefreshSessionResponse {
|
||||
valid
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,34 @@ impl SessionRepository {
|
||||
debug!("session: {:?}", session);
|
||||
|
||||
self.cache.lock().await
|
||||
.set(&cache_key, &session, 0).await
|
||||
.set(&cache_key, &session, 300).await
|
||||
.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
pub async fn refresh_session(&self, session_id: &str) -> Result<Session, sqlx::Error> {
|
||||
let cache_key = format!("session:{}", session_id);
|
||||
|
||||
if let Some(session) = self.cache.lock().await
|
||||
.get::<Session>(&cache_key).await
|
||||
.map_err(|_| sqlx::Error::RowNotFound)?
|
||||
{
|
||||
self.cache.lock().await
|
||||
.refresh(&cache_key, 300).await
|
||||
.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
return Ok(session);
|
||||
}
|
||||
|
||||
// Check to make sure the session is still valid
|
||||
let session = sqlx::query_as::<_, Session>(
|
||||
"SELECT id, \"userId\" as user_id FROM session WHERE id = $1",
|
||||
)
|
||||
.bind(session_id)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
self.cache.lock().await
|
||||
.set(&cache_key, &session, 300).await
|
||||
.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package session_db_api;
|
||||
|
||||
service SessionService {
|
||||
rpc GetSession(GetSessionRequest) returns (GetSessionResponse);
|
||||
rpc RefreshSession(RefreshSessionRequest) returns (RefreshSessionResponse);
|
||||
}
|
||||
|
||||
message GetSessionRequest {
|
||||
@@ -14,3 +15,11 @@ message GetSessionResponse {
|
||||
string session_id = 1;
|
||||
string user_id = 2;
|
||||
}
|
||||
|
||||
message RefreshSessionRequest {
|
||||
string session_id = 1;
|
||||
}
|
||||
|
||||
message RefreshSessionResponse {
|
||||
bool valid = 1;
|
||||
}
|
||||
Reference in New Issue
Block a user