Skip to content

Commit

Permalink
feat: Allow creating powerlines from Agent::delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Mar 26, 2024
1 parent 42aa9ce commit 4e412b2
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/delegation/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
pub fn delegate(
&self,
audience: DID,
subject: &DID,
subject: Option<&DID>,
via: Option<DID>,
command: String,
new_policy: Vec<Predicate>,
Expand All @@ -71,44 +71,34 @@ where
let mut salt = self.did.clone().to_string().into_bytes();
let nonce = Nonce::generate_12(&mut salt);

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
.store
.get_chain(&self.did, &subject, &command, vec![], now)
.map_err(DelegateError::StoreError)?
.ok_or(DelegateError::ProofsNotFound)?;
let to_delegate = proofs.first().1.payload();

let mut policy = to_delegate.policy.clone();
policy.append(&mut new_policy.clone());
let (subject, policy) = match subject {
Some(subject) if *subject == self.did => (Some(subject.clone()), new_policy),
None => (None, new_policy),
Some(subject) => {
let proofs = &self
.store
.get_chain(&self.did, &subject, &command, vec![], now)
.map_err(DelegateError::StoreError)?
.ok_or(DelegateError::ProofsNotFound)?;
let to_delegate = proofs.first().1.payload();

let mut policy = to_delegate.policy.clone();
policy.extend(new_policy);
(Some(subject.clone()), policy)
}
};

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

Ok(Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME"))
Expand Down

0 comments on commit 4e412b2

Please sign in to comment.