Changeset View
Changeset View
Standalone View
Standalone View
keyserver/addons/rust-node-addon/src/identity_client/mod.rs
Show All 35 Lines | |||||
use napi::bindgen_prelude::*; | use napi::bindgen_prelude::*; | ||||
use opaque_ke::{ | use opaque_ke::{ | ||||
ClientLogin, ClientLoginFinishParameters, ClientLoginStartParameters, | ClientLogin, ClientLoginFinishParameters, ClientLoginStartParameters, | ||||
ClientLoginStartResult, ClientRegistration, | ClientLoginStartResult, ClientRegistration, | ||||
ClientRegistrationFinishParameters, CredentialFinalization, | ClientRegistrationFinishParameters, CredentialFinalization, | ||||
CredentialResponse, RegistrationResponse, RegistrationUpload, | CredentialResponse, RegistrationResponse, RegistrationUpload, | ||||
}; | }; | ||||
use rand::{rngs::OsRng, CryptoRng, Rng}; | use rand::{rngs::OsRng, CryptoRng, Rng}; | ||||
use serde::{Deserialize, Serialize}; | |||||
use std::collections::HashMap; | use std::collections::HashMap; | ||||
use std::env::var; | use std::env::var; | ||||
use tokio::sync::mpsc; | use tokio::sync::mpsc; | ||||
use tokio_stream::wrappers::ReceiverStream; | use tokio_stream::wrappers::ReceiverStream; | ||||
use tonic::{metadata::MetadataValue, transport::Channel, Request}; | use tonic::{metadata::MetadataValue, transport::Channel, Request}; | ||||
use tracing::{error, instrument}; | use tracing::{error, instrument}; | ||||
lazy_static! { | lazy_static! { | ||||
pub static ref IDENTITY_SERVICE_SOCKET_ADDR: String = | static ref IDENTITY_SERVICE_CONFIG: IdentityServiceConfig = { | ||||
var("COMM_IDENTITY_SERVICE_SOCKET_ADDR") | let config_json_string = | ||||
.unwrap_or_else(|_| "https://[::1]:50051".to_string()); | var("COMM_JSONCONFIG_secrets_identity_service_config"); | ||||
pub static ref AUTH_TOKEN: String = var("COMM_IDENTITY_SERVICE_AUTH_TOKEN") | match config_json_string { | ||||
.unwrap_or_else(|_| "test".to_string()); | Ok(json) => serde_json::from_str(&json).unwrap(), | ||||
Err(_) => IdentityServiceConfig::default(), | |||||
} | |||||
}; | |||||
} | |||||
#[derive(Serialize, Deserialize)] | |||||
#[serde(rename_all = "camelCase")] | |||||
struct IdentityServiceConfig { | |||||
identity_socket_addr: String, | |||||
identity_auth_token: String, | |||||
} | |||||
impl Default for IdentityServiceConfig { | |||||
fn default() -> Self { | |||||
Self { | |||||
identity_socket_addr: "https://[::1]:50051".to_string(), | |||||
identity_auth_token: "test".to_string(), | |||||
} | |||||
} | |||||
} | } | ||||
fn handle_unexpected_response<T: std::fmt::Debug>(message: Option<T>) -> Error { | fn handle_unexpected_response<T: std::fmt::Debug>(message: Option<T>) -> Error { | ||||
error!("Received an unexpected message: {:?}", message); | error!("Received an unexpected message: {:?}", message); | ||||
Error::from_status(Status::GenericFailure) | Error::from_status(Status::GenericFailure) | ||||
} | } | ||||
async fn send_to_mpsc<T>(tx: mpsc::Sender<T>, request: T) -> Result<()> { | async fn send_to_mpsc<T>(tx: mpsc::Sender<T>, request: T) -> Result<()> { | ||||
Show All 36 Lines | client_login | ||||
.map_err(|e| { | .map_err(|e| { | ||||
error!("Failed to finish PAKE login: {}", e); | error!("Failed to finish PAKE login: {}", e); | ||||
Error::from_status(Status::GenericFailure) | Error::from_status(Status::GenericFailure) | ||||
}) | }) | ||||
.map(|res| res.message) | .map(|res| res.message) | ||||
} | } | ||||
async fn get_identity_service_channel() -> Result<Channel> { | async fn get_identity_service_channel() -> Result<Channel> { | ||||
Channel::from_static(&IDENTITY_SERVICE_SOCKET_ADDR) | Channel::from_static(&IDENTITY_SERVICE_CONFIG.identity_socket_addr) | ||||
.connect() | .connect() | ||||
.await | .await | ||||
.map_err(|_| { | .map_err(|_| { | ||||
Error::new( | Error::new( | ||||
Status::GenericFailure, | Status::GenericFailure, | ||||
"Unable to connect to identity service".to_string(), | "Unable to connect to identity service".to_string(), | ||||
) | ) | ||||
}) | }) | ||||
} | } |