Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 98 additions & 24 deletions crates/kharond/src/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1896,9 +1896,16 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
/// `locked`); an unknown item is `not_found` and a target the
/// broker cannot observe is `target_not_owned` — both as
/// [`GrantRequestBody`] states with no request created, mirroring
/// how fills report refusals in-band. A created request is
/// audited before the reply (REQ-AUD-003). Bounded by the fill
/// deadline from `received`, like fills and context calls.
/// how fills report refusals in-band. After the live origin is
/// observed, a matching live grant for the same agent, item,
/// origin and action, with uses left and a revision pin the item
/// admits (REQ-REV-004), is returned as `granted` with its
/// `grant_id`: no request is created and no notification is sent,
/// audited as `grant_request` with result `existing_grant` (#418).
/// The search is the one a fill admits with, so an older pin does
/// not hide a later grant. A created request is audited before
/// the reply (REQ-AUD-003). Bounded by the fill deadline from
/// `received`, like fills and context calls.
pub async fn request_grant(
&self,
agent: &AgentId,
Expand Down Expand Up @@ -1984,6 +1991,65 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
"target_not_owned",
)));
};
// #418: a live grant that already covers this (agent, item,
// observed origin, action) with uses left, and whose revision
// pin admits the item, is returned as-is. The search applies
// both predicates together, the same way `resolve_fill` does:
// a stale pin earlier in the store must not hide a later grant
// that pins the current revision, and the lookup spends no use.
// Claim mismatches still go through create_request so
// REQ-APR-007's origin_mismatch refusal is unchanged.
let claim_ok = super::grant::serialized_origin(&args.expected_origin)
== super::grant::serialized_origin(&observed);
if claim_ok {
let now = self.grant_now();
let existing = {
let (collections, revision) = {
let mut lock = self.lock.lock().unwrap_or_else(|p| p.into_inner());
match lock.require_unlocked() {
Ok(session) => match session.vault().get(&args.item_id) {
Ok(item) => (
item.collection_ids().to_vec(),
item.revision().map(str::to_owned),
),
Err(_) => {
drop(lock);
self.append(base("not_found"))?;
return Ok(Outcome::GrantRequest(GrantRequestBody::refused(
"not_found",
)));
}
},
Err(_) => {
drop(lock);
self.append(base("locked"))?;
return Err(ApiError::LOCKED);
}
}
};
self.grants
.lock()
.unwrap_or_else(|p| p.into_inner())
.admitting_grant(
agent.as_str(),
&args.item_id,
&collections,
&observed,
args.action,
now,
revision.as_deref(),
)
.cloned()
};
if let Some(grant) = existing {
self.append(
base("existing_grant")
.origin(observed)
.grant_id(grant.id.clone()),
)?;
return Ok(Outcome::GrantRequest(GrantRequestBody::existing(&grant)));
}
}
let (greq, created) = self
.grants
.lock()
Expand Down Expand Up @@ -3145,22 +3211,30 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
FillOperation::FillTotp => GrantAction::Totp,
};
let admits = |g: &Grant| {
g.matches(
g.admits(
agent.as_str(),
&req.item_id,
&collections,
&req.expected_origin,
action,
now,
) && g.revision_matches(revision.as_deref())
revision.as_deref(),
)
};
let store = self.grants.lock().unwrap_or_else(|p| p.into_inner());
let chosen = match &req.grant_id {
Some(gid) => store.grant(gid).filter(|g| admits(g)).cloned(),
None => store
.grants_for(agent.as_str(), now)
.into_iter()
.find(|g| admits(g)),
.admitting_grant(
agent.as_str(),
&req.item_id,
&collections,
&req.expected_origin,
action,
now,
revision.as_deref(),
)
.cloned(),
};
match chosen {
Some(g) if g.kind == crate::config::GrantKind::Once => FillBacking::OnceGrant,
Expand Down Expand Up @@ -3246,14 +3320,15 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
Some(gid) => {
let rev = item.revision();
let ok = store.grant(gid).is_some_and(|g| {
g.matches(
g.admits(
agent.as_str(),
&req.item_id,
item.collection_ids(),
&req.expected_origin,
action,
now,
) && g.revision_matches(rev)
rev,
)
}) && store.consume(gid, now);
if !ok {
return refuse(FillStatus::GrantDenied);
Expand All @@ -3264,19 +3339,16 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
None => {
let rev = item.revision();
let found = store
.grants_for(agent.as_str(), now)
.into_iter()
.find(|g| {
g.matches(
agent.as_str(),
&req.item_id,
item.collection_ids(),
&req.expected_origin,
action,
now,
) && g.revision_matches(rev)
})
.map(|g| g.id);
.admitting_grant(
agent.as_str(),
&req.item_id,
item.collection_ids(),
&req.expected_origin,
action,
now,
rev,
)
.map(|g| g.id.clone());
match found {
Some(gid) => {
let consumed = store.consume(&gid, now);
Expand All @@ -3287,7 +3359,9 @@ impl<S: VaultView, C: Clock> Broker<S, C> {
None => {
// A live grant that fails only on revision
// is denied (re-approval), not a fresh
// request (REQ-REV-004).
// request (REQ-REV-004). `admitting_grant`
// already found nothing, so the first
// dimension match, if any, fails the pin.
let revision_miss = store
.matching_grant(
agent.as_str(),
Expand Down
Loading