fabaccess-bffh/bffhd/capnp/connection.rs

171 lines
4.8 KiB
Rust
Raw Permalink 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;
2022-06-24 13:57:47 +02:00
use std::fmt;
use std::fmt::{Formatter, Write};
2022-05-05 15:50:44 +02:00
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;
use tracing::Span;
/// 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,
span: Span,
2022-03-12 17:31:53 +01:00
}
impl BootCap {
2022-05-05 15:50:44 +02:00
pub fn new(
peer_addr: SocketAddr,
authentication: AuthenticationHandle,
sessionmanager: SessionManager,
span: Span,
2022-05-05 15:50:44 +02:00
) -> Self {
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,
span,
2022-03-12 17:31:53 +01:00
}
}
}
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-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: "bffh::api",
"Bootstrap",
method = "getAPIVersion",
)
.entered();
tracing::trace!("method call");
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-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: "bffh::api",
"Bootstrap",
method = "getServerRelease",
)
.entered();
tracing::trace!("method call");
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-06-24 13:57:47 +02:00
tracing::trace!(
results.name = "bffhd",
results.release = crate::env::VERSION,
"method return"
);
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-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: "bffh::api",
"mechanisms",
)
.entered();
tracing::trace!(target: "bffh::api", "method call");
2022-03-15 19:56:41 +01:00
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);
}
2022-06-24 13:57:47 +02:00
struct DisMechs<'a>(Vec<&'a str>);
impl fmt::Display for DisMechs<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_char('[')?;
let mut first = true;
for mechanism in self.0.iter() {
if first {
first = false;
f.write_str(mechanism)?;
} else {
f.write_str(" ,")?;
f.write_str(mechanism)?;
}
}
f.write_char(']')?;
Ok(())
}
}
tracing::trace!(
results.mechs = %DisMechs(mechs),
"method return"
);
2022-03-12 17:31:53 +01:00
Promise::ok(())
}
fn create_session(
&mut self,
params: bootstrap::CreateSessionParams,
mut result: bootstrap::CreateSessionResults,
) -> Promise<(), ::capnp::Error> {
2022-06-24 13:57:47 +02:00
let _guard = self.span.enter();
let _span = tracing::trace_span!(
target: "bffh::api",
"createSession",
)
.entered();
2022-03-15 19:56:41 +01:00
2022-03-12 17:31:53 +01:00
let params = pry!(params.get());
let mechanism: &str = pry!(params.get_mechanism());
2022-06-24 13:57:47 +02:00
tracing::trace!(params.mechanism = mechanism, "method call");
2022-03-15 19:56:41 +01:00
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(&self.span, mechname, session, self.sessionmanager.clone())
2022-03-12 17:31:53 +01:00
} else {
Authentication::invalid_mechanism()
}
} else {
Authentication::invalid_mechanism()
};
2022-06-24 13:57:47 +02:00
tracing::trace!(
results.authentication = %auth,
"method return"
);
2022-03-12 17:31:53 +01:00
let mut builder = result.get();
builder.set_authentication(capnp_rpc::new_client(auth));
Promise::ok(())
}
}