bazzar/crates/channels/src/tokens.rs

110 lines
2.8 KiB
Rust

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 model::{AccessToken, AccessTokenString, RefreshToken};
pub use model::{AccountId, Role};
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub customer_id: uuid::Uuid,
pub role: Role,
pub account_id: AccountId,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Details {
pub access_token: AccessToken,
pub access_token_string: AccessTokenString,
pub refresh_token: RefreshToken,
pub refresh_token_string: model::RefreshTokenString,
}
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())
}
}