diff --git a/services/identity/Cargo.lock b/services/identity/Cargo.lock --- a/services/identity/Cargo.lock +++ b/services/identity/Cargo.lock @@ -1278,6 +1278,7 @@ "derive_more", "futures-core", "hex", + "hyper", "once_cell", "opaque-ke 1.2.0", "prost", @@ -1287,6 +1288,7 @@ "tokio-stream", "tonic", "tonic-build", + "tonic-web", "tracing", "tracing-subscriber", ] @@ -2309,6 +2311,26 @@ "syn", ] +[[package]] +name = "tonic-web" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9213351ad53b0dcf1c9cf7c372a47533446b1114928a9177bedc6c551e14b7cf" +dependencies = [ + "base64", + "bytes", + "futures-core", + "http", + "http-body", + "hyper", + "pin-project", + "tonic", + "tower-http", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower" version = "0.4.13" diff --git a/services/identity/Cargo.toml b/services/identity/Cargo.toml --- a/services/identity/Cargo.toml +++ b/services/identity/Cargo.toml @@ -28,6 +28,8 @@ comm-opaque2 = { path = "../../shared/comm-opaque2" } once_cell = "1.17" hex = "0.4" +tonic-web = "0.5" +hyper = "0.14" [build-dependencies] tonic-build = "0.8" diff --git a/services/identity/src/main.rs b/services/identity/src/main.rs --- a/services/identity/src/main.rs +++ b/services/identity/src/main.rs @@ -13,12 +13,14 @@ mod nonce; mod pake_grpc; mod service; +mod service_wrapper; mod token; use config::load_config; use constants::{IDENTITY_SERVICE_SOCKET_ADDR, SECRETS_DIRECTORY}; use keygen::generate_and_persist_keypair; use service::{IdentityKeyserverServiceServer, MyIdentityService}; +use service_wrapper::NamedClientServiceWrapper; use tracing::info; use client_service::{ClientService, IdentityClientServiceServer}; @@ -62,12 +64,15 @@ let server = MyIdentityService::new(database_client.clone()); let keyserver_service = IdentityKeyserverServiceServer::with_interceptor(server, check_auth); - let client_server = + let client_service = IdentityClientServiceServer::new(ClientService::new(database_client)); info!("Listening to gRPC traffic on {}", addr); Server::builder() + .accept_http1(true) .add_service(keyserver_service) - .add_service(client_server) + .add_service(NamedClientServiceWrapper::new(tonic_web::enable( + client_service, + ))) .serve(addr) .await?; } diff --git a/services/identity/src/service_wrapper.rs b/services/identity/src/service_wrapper.rs new file mode 100644 --- /dev/null +++ b/services/identity/src/service_wrapper.rs @@ -0,0 +1,44 @@ +use std::convert::Infallible; +use std::task::{Context, Poll}; + +use hyper::{Request, Response}; +use tonic::body::BoxBody; +use tonic::codegen::Service; +use tonic::server::NamedService; +use tonic::transport::Body; + +#[derive(Clone)] +pub struct NamedClientServiceWrapper(S); +impl NamedClientServiceWrapper { + pub fn new(service: S) -> Self { + Self(service) + } +} + +impl Service> for NamedClientServiceWrapper +where + S: Service, Response = Response, Error = Infallible> + + Clone + + Send + + 'static, + S::Future: Send + 'static, +{ + type Response = S::Response; + type Error = S::Error; + type Future = S::Future; + + fn poll_ready( + &mut self, + cx: &mut Context<'_>, + ) -> Poll> { + self.0.poll_ready(cx) + } + + fn call(&mut self, req: Request) -> Self::Future { + self.0.call(req) + } +} + +impl NamedService for NamedClientServiceWrapper { + const NAME: &'static str = "identity.client"; +}