this repo has no description
1use crate::api::ApiError; 2use crate::api::proxy_client::{ 3 MAX_RESPONSE_SIZE, is_ssrf_safe, proxy_client, validate_at_uri, validate_limit, 4}; 5use crate::state::AppState; 6use axum::{ 7 extract::{Query, State}, 8 http::StatusCode, 9 response::{IntoResponse, Response}, 10}; 11use serde::Deserialize; 12use std::collections::HashMap; 13use tracing::{error, info}; 14 15#[derive(Deserialize)] 16pub struct GetFeedParams { 17 pub feed: String, 18 pub limit: Option<u32>, 19 pub cursor: Option<String>, 20} 21 22pub async fn get_feed( 23 State(state): State<AppState>, 24 headers: axum::http::HeaderMap, 25 Query(params): Query<GetFeedParams>, 26) -> Response { 27 let token = match crate::auth::extract_bearer_token_from_header( 28 headers.get("Authorization").and_then(|h| h.to_str().ok()), 29 ) { 30 Some(t) => t, 31 None => return ApiError::AuthenticationRequired.into_response(), 32 }; 33 let auth_user = match crate::auth::validate_bearer_token(&state.db, &token).await { 34 Ok(user) => user, 35 Err(e) => return ApiError::from(e).into_response(), 36 }; 37 if let Err(e) = validate_at_uri(&params.feed) { 38 return ApiError::InvalidRequest(format!("Invalid feed URI: {}", e)).into_response(); 39 } 40 let resolved = match state.appview_registry.get_appview_for_method("app.bsky.feed.getFeed").await { 41 Some(r) => r, 42 None => { 43 return ApiError::UpstreamUnavailable("No upstream AppView configured for app.bsky.feed.getFeed".to_string()) 44 .into_response(); 45 } 46 }; 47 if let Err(e) = is_ssrf_safe(&resolved.url) { 48 error!("SSRF check failed for appview URL: {}", e); 49 return ApiError::UpstreamUnavailable(format!("Invalid upstream URL: {}", e)) 50 .into_response(); 51 } 52 let limit = validate_limit(params.limit, 50, 100); 53 let mut query_params = HashMap::new(); 54 query_params.insert("feed".to_string(), params.feed.clone()); 55 query_params.insert("limit".to_string(), limit.to_string()); 56 if let Some(cursor) = &params.cursor { 57 query_params.insert("cursor".to_string(), cursor.clone()); 58 } 59 let target_url = format!("{}/xrpc/app.bsky.feed.getFeed", resolved.url); 60 info!(target = %target_url, feed = %params.feed, "Proxying getFeed request"); 61 let client = proxy_client(); 62 let mut request_builder = client.get(&target_url).query(&query_params); 63 if let Some(key_bytes) = auth_user.key_bytes.as_ref() { 64 match crate::auth::create_service_token( 65 &auth_user.did, 66 &resolved.did, 67 "app.bsky.feed.getFeed", 68 key_bytes, 69 ) { 70 Ok(service_token) => { 71 request_builder = 72 request_builder.header("Authorization", format!("Bearer {}", service_token)); 73 } 74 Err(e) => { 75 error!(error = ?e, "Failed to create service token for getFeed"); 76 return ApiError::InternalError.into_response(); 77 } 78 } 79 } 80 match request_builder.send().await { 81 Ok(resp) => { 82 let status = 83 StatusCode::from_u16(resp.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY); 84 let content_length = resp.content_length().unwrap_or(0); 85 if content_length > MAX_RESPONSE_SIZE { 86 error!( 87 content_length, 88 max = MAX_RESPONSE_SIZE, 89 "getFeed response too large" 90 ); 91 return ApiError::UpstreamFailure.into_response(); 92 } 93 let resp_headers = resp.headers().clone(); 94 let body = match resp.bytes().await { 95 Ok(b) => { 96 if b.len() as u64 > MAX_RESPONSE_SIZE { 97 error!(len = b.len(), "getFeed response body exceeded limit"); 98 return ApiError::UpstreamFailure.into_response(); 99 } 100 b 101 } 102 Err(e) => { 103 error!(error = ?e, "Error reading getFeed response"); 104 return ApiError::UpstreamFailure.into_response(); 105 } 106 }; 107 let mut response_builder = axum::response::Response::builder().status(status); 108 if let Some(ct) = resp_headers.get("content-type") { 109 response_builder = response_builder.header("content-type", ct); 110 } 111 match response_builder.body(axum::body::Body::from(body)) { 112 Ok(r) => r, 113 Err(e) => { 114 error!(error = ?e, "Error building getFeed response"); 115 ApiError::UpstreamFailure.into_response() 116 } 117 } 118 } 119 Err(e) => { 120 error!(error = ?e, "Error proxying getFeed"); 121 if e.is_timeout() { 122 ApiError::UpstreamTimeout.into_response() 123 } else if e.is_connect() { 124 ApiError::UpstreamUnavailable("Failed to connect to upstream".to_string()) 125 .into_response() 126 } else { 127 ApiError::UpstreamFailure.into_response() 128 } 129 } 130 } 131}