Files
imks/socket/session_store/memory.rs
T
zhenyi 06e8ee96a5 feat(auth): add authentication protocol definitions and build configuration
- Add TokenClaims message for JWT payload structure with user id, issuer, timestamps, and scopes
- Implement IssueTokenRequest/Response for creating access and refresh tokens with TTL support
- Create RefreshTokenRequest/Response for token rotation functionality
- Define RevokeTokenRequest/Response with support for single token or user-wide revocation
- Add VerifyTokenRequest/Response for validating JWT tokens with detailed claims information
- Implement signing key distribution system with GetSigningKeysRequest/Response
- Create TokenService gRPC service with IssueToken, RefreshToken, RevokeToken, VerifyToken, and GetSigningKeys methods
- Add build.rs configuration to compile proto files using tonic_prost_build
- Include channel, channel_settings, member, and permission protocol definitions for IM services
- Generate Rust code bindings through pb/core.rs and pb/im.rs modules
2026-06-10 23:45:40 +08:00

88 lines
2.5 KiB
Rust

use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use async_trait::async_trait;
use dashmap::DashMap;
use crate::socket::session_store::{SessionError, SessionInfo, SessionStoreTrait};
pub struct InMemorySessionStore {
sessions: Arc<DashMap<String, SessionInfo>>,
}
impl InMemorySessionStore {
pub fn new() -> Self {
Self {
sessions: Arc::new(DashMap::new()),
}
}
}
impl Default for InMemorySessionStore {
fn default() -> Self {
Self::new()
}
}
fn now_millis() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64
}
#[async_trait]
impl SessionStoreTrait for InMemorySessionStore {
async fn create(&self, sid: &str, transport: &str, server_id: &str) -> Result<(), SessionError> {
let info = SessionInfo {
sid: sid.to_string(),
transport: transport.to_string(),
state: "connecting".to_string(),
server_id: server_id.to_string(),
created_at: now_millis(),
last_ping: now_millis(),
};
self.sessions.insert(sid.to_string(), info);
Ok(())
}
async fn get(&self, sid: &str) -> Result<Option<SessionInfo>, SessionError> {
Ok(self.sessions.get(sid).map(|r| r.value().clone()))
}
async fn set_state(&self, sid: &str, state: &str) -> Result<(), SessionError> {
if let Some(mut entry) = self.sessions.get_mut(sid) {
entry.value_mut().state = state.to_string();
Ok(())
} else {
Err(SessionError::NotFound(sid.to_string()))
}
}
async fn set_transport(&self, sid: &str, transport: &str) -> Result<(), SessionError> {
if let Some(mut entry) = self.sessions.get_mut(sid) {
entry.value_mut().transport = transport.to_string();
Ok(())
} else {
Err(SessionError::NotFound(sid.to_string()))
}
}
async fn update_ping(&self, sid: &str) -> Result<(), SessionError> {
if let Some(mut entry) = self.sessions.get_mut(sid) {
entry.value_mut().last_ping = now_millis();
Ok(())
} else {
Err(SessionError::NotFound(sid.to_string()))
}
}
async fn remove(&self, sid: &str) -> Result<(), SessionError> {
self.sessions.remove(sid);
Ok(())
}
async fn exists(&self, sid: &str) -> Result<bool, SessionError> {
Ok(self.sessions.contains_key(sid))
}
}