bffh/src/api.rs

110 lines
3.6 KiB
Rust
Raw Normal View History

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;
2021-09-19 22:53:43 +02:00
mod user;
2021-09-19 19:47:29 +02:00
mod users;
use users::Users;
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 {
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 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-19 19:47:29 +02:00
debug!(session.log, "Giving MachineSystem cap to user {} with perms:", user.id);
for r in perms.iter() {
debug!(session.log, " {}", r);
}
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
}
2021-09-19 19:47:29 +02:00
fn user_system(
&mut self,
_: UserSystemParams,
mut results: UserSystemResults
) -> Promise<(), capnp::Error> {
let session = self.session.clone();
let accessdb = self.db.access.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)))?;
// TODO actual permission check and stuff
// Right now we only check that the user has authenticated at all.
let c = capnp_rpc::new_client(Users::new(perms));
results.get().set_user_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)
}
}