Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138//! 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<SpaceInput>,) -> ApiResult<Json<Value>> { 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<String>,}
pub(crate) async fn get_space_credential( (State(state), OriginalUri(uri), headers, Json(input)): SpaceJson<CredentialInput>,) -> ApiResult<Json<Value>> { 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::<crate::syntax::SpaceRef>()?; 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(), ); } } }}