use super::*;
use crate::sync::direct::board_grant::{BoardPin, Call};
use gent_protocol::dashboard::GrantOp;
use iroh::{EndpointAddr, EndpointId};
use uuid::Uuid;
impl StoreSession<'_> {
fn check_board_grant_peer(
&self,
peer: &str,
endpoint: EndpointId,
board: &BoardPin,
) -> Result<()> {
ensure!(
matches!(self.native_target(peer)?, Some(direct::NativeTarget::Advertised(addr)) if addr.id == endpoint),
"grant requester changed"
);
ensure!(
self.grant_pin(board.agent)? == *board
&& self.state.transport_host(&board.publisher)? == self.identity.node.id(),
"board carrier changed"
);
Ok(())
}
fn grant_pin(&self, agent: Uuid) -> Result<BoardPin> {
self.require_local_membership()?;
require_pro(self.home, &self.config, "Dashboard access")?;
let who = self.get_agent(&agent.to_string())?;
ensure!(!who.revoked, "board unavailable");
let member = self
.state
.state()
.nodes
.get(&who.node_id)
.context("publisher unavailable")?;
self.state
.authorize_node(&who.node_id, &member.signing_key, self.timestamp as u64)?;
Ok(BoardPin {
agent,
publisher: who.node_id,
checkpoint: self.state.checkpoint().state_hash,
})
}
}
impl Store {
pub(crate) fn board_grant_target(
&self,
agent: Uuid,
owner: Option<&str>,
) -> Result<(BoardPin, Option<EndpointAddr>)> {
if let Some(credential) = owner {
self.require_owner_credential(credential)?;
}
let tx = self.db.unchecked_transaction()?;
let mut s = self.network_session(&tx, self.identity.state(&tx)?);
s.timestamp = requests::diagnostic_clock(&tx, now())?;
if owner.is_some() {
s.authorize_dashboard_grant_admin(s.identity.node.id())?;
}
let pin = s.grant_pin(agent)?;
let peer = s.state.transport_host(&pin.publisher)?;
let address = if peer == s.identity.node.id() {
None
} else {
ensure!(
matches!(self.direct_settings()?.direct, config::Direct::Auto),
"board transport unavailable"
);
let Some(direct::NativeTarget::Advertised(address)) = s.native_target(peer)? else {
anyhow::bail!("board carrier unavailable")
};
Some(address)
};
Ok((pin, address))
}
pub(crate) fn check_board_grant_peer(
&self,
endpoint: EndpointId,
board: &BoardPin,
) -> Result<()> {
let (peer, _) = self.direct_binding(endpoint, true)?;
let tx = self.db.unchecked_transaction()?;
let mut s = self.network_session(&tx, self.identity.state(&tx)?);
s.timestamp = requests::diagnostic_clock(&tx, now())?;
s.check_board_grant_peer(&peer, endpoint, board)
}
pub(crate) fn serve_board_grant(
&mut self,
endpoint: EndpointId,
board: BoardPin,
call: Call,
) -> Result<Value> {
let (peer, _) = self.direct_binding(endpoint, true)?;
let mutation = !matches!(
call,
Call::Delegate {
action: GrantOp::Get,
..
}
);
let tx = self.db.unchecked_transaction()?;
let mut s = self.network_session(&tx, self.identity.state(&tx)?);
s.timestamp = if mutation {
requests::clock(&tx, now())?
} else {
requests::diagnostic_clock(&tx, now())?
};
s.check_board_grant_peer(&peer, endpoint, &board)?;
let id = board.agent.to_string();
let value = match call {
Call::Admin { action } => {
s.authorize_dashboard_grant_admin(&peer)?;
s.dashboard_grant(&id, action)?
}
Call::Delegate { secret, action } => s.dashboard_delegate(&id, &secret, action)?,
};
tx.commit()?;
if mutation {
self.sync_wakeup.notify_one();
}
Ok(value)
}
}