bazzar/crates/channels/src/tokens.rs

98 lines
2.4 KiB
Rust
Raw Normal View History

2022-12-20 15:34:20 +01:00
pub static CLIENT_NAME: &str = "tokens";
pub enum Topic {}
#[derive(Debug, Clone, thiserror::Error, serde::Serialize, serde::Deserialize)]
pub enum Error {}
pub mod create_pair {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {}
pub type Output = Result<Details, Error>;
}
pub mod validate {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {}
pub type Output = Result<Details, Error>;
}
pub mod refresh {
use model::RefreshTokenString;
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub refresh_token: RefreshTokenString,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {}
pub type Output = Result<Details, Error>;
}
pub mod rpc {
use config::SharedAppConfig;
use crate::tokens::{create_pair, refresh, validate};
#[tarpc::service]
pub trait Tokens {
/// Create new access token and refresh token without any validations
async fn create_pair(input: create_pair::Input) -> create_pair::Output;
/// Check if access token is valid
async fn validate(input: validate::Input) -> validate::Output;
/// Validate with refresh token and create new access token and refresh
/// token
async fn refresh(input: refresh::Input) -> refresh::Output;
}
pub async fn create_client(config: SharedAppConfig) -> TokensClient {
use tarpc::client;
use tarpc::tokio_serde::formats::Bincode;
let addr = {
let l = config.lock();
(l.tokens().rpc_bind.clone(), l.tokens().rpc_port)
};
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default);
let client = TokensClient::new(
client::Config::default(),
transport.await.expect("Failed to connect to server"),
)
.spawn();
client
}
}
pub mod mqtt {
use config::SharedAppConfig;
use rumqttc::EventLoop;
use super::CLIENT_NAME;
use crate::AsyncClient;
pub fn create_client(config: SharedAppConfig) -> (AsyncClient, EventLoop) {
crate::mqtt::create_client(CLIENT_NAME, config.lock().tokens().mqtt_addr())
}
}