Skip to content

Commit

Permalink
Require Subject on get_chain
Browse files Browse the repository at this point in the history
  • Loading branch information
expede committed Mar 26, 2024
1 parent 96b0fe1 commit 94e946f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 45 deletions.
38 changes: 17 additions & 21 deletions src/delegation/agent.rs
Expand Up @@ -62,7 +62,7 @@ where
pub fn delegate(
&self,
audience: DID,
subject: Option<DID>,
subject: &DID,
via: Option<DID>,
command: String,
new_policy: Vec<Predicate>,
Expand All @@ -75,25 +75,21 @@ where
let mut salt = self.did.clone().to_string().into_bytes();
let nonce = Nonce::generate_12(&mut salt);

if let Some(ref sub) = subject {
if sub == &self.did {
let payload: Payload<DID> = Payload {
issuer: self.did.clone(),
audience,
subject,
via,
command,
metadata,
nonce,
expiration: expiration.into(),
not_before: not_before.map(Into::into),
policy: new_policy,
};

return Ok(
Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME")
);
}
if *subject == self.did {
let payload: Payload<DID> = Payload {
issuer: self.did.clone(),
audience,
subject: Some(subject.clone()),
via,
command,
metadata,
nonce,
expiration: expiration.into(),
not_before: not_before.map(Into::into),
policy: new_policy,
};

return Ok(Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME"));
}

let proofs = &self
Expand All @@ -109,7 +105,7 @@ where
let payload: Payload<DID> = Payload {
issuer: self.did.clone(),
audience,
subject,
subject: Some(subject.clone()),
via,
command,
policy,
Expand Down
32 changes: 14 additions & 18 deletions src/delegation/store/memory.rs
Expand Up @@ -198,11 +198,10 @@ where
Ok(())
}

// FIXME take a PayloadBuilder
fn get_chain(
&self,
aud: &DID,
subject: &Option<DID>,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
Expand All @@ -213,7 +212,10 @@ where
let read_tx = self.read();

let all_powerlines = read_tx.index.get(&None).unwrap_or(&blank_map);
let all_aud_for_subject = read_tx.index.get(subject).unwrap_or(&blank_map);
let all_aud_for_subject = read_tx
.index
.get(&Some(subject.clone()))
.unwrap_or(&blank_map);
let powerline_candidates = all_powerlines.get(aud).unwrap_or(&blank_set);
let sub_candidates = all_aud_for_subject.get(aud).unwrap_or(&blank_set);

Expand Down Expand Up @@ -411,13 +413,14 @@ mod tests {
#[test_log::test]
fn test_simple_fail() -> TestResult {
let (server, _server_signer) = gen_did();
let (nope, _nope_signer) = gen_did();

let store = MemoryStore::<
did::preset::Verifier,
varsig::header::Preset,
varsig::encoding::Preset,
>::default();
let got = store.get_chain(&server, &None, "/".into(), vec![], SystemTime::now())?;
let got = store.get_chain(&server, &nope, "/".into(), vec![], SystemTime::now())?;

pretty::assert_eq!(got, None);
Ok(())
Expand Down Expand Up @@ -449,7 +452,7 @@ mod tests {

store.insert(deleg.clone())?;

let got = store.get_chain(&bob, &Some(alice), "/".into(), vec![], SystemTime::now())?;
let got = store.get_chain(&bob, &alice, "/".into(), vec![], SystemTime::now())?;
pretty::assert_eq!(got, Some(nonempty![(deleg.cid()?, Arc::new(deleg))].into()));
Ok(())
}
Expand Down Expand Up @@ -509,7 +512,7 @@ mod tests {

store.insert(more_noise.clone())?;

let got = store.get_chain(&bob, &Some(alice), "/".into(), vec![], SystemTime::now())?;
let got = store.get_chain(&bob, &alice, "/".into(), vec![], SystemTime::now())?;
pretty::assert_eq!(got, Some(nonempty![(deleg.cid()?, Arc::new(deleg))].into()));
Ok(())
}
Expand Down Expand Up @@ -555,8 +558,7 @@ mod tests {

store.insert(deleg_2.clone())?;

let got =
store.get_chain(&carol, &Some(alice), "/".into(), vec![], SystemTime::now())?;
let got = store.get_chain(&carol, &alice, "/".into(), vec![], SystemTime::now())?;

pretty::assert_eq!(
got,
Expand Down Expand Up @@ -614,7 +616,7 @@ mod tests {

let got = store.get_chain(
&carol,
&Some(alice),
&alice,
"/test/me/now".into(),
vec![],
SystemTime::now(),
Expand Down Expand Up @@ -677,7 +679,7 @@ mod tests {

let got = store.get_chain(
&carol,
&Some(alice),
&alice,
"/test/me/now".into(),
vec![],
SystemTime::now(),
Expand Down Expand Up @@ -751,7 +753,7 @@ mod tests {
store.insert(alice_to_bob.clone())?;

let got: Vec<Cid> = store
.get_chain(&dave, &Some(alice), "/".into(), vec![], SystemTime::now())
.get_chain(&dave, &alice, "/".into(), vec![], SystemTime::now())
.map_err(|e| e.to_string())?
.ok_or("failed during proof lookup")?
.iter()
Expand Down Expand Up @@ -835,13 +837,7 @@ mod tests {
store.insert(alice_to_bob.clone())?;

let got: Vec<Cid> = store
.get_chain(
&dave,
&Some(alice.clone()),
"/".into(),
vec![],
SystemTime::now(),
)
.get_chain(&dave, &alice.clone(), "/".into(), vec![], SystemTime::now())
.map_err(|e| e.to_string())?
.ok_or("failed during proof lookup")?
.iter()
Expand Down
8 changes: 4 additions & 4 deletions src/delegation/store/traits.rs
Expand Up @@ -49,7 +49,7 @@ where
fn get_chain(
&self,
audience: &DID,
subject: &Option<DID>,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
Expand All @@ -58,7 +58,7 @@ where
fn get_chain_cids(
&self,
audience: &DID,
subject: &Option<DID>,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
Expand All @@ -75,7 +75,7 @@ where
policy: Vec<Predicate>,
now: SystemTime,
) -> Result<bool, Self::DelegationStoreError> {
self.get_chain(audience, &Some(issuer), command, policy, now)
self.get_chain(audience, &issuer, command, policy, now)
.map(|chain| chain.is_some())
}

Expand Down Expand Up @@ -124,7 +124,7 @@ where
fn get_chain(
&self,
audience: &DID,
subject: &Option<DID>,
subject: &DID,
command: String,
policy: Vec<Predicate>,
now: SystemTime,
Expand Down
4 changes: 2 additions & 2 deletions src/invocation/agent.rs
Expand Up @@ -104,7 +104,7 @@ where
self.delegation_store
.get_chain(
&self.did,
&Some(subject.clone()),
&subject.clone(),
ability.to_command(),
vec![],
now,
Expand Down Expand Up @@ -695,7 +695,7 @@ mod tests {
let chain_for_dnslink: Vec<Cid> = del_store
.get_chain(
&device,
&Some(dnslink.clone()),
&dnslink.clone(),
"/".into(),
vec![],
SystemTime::now(),
Expand Down

0 comments on commit 94e946f

Please sign in to comment.