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 {
|
2023-05-05 14:20:50 +02:00
|
|
|
use model::{AccessToken, AccessTokenString, RefreshToken};
|
2022-12-22 09:25:23 +01:00
|
|
|
pub use model::{AccountId, Role};
|
|
|
|
|
2022-12-20 15:34:20 +01:00
|
|
|
use super::Error;
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
2022-12-22 09:25:23 +01:00
|
|
|
pub struct Input {
|
|
|
|
pub customer_id: uuid::Uuid,
|
|
|
|
pub role: Role,
|
|
|
|
pub account_id: AccountId,
|
|
|
|
}
|
2022-12-20 15:34:20 +01:00
|
|
|
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
2022-12-22 09:25:23 +01:00
|
|
|
pub struct Details {
|
|
|
|
pub access_token: AccessToken,
|
|
|
|
pub access_token_string: AccessTokenString,
|
|
|
|
pub refresh_token: RefreshToken,
|
|
|
|
pub refresh_token_string: model::RefreshTokenString,
|
|
|
|
}
|
2022-12-20 15:34:20 +01:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|