MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / delegate_from_request

Method delegate_from_request

atomic-cli/src/commands/identity/delegate.rs:319–365  ·  view source on GitHub ↗

Verify a self-signed request and turn it into a delegate identity. The verification is the point: it proves whoever produced the request holds the private half of the key it names. Without it, `--request` would be a way to talk someone into signing a certificate for a key chosen by an attacker.

(&self, path: &str)

Source from the content-addressed store, hash-verified

317 /// would be a way to talk someone into signing a certificate for a key
318 /// chosen by an attacker.
319 fn delegate_from_request(&self, path: &str) -> CliResult<Identity> {
320 let raw = if path == "-" {
321 use std::io::Read;
322 let mut buf = String::new();
323 std::io::stdin().read_to_string(&mut buf)?;
324 buf
325 } else {
326 std::fs::read_to_string(path)?
327 };
328
329 // Admission before the value exists: a request arrives from the far end
330 // as bytes, and its self-signature is about to be checked over the
331 // canonical form of whatever those bytes denote.
332 let value = jcs::admit_document(raw.as_bytes()).map_err(|e| CliError::InvalidArgument {
333 message: format!("{path} was refused: {e}"),
334 })?;
335
336 let request = cert::verify_request(&value).map_err(|e| CliError::DelegationError {
337 message: format!(
338 "The request in {path} does not verify: {e}\n \
339 Only countersign a request whose self-signature checks out — it is the \
340 only evidence the far end actually holds that key."
341 ),
342 })?;
343
344 let public_key =
345 atomic_identity::PublicKey::from_did_key(&request.delegate_key).map_err(|e| {
346 CliError::DelegationError {
347 message: format!("Request carries a malformed key: {e}"),
348 }
349 })?;
350
351 print_hint(&format!(
352 "Countersigning a verified request from '{}' ({})",
353 request.delegate_name, request.delegate
354 ));
355
356 let mut builder = Identity::builder(&request.delegate_name)
357 .identity_type(IdentityType::Agent)
358 .public_key(public_key);
359 if let Some(agent) = &request.software_agent {
360 builder = builder.description(format!("software-agent:{agent}"));
361 }
362 builder
363 .build()
364 .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to build delegate: {e}")))
365 }
366}
367
368/// Recover the software-agent URN we stash in an identity description.

Callers 1

runMethod · 0.80

Calls 8

admit_documentFunction · 0.85
verify_requestFunction · 0.85
print_hintFunction · 0.85
as_bytesMethod · 0.45
public_keyMethod · 0.45
identity_typeMethod · 0.45
descriptionMethod · 0.45
buildMethod · 0.45

Tested by

no test coverage detected