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

Function from_environment

atomic-remote/src/auth/delegation.rs:190–244  ·  view source on GitHub ↗

A grant supplied through [`DELEGATION_ENV`], verified and checked to belong to this agent. Returns `Err` rather than `Ok(None)` when the variable is set but unusable. Falling back to the store there would be worse than failing: the operator asked for a specific grant, and silently using a different one is how you get an agent acting under a scope nobody intended.

(identity: &Identity)

Source from the content-addressed store, hash-verified

188/// asked for a specific grant, and silently using a different one is how you
189/// get an agent acting under a scope nobody intended.
190fn from_environment(identity: &Identity) -> AuthResult<Option<ResolvedDelegation>> {
191 let Ok(encoded) = std::env::var(DELEGATION_ENV) else {
192 return Ok(None);
193 };
194 let encoded = encoded.trim();
195 if encoded.is_empty() {
196 return Ok(None);
197 }
198
199 let document =
200 cert::decode_from_transport(encoded).map_err(|e| AuthError::DelegationError {
201 message: format!("{DELEGATION_ENV} is not a usable grant: {e}"),
202 })?;
203
204 let delegation =
205 cert::verify_self_contained(&document).map_err(|e| AuthError::DelegationError {
206 message: format!("The grant in {DELEGATION_ENV} does not verify: {e}"),
207 })?;
208
209 if delegation.delegate != identity.id.to_did() {
210 // Compare DIDs, and say so. Display names are not unique — two agents
211 // called `alice+claude` on different machines are different keys, and
212 // an error reading "issued to 'alice+claude', not 'alice+claude'" tells
213 // the reader nothing.
214 return Err(AuthError::DelegationError {
215 message: format!(
216 "The grant in {DELEGATION_ENV} was issued to a different key.\n \
217 Grant is for {} ({})\n \
218 Running as {} ({})",
219 delegation.delegate_name,
220 delegation.delegate,
221 identity.name,
222 identity.id.to_did()
223 ),
224 });
225 }
226
227 if delegation.is_expired() {
228 return Err(AuthError::DelegationError {
229 message: format!(
230 "The grant in {DELEGATION_ENV} expired {}. Ask for a fresh one.",
231 delegation
232 .expires
233 .map(|e| e.format("on %Y-%m-%d %H:%M UTC").to_string())
234 .unwrap_or_else(|| "some time ago".to_string())
235 ),
236 });
237 }
238
239 Ok(Some(ResolvedDelegation {
240 delegation,
241 document,
242 status: DelegationStatus::Active,
243 }))
244}
245
246/// Pre-flight a specific operation before paying for a network round trip.
247///

Callers 1

active_forFunction · 0.85

Calls 5

decode_from_transportFunction · 0.85
verify_self_containedFunction · 0.85
to_didMethod · 0.80
is_emptyMethod · 0.45
is_expiredMethod · 0.45

Tested by

no test coverage detected