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

Function resolve_identity_for_server

atomic-cli/src/commands/client.rs:248–295  ·  view source on GitHub ↗

Resolve the identity to authenticate as for a given server profile. Resolution order: 1. `server.identity` — a per-server identity override (set when `atomic identity register --identity ` is used). 2. Global default identity from the identity store. Shared by [`build_client_with_org`] and [`build_apex_client`] so the org-scoped and apex-scoped code paths pick the same identity. # Errors

(
    server: &atomic_config::ServerConfig,
)

Source from the content-addressed store, hash-verified

246/// - Per-server identity name not found in the store.
247/// - No default identity set.
248pub fn resolve_identity_for_server(
249 server: &atomic_config::ServerConfig,
250) -> CliResult<atomic_identity::Identity> {
251 let store = IdentityStore::open_default()
252 .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to open identity store: {}", e)))?;
253
254 if let Some(ref identity_name) = server.identity {
255 // Server profile specifies an identity — use it.
256 log::debug!(
257 "Authenticating as '{}' (bound to server profile {})",
258 identity_name,
259 server.url.as_deref().unwrap_or("<no url>")
260 );
261 store.load_by_name(identity_name).map_err(|e| {
262 CliError::Internal(anyhow::anyhow!(
263 "Identity '{}' specified by server profile not found: {}",
264 identity_name,
265 e
266 ))
267 })
268 } else {
269 // Fall back to global default identity.
270 //
271 // Worth logging loudly: the fallback is silent on the wire, so when
272 // the default identity is not the one registered with this server the
273 // only symptom is a 401 from the far end that names no identity at
274 // all. Saying which identity was chosen, and that it was a fallback,
275 // is the difference between a one-line fix and a blind hunt.
276 let identity = store
277 .get_default()
278 .map_err(|e| {
279 CliError::Internal(anyhow::anyhow!("Failed to load default identity: {}", e))
280 })?
281 .ok_or_else(|| {
282 CliError::Internal(anyhow::anyhow!(
283 "No default identity set. Create one first:\n \
284 atomic identity new <name> --email <email> --set-default"
285 ))
286 })?;
287 log::debug!(
288 "Server profile {} declares no identity; falling back to the default identity '{}'. \
289 Bind one with 'atomic server set-identity <profile> <identity>'.",
290 server.url.as_deref().unwrap_or("<no url>"),
291 identity.name
292 );
293 Ok(identity)
294 }
295}
296
297/// Convenience: map a [`atomic_remote::RemoteError`] to a [`CliError`].
298pub fn remote_err(e: atomic_remote::RemoteError) -> CliError {

Callers 3

build_apex_clientFunction · 0.85
build_client_with_orgFunction · 0.85
resolve_org_with_serverFunction · 0.85

Calls 2

load_by_nameMethod · 0.80
get_defaultMethod · 0.45

Tested by

no test coverage detected