//! Delegation exchange and DPoP-bound Space credential endpoints. use crate::http::request::{HeaderQuery, SpaceJson}; use axum::{ Json, extract::{OriginalUri, Query, State}, }; use serde::Deserialize; use serde_json::{Value, json}; use crate::{ error::Error, http::{ApiResult, auth}, space::auth::SpaceClaims, }; use super::SpaceInput; pub(crate) async fn get_delegation_token( (State(state), headers, Query(query)): HeaderQuery, ) -> ApiResult> { let oauth = auth::token(&headers, &state)?; state.spaces.authorize_delegation(&oauth, &query.space)?; Ok(Json(json!({ "token": state .space_auth .mint_delegation(&query.space, &oauth.account.did)?, }))) } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct CredentialInput { space: String, client_attestation: Option, } pub(crate) async fn get_space_credential( (State(state), OriginalUri(uri), headers, Json(input)): SpaceJson, ) -> ApiResult> { let token = auth::bearer(&headers)?; let proof = auth::dpop_proof(&headers)?; let htu = auth::request_url(&state, &uri); let (SpaceClaims { iss, sub, .. }, jkt) = state .space_auth .exchange_delegation(token, proof, "POST", &htu) .await?; if sub != input.space { return Err(Error::xrpc( "InvalidDelegationToken", "delegation token subject does not match the requested space", )); } let space_ref = input.space.parse::()?; let client_id = if let Some(attestation) = input.client_attestation.as_deref() { Some( state .space_auth .verify_client_attestation( attestation, &format!("{}#atproto_space_host", space_ref.authority), ) .await?, ) } else { None }; let managing_app = state .spaces .authorize_credential(&input.space, &iss, client_id.as_deref())?; if let Some(service) = managing_app && !state .space_auth .check_user_access(&service, &input.space, &iss, client_id.as_deref()) .await { return Err(Error::xrpc( "UserNotAuthorized", "managing app denied access", )); } Ok(Json(json!({ "credential": state.space_auth.mint_credential(&input.space, jkt)?, }))) } #[cfg(test)] mod tests { use super::*; use axum::{body::to_bytes, response::IntoResponse}; use serde::Serialize; #[tokio::test] async fn single_string_credential_outputs_keep_full_response_bytes() { #[derive(Serialize)] struct PreviousDelegationOutput { token: String, } #[derive(Serialize)] struct PreviousCredentialOutput { credential: String, } let values = [ String::new(), "header.payload.signature".to_owned(), "quote-\"-backslash-\\-雪-🦀".to_owned(), (0u8..=31).map(char::from).collect(), "a".repeat(4096), ]; for value in values { for (previous, actual) in [ ( Json(PreviousDelegationOutput { token: value.clone(), }) .into_response(), Json(json!({"token": value})).into_response(), ), ( Json(PreviousCredentialOutput { credential: value.clone(), }) .into_response(), Json(json!({"credential": value})).into_response(), ), ] { assert_eq!(actual.status(), previous.status()); assert_eq!(actual.headers(), previous.headers()); assert_eq!( to_bytes(actual.into_body(), usize::MAX).await.unwrap(), to_bytes(previous.into_body(), usize::MAX).await.unwrap(), ); } } } }