From 0ce725174df2fbdae9904ac859461ab638336df2 Mon Sep 17 00:00:00 2001 From: Louis Escher Date: Wed, 19 Aug 2026 21:51:00 +0200 Subject: [PATCH] fix: DID length test, service test, cloning, dead code (should be it!) --- .../tranquil-api/src/server/service_auth.rs | 27 ++++++------ crates/tranquil-pds/src/api/proxy.rs | 2 +- crates/tranquil-types/src/lib.rs | 44 ++++++++++--------- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/crates/tranquil-api/src/server/service_auth.rs b/crates/tranquil-api/src/server/service_auth.rs index f98cc5d..5365c87 100644 --- a/crates/tranquil-api/src/server/service_auth.rs +++ b/crates/tranquil-api/src/server/service_auth.rs @@ -169,20 +169,19 @@ pub async fn get_service_auth( } } - let service_token = - match tranquil_pds::auth::create_service_token( - &auth.did, - ¶ms.aud, - lxm, - params.exp, - &key_bytes, - ) { - Ok(t) => t, - Err(e) => { - error!("Failed to create service token: {:?}", e); - return ApiError::InternalError(None).into_response(); - } - }; + let service_token = match tranquil_pds::auth::create_service_token( + &auth.did, + ¶ms.aud, + lxm, + params.exp, + &key_bytes, + ) { + Ok(t) => t, + Err(e) => { + error!("Failed to create service token: {:?}", e); + return ApiError::InternalError(None).into_response(); + } + }; ( StatusCode::OK, Json(GetServiceAuthOutput { diff --git a/crates/tranquil-pds/src/api/proxy.rs b/crates/tranquil-pds/src/api/proxy.rs index 39f4406..66344ed 100644 --- a/crates/tranquil-pds/src/api/proxy.rs +++ b/crates/tranquil-pds/src/api/proxy.rs @@ -361,7 +361,7 @@ async fn proxy_handler( match crate::auth::create_service_token( &auth_user.did, - &DidRef::from(&token_aud), + &DidRef::from(token_aud), Some(&token_lxm), None, &key_bytes, diff --git a/crates/tranquil-types/src/lib.rs b/crates/tranquil-types/src/lib.rs index 3703bff..7a6cccb 100644 --- a/crates/tranquil-types/src/lib.rs +++ b/crates/tranquil-types/src/lib.rs @@ -278,13 +278,9 @@ validated_string_newtype! { }; } -impl DidRef { - pub fn did(&self) -> &str { - self.0.split('#').next().unwrap_or(&self.0) - } - - pub fn service_id(&self) -> Option<&str> { - self.0.split_once('#').map(|(_, service_id)| service_id) +impl From for DidRef { + fn from(did: Did) -> Self { + Self(did.0) } } @@ -1660,12 +1656,10 @@ mod validated_newtype_tests { #[test] fn a_bare_did_ref_names_no_service() { let aud = DidRef::new("did:plc:abc").unwrap(); - assert_eq!(aud.as_str(), "did:plc:abc"); - assert_eq!(aud.did(), "did:plc:abc"); assert_eq!( - aud.service_id(), - None, - "an absent fragment is not the same as an empty one" + aud.as_str(), + "did:plc:abc", + "an absent fragment is not the same as an empty one, so nothing may be appended" ); } @@ -1678,8 +1672,6 @@ mod validated_newtype_tests { "the fragment is what tells the receiver which of its services was audienced, \ so it must survive entirely" ); - assert_eq!(aud.did(), "did:web:api.colibri.social"); - assert_eq!(aud.service_id(), Some("colibri_appview")); } #[test] @@ -1755,19 +1747,31 @@ mod validated_newtype_tests { #[test] fn an_over_long_did_ref_is_rejected() { - let long = format!("did:web:{}#def", "a".repeat(2048)); + let did = format!("did:plc:{}", "a".repeat(DID_REF_MAX_LEN - "did:plc:".len())); + assert_eq!(did.len(), DID_REF_MAX_LEN); assert!( - DidRef::new(&long).is_err(), - "the lexicon bounds aud at 2048 bytes" + Did::new(&did).is_ok(), + "the DID half has to stand on its own, or the bound below proves nothing" + ); + assert!( + DidRef::new(format!("{did}#x")).is_err(), + "the lexicon bounds aud at {DID_REF_MAX_LEN} bytes, which a whole DID plus the shortest service id already exceeds" ); } #[test] fn a_did_ref_built_from_a_did_names_no_service() { let did = Did::new("did:plc:def").unwrap(); - let aud = DidRef::from(&did); - assert_eq!(aud.as_str(), did.as_str()); - assert_eq!(aud.service_id(), None); + assert_eq!( + DidRef::from(&did).as_str(), + did.as_str(), + "a DID that named no service must not gain one on the way in" + ); + assert_eq!( + DidRef::from(did.clone()).as_str(), + did.as_str(), + "the owned conversion must land on the same bytes as the borrowed one" + ); } #[test]