fabaccess-bffh/src/api.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

2020-02-14 12:20:17 +01:00
// module needs to be top level for generated functions to be in scope:
// https://github.com/capnproto/capnproto-rust/issues/16
2020-02-17 03:44:02 +01:00
pub mod api {
2020-02-14 12:20:17 +01:00
include!(concat!(env!("OUT_DIR"), "/schema/api_capnp.rs"));
}
use std::default::Default;
use async_std::net::TcpStream;
use futures_signals::signal::Mutable;
use casbin::Enforcer;
use casbin::MgmtApi;
2020-02-17 03:44:02 +01:00
use crate::machine::Machines;
use crate::auth::Authentication;
2020-02-17 03:44:02 +01:00
use crate::access::Permissions;
2020-02-17 00:50:42 +01:00
use capnp::{Error};
2020-02-17 03:44:02 +01:00
use capnp::capability::Promise;
2020-02-17 00:50:42 +01:00
use capnp_rpc::RpcSystem;
use capnp_rpc::twoparty::VatNetwork;
use capnp_rpc::rpc_twoparty_capnp::Side;
use api::diflouroborane;
2020-02-14 12:20:17 +01:00
pub fn init() {
}
2020-02-17 03:44:02 +01:00
pub async fn process_socket(auth: Authentication, perm: Permissions, mach: Machines, socket: TcpStream)
2020-02-17 00:50:42 +01:00
-> Result<(), Error>
{
2020-02-17 03:44:02 +01:00
let api = Api { auth, perm, mach };
2020-02-17 00:50:42 +01:00
let a = api::diflouroborane::ToClient::new(api).into_client::<capnp_rpc::Server>();
let netw = VatNetwork::new(socket.clone(), socket, Side::Server, Default::default());
let rpc = RpcSystem::new(Box::new(netw), Some(a.clone().client));
2020-02-14 12:20:17 +01:00
rpc.await
}
struct Api {
2020-02-17 03:44:02 +01:00
auth: Authentication,
perm: Permissions,
mach: Machines,
2020-02-14 12:20:17 +01:00
}
2020-02-17 00:50:42 +01:00
impl diflouroborane::Server for Api {
2020-02-17 03:44:02 +01:00
fn authentication(&mut self,
_params: diflouroborane::AuthenticationParams,
mut results: diflouroborane::AuthenticationResults)
-> Promise<(), Error>
{
2020-02-17 03:44:02 +01:00
let mut b = results.get();
let auth = api::authentication::ToClient::new(self.auth.clone()).into_client::<capnp_rpc::Server>();
b.set_auth(auth);
Promise::ok(())
}
2020-02-17 03:44:02 +01:00
fn permissions(&mut self,
_params: diflouroborane::PermissionsParams,
mut results: diflouroborane::PermissionsResults)
-> Promise<(), Error>
{
2020-02-17 03:44:02 +01:00
let mut b = results.get();
let perm = api::permissions::ToClient::new(self.perm.clone()).into_client::<capnp_rpc::Server>();
b.set_perm(perm);
Promise::ok(())
}
2020-02-17 03:44:02 +01:00
fn machines(&mut self,
_params: diflouroborane::MachinesParams,
mut results: diflouroborane::MachinesResults)
-> Promise<(), Error>
{
let mut b = results.get();
2020-02-17 03:44:02 +01:00
let mach = api::machines::ToClient::new(self.mach.clone()).into_client::<capnp_rpc::Server>();
b.set_mach(mach);
Promise::ok(())
}
2020-02-14 12:20:17 +01:00
}