fabaccess-bffh/bffhd/capnp/connection.rs

113 lines
3.3 KiB
Rust
Raw Normal View History

2022-03-12 17:31:53 +01:00
use api::connection_capnp::bootstrap;
2022-05-05 15:50:44 +02:00
pub use api::connection_capnp::bootstrap::Client;
use std::net::SocketAddr;
2022-03-12 17:31:53 +01:00
use crate::authentication::AuthenticationHandle;
use crate::capnp::authenticationsystem::Authentication;
use crate::session::SessionManager;
2022-05-05 15:50:44 +02:00
use capnp::capability::Promise;
use capnp_rpc::pry;
use rsasl::mechname::Mechname;
/// Cap'n Proto API Handler
2022-03-12 17:31:53 +01:00
pub struct BootCap {
2022-03-15 19:56:41 +01:00
peer_addr: SocketAddr,
2022-03-12 17:31:53 +01:00
authentication: AuthenticationHandle,
sessionmanager: SessionManager,
}
impl BootCap {
2022-05-05 15:50:44 +02:00
pub fn new(
peer_addr: SocketAddr,
authentication: AuthenticationHandle,
sessionmanager: SessionManager,
) -> Self {
2022-03-15 19:56:41 +01:00
tracing::trace!(%peer_addr, "bootstrapping RPC");
2022-03-12 17:31:53 +01:00
Self {
2022-03-15 19:56:41 +01:00
peer_addr,
2022-03-12 17:31:53 +01:00
authentication,
sessionmanager,
}
}
}
2022-03-12 17:31:53 +01:00
impl bootstrap::Server for BootCap {
fn get_a_p_i_version(
&mut self,
_: bootstrap::GetAPIVersionParams,
_: bootstrap::GetAPIVersionResults,
) -> Promise<(), ::capnp::Error> {
2022-03-15 19:56:41 +01:00
let span = tracing::trace_span!("get_api_version", peer_addr=%self.peer_addr);
let _guard = span.enter();
2022-03-12 17:31:53 +01:00
Promise::ok(())
}
fn get_server_release(
&mut self,
_: bootstrap::GetServerReleaseParams,
mut result: bootstrap::GetServerReleaseResults,
) -> Promise<(), ::capnp::Error> {
2022-03-15 19:56:41 +01:00
let span = tracing::trace_span!("get_server_release", peer_addr=%self.peer_addr);
let _guard = span.enter();
2022-03-12 17:31:53 +01:00
let mut builder = result.get();
builder.set_name("bffhd");
2022-05-14 15:36:32 +02:00
builder.set_release(crate::env::VERSION);
2022-03-12 17:31:53 +01:00
Promise::ok(())
}
fn mechanisms(
&mut self,
2022-03-15 20:00:43 +01:00
_params: bootstrap::MechanismsParams,
2022-03-12 17:31:53 +01:00
mut result: bootstrap::MechanismsResults,
) -> Promise<(), ::capnp::Error> {
2022-03-15 19:56:41 +01:00
let span = tracing::trace_span!("mechanisms", peer_addr=%self.peer_addr);
let _guard = span.enter();
tracing::trace!("mechanisms");
2022-03-15 20:00:43 +01:00
let builder = result.get();
2022-05-05 15:50:44 +02:00
let mechs: Vec<_> = self
.authentication
.list_available_mechs()
2022-03-12 17:31:53 +01:00
.into_iter()
.map(|m| m.as_str())
.collect();
let mut mechbuilder = builder.init_mechs(mechs.len() as u32);
2022-05-05 15:50:44 +02:00
for (i, m) in mechs.iter().enumerate() {
2022-03-12 17:31:53 +01:00
mechbuilder.set(i as u32, m);
}
Promise::ok(())
}
fn create_session(
&mut self,
params: bootstrap::CreateSessionParams,
mut result: bootstrap::CreateSessionResults,
) -> Promise<(), ::capnp::Error> {
2022-03-15 19:56:41 +01:00
let span = tracing::trace_span!("create_session", peer_addr=%self.peer_addr);
let _guard = span.enter();
2022-03-12 17:31:53 +01:00
let params = pry!(params.get());
let mechanism: &str = pry!(params.get_mechanism());
2022-03-15 19:56:41 +01:00
tracing::trace!(mechanism);
2022-03-12 17:31:53 +01:00
let mechname = Mechname::new(mechanism.as_bytes());
let auth = if let Ok(mechname) = mechname {
if let Ok(session) = self.authentication.start(mechname) {
Authentication::new(session, self.sessionmanager.clone())
} else {
Authentication::invalid_mechanism()
}
} else {
Authentication::invalid_mechanism()
};
let mut builder = result.get();
builder.set_authentication(capnp_rpc::new_client(auth));
Promise::ok(())
}
}