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,31 @@
|
||||
pub mod redis;
|
||||
pub mod nats;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use thiserror::Error;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum MessageBusError {
|
||||
#[error("Redis error: {0}")]
|
||||
Redis(String),
|
||||
#[error("NATS error: {0}")]
|
||||
Nats(String),
|
||||
#[error("Connection closed")]
|
||||
ConnectionClosed,
|
||||
#[error("Channel not found: {0}")]
|
||||
ChannelNotFound(String),
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(String),
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait MessageBus: Send + Sync + 'static {
|
||||
async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), MessageBusError>;
|
||||
async fn subscribe(&self, channel: &str) -> Result<mpsc::Receiver<Vec<u8>>, MessageBusError>;
|
||||
async fn unsubscribe(&self, channel: &str) -> Result<(), MessageBusError>;
|
||||
async fn close(&self) -> Result<(), MessageBusError>;
|
||||
}
|
||||
|
||||
pub use redis::RedisMessageBus;
|
||||
pub use nats::NatsMessageBus;
|
||||
@@ -0,0 +1,88 @@
|
||||
use async_trait::async_trait;
|
||||
use dashmap::DashMap;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
use crate::socket::message_bus::{MessageBus, MessageBusError};
|
||||
|
||||
pub struct NatsMessageBus {
|
||||
client: async_nats::Client,
|
||||
shutdowns: DashMap<String, watch::Sender<bool>>,
|
||||
}
|
||||
|
||||
impl NatsMessageBus {
|
||||
pub async fn new(nats_url: &str) -> Result<Self, MessageBusError> {
|
||||
let client = async_nats::connect(nats_url)
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Nats(e.to_string()))?;
|
||||
Ok(Self {
|
||||
client,
|
||||
shutdowns: DashMap::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MessageBus for NatsMessageBus {
|
||||
async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), MessageBusError> {
|
||||
self.client
|
||||
.publish(channel.to_string(), message.to_vec().into())
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Nats(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn subscribe(&self, channel: &str) -> Result<mpsc::Receiver<Vec<u8>>, MessageBusError> {
|
||||
let (tx, rx) = mpsc::channel::<Vec<u8>>(256);
|
||||
|
||||
let mut subscriber = self.client
|
||||
.subscribe(channel.to_string())
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Nats(e.to_string()))?;
|
||||
|
||||
let (shutdown_tx, mut shutdown_rx) = watch::channel(false);
|
||||
self.shutdowns.insert(channel.to_string(), shutdown_tx);
|
||||
|
||||
tokio::spawn(async move {
|
||||
use futures_util::StreamExt;
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown_rx.changed() => {
|
||||
break;
|
||||
}
|
||||
message = subscriber.next() => {
|
||||
match message {
|
||||
Some(msg) => {
|
||||
let data = msg.payload.to_vec();
|
||||
if tx.send(data).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(e) = subscriber.unsubscribe().await {
|
||||
tracing::warn!("NATS unsubscribe error: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
async fn unsubscribe(&self, channel: &str) -> Result<(), MessageBusError> {
|
||||
if let Some((_, tx)) = self.shutdowns.remove(channel) {
|
||||
let _ = tx.send(true);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), MessageBusError> {
|
||||
// Signal all subscribers to shutdown
|
||||
self.shutdowns.iter().for_each(|entry| {
|
||||
let _ = entry.value().send(true);
|
||||
});
|
||||
self.shutdowns.clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
use async_trait::async_trait;
|
||||
use fred::clients::{Client, SubscriberClient};
|
||||
use fred::interfaces::{ClientLike, EventInterface, PubsubInterface};
|
||||
use fred::prelude::*;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::socket::message_bus::{MessageBus, MessageBusError};
|
||||
|
||||
pub struct RedisMessageBus {
|
||||
client: Client,
|
||||
subscriber: SubscriberClient,
|
||||
}
|
||||
|
||||
impl RedisMessageBus {
|
||||
pub async fn new(redis_url: &str) -> Result<Self, MessageBusError> {
|
||||
let config = Config::from_url(redis_url)
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
|
||||
let client = Client::new(config.clone(), None, None, None);
|
||||
let subscriber = SubscriberClient::new(config, None, None, None);
|
||||
|
||||
// connect() starts the connection task; result is checked by wait_for_connect()
|
||||
let _ = client.connect().await;
|
||||
let _ = subscriber.connect().await;
|
||||
|
||||
client
|
||||
.wait_for_connect()
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
subscriber
|
||||
.wait_for_connect()
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
|
||||
Ok(Self { client, subscriber })
|
||||
}
|
||||
|
||||
pub fn client(&self) -> &Client {
|
||||
&self.client
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MessageBus for RedisMessageBus {
|
||||
async fn publish(&self, channel: &str, message: &[u8]) -> Result<(), MessageBusError> {
|
||||
self.client
|
||||
.publish::<(), _, Vec<u8>>(channel, message.to_vec())
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn subscribe(&self, channel: &str) -> Result<mpsc::Receiver<Vec<u8>>, MessageBusError> {
|
||||
let (tx, rx) = mpsc::channel::<Vec<u8>>(256);
|
||||
|
||||
self.subscriber
|
||||
.subscribe(channel.to_string())
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
|
||||
let subscriber = self.subscriber.clone();
|
||||
let channel_owned = channel.to_string();
|
||||
let mut message_rx = subscriber.message_rx();
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Ok(message) = message_rx.recv().await {
|
||||
if &message.channel == &channel_owned {
|
||||
let data: Vec<u8> = FromValue::from_value(message.value)
|
||||
.unwrap_or_default();
|
||||
if tx.send(data).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
async fn unsubscribe(&self, channel: &str) -> Result<(), MessageBusError> {
|
||||
self.subscriber
|
||||
.unsubscribe(channel.to_string())
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close(&self) -> Result<(), MessageBusError> {
|
||||
self.client
|
||||
.quit()
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
self.subscriber
|
||||
.quit()
|
||||
.await
|
||||
.map_err(|e| MessageBusError::Redis(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user