2020-11-17 14:35:16 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-11-17 14:38:11 +01:00
|
|
|
use capnp::capability::{Params, Results, Promise};
|
2020-11-17 14:35:16 +01:00
|
|
|
|
|
|
|
use crate::schema::connection_capnp;
|
|
|
|
use crate::connection::Session;
|
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
use crate::db::Databases;
|
2021-09-18 17:01:35 +02:00
|
|
|
use crate::db::user::UserId;
|
2020-11-20 13:06:55 +01:00
|
|
|
|
2020-12-15 13:12:22 +01:00
|
|
|
use crate::network::Network;
|
|
|
|
|
2020-11-17 14:35:16 +01:00
|
|
|
pub mod auth;
|
2020-11-17 14:15:29 +01:00
|
|
|
mod machine;
|
|
|
|
mod machines;
|
2020-11-17 14:35:16 +01:00
|
|
|
|
|
|
|
use machines::Machines;
|
|
|
|
|
2020-12-09 18:44:52 +01:00
|
|
|
// TODO Session restoration by making the Bootstrap cap a SturdyRef
|
2020-11-17 14:35:16 +01:00
|
|
|
pub struct Bootstrap {
|
2020-11-20 13:06:55 +01:00
|
|
|
session: Arc<Session>,
|
|
|
|
db: Databases,
|
2020-12-15 13:12:22 +01:00
|
|
|
nw: Arc<Network>,
|
2020-11-17 14:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bootstrap {
|
2020-12-15 13:12:22 +01:00
|
|
|
pub fn new(session: Arc<Session>, db: Databases, nw: Arc<Network>) -> Self {
|
2020-11-19 14:53:14 +01:00
|
|
|
info!(session.log, "Created Bootstrap");
|
2020-12-15 13:12:22 +01:00
|
|
|
Self { session, db, nw }
|
2020-11-17 14:35:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use connection_capnp::bootstrap::*;
|
|
|
|
impl connection_capnp::bootstrap::Server for Bootstrap {
|
2021-09-09 21:50:11 +02:00
|
|
|
fn authentication_system(&mut self,
|
|
|
|
_: AuthenticationSystemParams,
|
|
|
|
mut res: AuthenticationSystemResults
|
2020-11-17 14:35:16 +01:00
|
|
|
) -> Promise<(), capnp::Error> {
|
2020-12-09 18:44:52 +01:00
|
|
|
// TODO: Forbid mutltiple authentication for now
|
2020-11-17 14:35:16 +01:00
|
|
|
// TODO: When should we allow multiple auth and how do me make sure that does not leak
|
|
|
|
// priviledges (e.g. due to previously issues caps)?
|
2020-11-24 15:57:23 +01:00
|
|
|
|
2021-09-09 21:50:11 +02:00
|
|
|
res.get().set_authentication_system(capnp_rpc::new_client(auth::Auth::new(self.db.clone(), self.session.clone())));
|
2020-11-24 15:57:23 +01:00
|
|
|
|
|
|
|
Promise::ok(())
|
2020-11-17 14:35:16 +01:00
|
|
|
}
|
|
|
|
|
2021-09-09 21:50:11 +02:00
|
|
|
fn permission_system(&mut self,
|
|
|
|
_: PermissionSystemParams,
|
|
|
|
_: PermissionSystemResults
|
2020-11-17 14:35:16 +01:00
|
|
|
) -> Promise<(), capnp::Error> {
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:50:11 +02:00
|
|
|
fn machine_system(&mut self,
|
|
|
|
_: MachineSystemParams,
|
|
|
|
mut res: MachineSystemResults
|
2020-11-17 14:35:16 +01:00
|
|
|
) -> Promise<(), capnp::Error> {
|
2021-09-18 17:01:35 +02:00
|
|
|
let session = self.session.clone();
|
|
|
|
let accessdb = self.db.access.clone();
|
|
|
|
let nw = self.nw.clone();
|
|
|
|
let f = async move {
|
|
|
|
// Ensure the lock is dropped as soon as possible
|
|
|
|
if let Some(user) = { session.user.lock().await.clone() } {
|
|
|
|
let perms = accessdb.collect_permrules(&user.data)
|
|
|
|
.map_err(|e| capnp::Error::failed(format!("AccessDB lookup failed: {}", e)))?;
|
2020-11-17 14:35:16 +01:00
|
|
|
|
2021-09-18 17:01:35 +02:00
|
|
|
// TODO actual permission check and stuff
|
|
|
|
// Right now we only check that the user has authenticated at all.
|
|
|
|
let c = capnp_rpc::new_client(Machines::new(user.id, perms, nw));
|
|
|
|
res.get().set_machine_system(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Promise is Ok either way, just the machine system may not be set, indicating as
|
|
|
|
// usual a lack of permission.
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
Promise::from_future(f)
|
2020-11-17 14:35:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|