Attach a Bearer JWT auth header to the remote config. `identity_override` — if provided, this identity name is used directly, bypassing URL-based inference. Set from `RemoteEntry.identity` or the `--identity` CLI flag. If the identity cannot be resolved (no override, no userinfo, no subdomain, or identity not found in the store) or login fails, the config is returned unmodified and a debug log i
(
config: HttpRemoteConfig,
remote_url: &str,
identity_override: Option<&str>,
)
| 163 | /// unmodified and a debug log is emitted. This keeps push/pull/clone working |
| 164 | /// against servers that don't require auth (e.g. public reads). |
| 165 | pub async fn attach_identity( |
| 166 | config: HttpRemoteConfig, |
| 167 | remote_url: &str, |
| 168 | identity_override: Option<&str>, |
| 169 | ) -> HttpRemoteConfig { |
| 170 | if identity_override.is_some() { |
| 171 | log::debug!("Using explicit identity override: {:?}", identity_override); |
| 172 | } |
| 173 | |
| 174 | // Priority 1: explicit override (--identity); 2/3: URL userinfo or subdomain. |
| 175 | let identity_name = match resolve_identity_name_with_override(remote_url, identity_override) { |
| 176 | Some(name) => name, |
| 177 | None => { |
| 178 | log::debug!("No identity resolvable from URL: {}", remote_url); |
| 179 | return config; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | log::debug!("Resolved identity name: {}", identity_name); |
| 184 | |
| 185 | let store = match IdentityStore::open_default() { |
| 186 | Ok(s) => s, |
| 187 | Err(e) => { |
| 188 | log::debug!("Failed to open identity store: {}", e); |
| 189 | return config; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | // Match by name, tolerating tenant-slug/identity-name case differences |
| 194 | // (e.g. the `aaron` subdomain selecting the local `Aaron` identity). |
| 195 | let identity = match load_identity_lenient(&store, &identity_name) { |
| 196 | Some(id) => id, |
| 197 | None => { |
| 198 | log::debug!("Identity '{}' not found in store", identity_name); |
| 199 | return config; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | // Tokens are keyed to the apex server (where the identity registered), not |
| 204 | // the tenant subdomain — strip the leading subdomain label from the host. |
| 205 | let server = match apex_server_url(remote_url) { |
| 206 | Some(s) => s, |
| 207 | None => { |
| 208 | log::debug!("Could not derive apex server URL from: {}", remote_url); |
| 209 | return config; |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | match crate::commands::token::get_token(&server, &identity).await { |
| 214 | Ok(jwt) => { |
| 215 | log::debug!("Attaching Bearer JWT for identity '{}'", identity_name); |
| 216 | config.with_header("Authorization", format!("Bearer {}", jwt)) |
| 217 | } |
| 218 | Err(e) => { |
| 219 | // Non-fatal: a server that doesn't require auth still works for |
| 220 | // public reads. Auth-required operations will then fail with a |
| 221 | // clear 401 from the server. |
| 222 | log::debug!("Could not obtain JWT for '{}': {}", identity_name, e); |
no test coverage detected