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
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::socket::packet::Packet;
|
||||
|
||||
pub struct Socket {
|
||||
pub sid: String,
|
||||
pub namespace: String,
|
||||
pub engine_sid: String,
|
||||
ack_id: AtomicU64,
|
||||
tx: mpsc::Sender<Packet>,
|
||||
}
|
||||
|
||||
impl Socket {
|
||||
pub fn new(
|
||||
sid: String,
|
||||
namespace: String,
|
||||
engine_sid: String,
|
||||
tx: mpsc::Sender<Packet>,
|
||||
) -> Self {
|
||||
Self {
|
||||
sid,
|
||||
namespace,
|
||||
engine_sid,
|
||||
ack_id: AtomicU64::new(0),
|
||||
tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_ack_id(&self) -> u64 {
|
||||
self.ack_id.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub fn send_packet(&self, packet: &Packet) -> Result<(), mpsc::error::TrySendError<Packet>> {
|
||||
self.tx.try_send(packet.clone())
|
||||
}
|
||||
|
||||
pub fn emit(&self, event: impl Into<String>, data: serde_json::Value) -> Result<(), mpsc::error::TrySendError<Packet>> {
|
||||
let packet = Packet::event(
|
||||
&self.namespace,
|
||||
serde_json::json!([event.into(), data]),
|
||||
None,
|
||||
);
|
||||
self.send_packet(&packet)
|
||||
}
|
||||
|
||||
pub fn emit_with_ack(
|
||||
&self,
|
||||
event: impl Into<String>,
|
||||
data: serde_json::Value,
|
||||
) -> Result<u64, mpsc::error::TrySendError<Packet>> {
|
||||
let ack_id = self.next_ack_id();
|
||||
let packet = Packet::event(
|
||||
&self.namespace,
|
||||
serde_json::json!([event.into(), data]),
|
||||
Some(ack_id),
|
||||
);
|
||||
self.send_packet(&packet)?;
|
||||
Ok(ack_id)
|
||||
}
|
||||
|
||||
pub fn disconnect(&self) -> Result<(), mpsc::error::TrySendError<Packet>> {
|
||||
let packet = Packet::disconnect(&self.namespace);
|
||||
self.send_packet(&packet)
|
||||
}
|
||||
|
||||
pub fn send_ack(&self, id: u64, data: serde_json::Value) -> Result<(), mpsc::error::TrySendError<Packet>> {
|
||||
let packet = Packet::ack(&self.namespace, data, id);
|
||||
self.send_packet(&packet)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user