CLI app for developers prototyping atproto functionality
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(create_report): eliminate duplicate createSession call (I1, I2)

I1: Remove dead fetch_service_auth_jwt function and replace with PdsJwtFetcher::fetch_with_jwt call.
I2: Thread access_jwt from fetch_session_and_did through PdsJwtFetcher::fetch_with_jwt to avoid duplicate createSession call.

This reduces PDS calls from 2 createSession per run to 1, as only fetch_session_and_did needs to call createSession; fetch_with_jwt (formerly fetch_service_auth_jwt) now uses the reused access_jwt.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

authored by

Jack Grigg
Claude Haiku 4.5
and committed by
Tangled
22e97f16 f1d365f2

+18 -114
+18 -114
src/commands/test/labeler/create_report.rs
··· 387 387 Self { client } 388 388 } 389 389 390 - /// Run `createSession` then `getServiceAuth`, returning the minted 391 - /// service-auth JWT. 392 - pub async fn fetch( 390 + /// Call `getServiceAuth` using the provided access JWT, returning the 391 + /// minted service-auth JWT. The access JWT should come from a prior 392 + /// `createSession` call. 393 + pub async fn fetch_with_jwt( 393 394 &self, 394 - handle: &str, 395 - app_password: &str, 395 + access_jwt: &str, 396 396 aud: &str, 397 397 lxm: &str, 398 398 exp_absolute_unix: i64, 399 399 ) -> Result<String, PdsJwtFetchError> { 400 - // 1. createSession. 401 - let body = serde_json::json!({ 402 - "identifier": handle, 403 - "password": app_password, 404 - }); 405 - let resp = self 406 - .client 407 - .post("xrpc/com.atproto.server.createSession", None, None, &body) 408 - .await 409 - .map_err(|e| PdsJwtFetchError::Pds { 410 - message: format!("createSession transport: {e}"), 411 - })?; 412 - if !resp.status.is_success() { 413 - return Err(PdsJwtFetchError::Pds { 414 - message: format!("createSession returned status {}", resp.status), 415 - }); 416 - } 417 - let session: serde_json::Value = 418 - serde_json::from_slice(&resp.raw_body).map_err(|e| PdsJwtFetchError::Pds { 419 - message: format!("createSession body not JSON: {e}"), 420 - })?; 421 - let access_jwt = session["accessJwt"] 422 - .as_str() 423 - .ok_or_else(|| PdsJwtFetchError::Pds { 424 - message: "createSession response missing accessJwt".to_string(), 425 - })? 426 - .to_string(); 427 - 428 - // 2. getServiceAuth (GET with query params). 400 + // getServiceAuth (GET with query params). 429 401 let exp_s = exp_absolute_unix.to_string(); 430 402 let resp = self 431 403 .client 432 404 .get( 433 405 "xrpc/com.atproto.server.getServiceAuth", 434 - Some(&access_jwt), 406 + Some(access_jwt), 435 407 &[("aud", aud), ("lxm", lxm), ("exp", &exp_s)], 436 408 ) 437 409 .await ··· 1158 1130 let creds = opts.pds_credentials.expect("pds_ready implies creds"); 1159 1131 let pds_client = opts.pds_xrpc_client.expect("pds_ready implies client"); 1160 1132 1161 - // Recompute locality for pollution-avoidance. 1162 - let is_local = is_local_labeler_hostname(&id_facts.labeler_endpoint); 1133 + // Reuse locality computed earlier. 1134 + let is_local = is_local_labeler; 1163 1135 let reason_type = pollution::choose_reason_type( 1164 1136 id_facts.reason_types.as_deref().unwrap_or(&[]), 1165 1137 is_local, ··· 1191 1163 1192 1164 // Mode 2: getServiceAuth direct-POST. 1193 1165 let exp_abs = now + 60; 1194 - match fetch_service_auth_jwt( 1195 - pds_client, 1196 - &creds.handle, 1197 - &creds.app_password, 1198 - &id_facts.did.0, 1199 - "com.atproto.moderation.createReport", 1200 - exp_abs, 1201 - ) 1202 - .await 1166 + let fetcher = PdsJwtFetcher::new(pds_client); 1167 + match fetcher 1168 + .fetch_with_jwt( 1169 + &access_jwt, 1170 + &id_facts.did.0, 1171 + "com.atproto.moderation.createReport", 1172 + exp_abs, 1173 + ) 1174 + .await 1203 1175 { 1204 1176 Err(PdsJwtFetchError::Pds { message }) => { 1205 1177 results.push(Check::PdsServiceAuthAccepted.network_error(message)); ··· 1308 1280 .ok_or("createSession missing accessJwt")? 1309 1281 .to_string(); 1310 1282 Ok(SessionResult { did, access_jwt }) 1311 - } 1312 - 1313 - /// Fetch a service-auth JWT from a PDS by calling getServiceAuth after 1314 - /// createSession. This JWT can be used directly to POST to the labeler. 1315 - async fn fetch_service_auth_jwt( 1316 - client: &dyn PdsXrpcClient, 1317 - handle: &str, 1318 - app_password: &str, 1319 - aud: &str, 1320 - lxm: &str, 1321 - exp_absolute_unix: i64, 1322 - ) -> Result<String, PdsJwtFetchError> { 1323 - // 1. createSession. 1324 - let body = serde_json::json!({ 1325 - "identifier": handle, 1326 - "password": app_password, 1327 - }); 1328 - let resp = client 1329 - .post("xrpc/com.atproto.server.createSession", None, None, &body) 1330 - .await 1331 - .map_err(|e| PdsJwtFetchError::Pds { 1332 - message: format!("createSession transport: {e}"), 1333 - })?; 1334 - if !resp.status.is_success() { 1335 - return Err(PdsJwtFetchError::Pds { 1336 - message: format!("createSession returned status {}", resp.status), 1337 - }); 1338 - } 1339 - let session: serde_json::Value = 1340 - serde_json::from_slice(&resp.raw_body).map_err(|e| PdsJwtFetchError::Pds { 1341 - message: format!("createSession body not JSON: {e}"), 1342 - })?; 1343 - let access_jwt = session["accessJwt"] 1344 - .as_str() 1345 - .ok_or_else(|| PdsJwtFetchError::Pds { 1346 - message: "createSession response missing accessJwt".to_string(), 1347 - })? 1348 - .to_string(); 1349 - 1350 - // 2. getServiceAuth (GET with query params). 1351 - let exp_s = exp_absolute_unix.to_string(); 1352 - let resp = client 1353 - .get( 1354 - "xrpc/com.atproto.server.getServiceAuth", 1355 - Some(&access_jwt), 1356 - &[("aud", aud), ("lxm", lxm), ("exp", &exp_s)], 1357 - ) 1358 - .await 1359 - .map_err(|e| PdsJwtFetchError::Pds { 1360 - message: format!("getServiceAuth transport: {e}"), 1361 - })?; 1362 - if !resp.status.is_success() { 1363 - return Err(PdsJwtFetchError::Pds { 1364 - message: format!("getServiceAuth returned status {}", resp.status), 1365 - }); 1366 - } 1367 - let auth: serde_json::Value = 1368 - serde_json::from_slice(&resp.raw_body).map_err(|e| PdsJwtFetchError::Pds { 1369 - message: format!("getServiceAuth body not JSON: {e}"), 1370 - })?; 1371 - let token = auth["token"] 1372 - .as_str() 1373 - .ok_or_else(|| PdsJwtFetchError::Pds { 1374 - message: "getServiceAuth response missing token".to_string(), 1375 - })? 1376 - .to_string(); 1377 - 1378 - Ok(token) 1379 1283 } 1380 1284 1381 1285 /// Synthesize a `reasonType` string that is definitely NOT in the