this repo has no description
1mod common; 2 3use axum::{Router, extract::Request, http::StatusCode, routing::any}; 4use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD}; 5use reqwest::Client; 6use std::sync::Arc; 7use tokio::net::TcpListener; 8 9async fn spawn_mock_upstream() -> ( 10 String, 11 tokio::sync::mpsc::Receiver<(String, String, Option<String>)>, 12) { 13 let (tx, rx) = tokio::sync::mpsc::channel(10); 14 let tx = Arc::new(tx); 15 16 let app = Router::new().fallback(any(move |req: Request| { 17 let tx = tx.clone(); 18 async move { 19 let method = req.method().to_string(); 20 let uri = req.uri().to_string(); 21 let auth = req 22 .headers() 23 .get("Authorization") 24 .and_then(|h| h.to_str().ok()) 25 .map(|s| s.to_string()); 26 27 let _ = tx.send((method, uri, auth)).await; 28 (StatusCode::OK, "Mock Response") 29 } 30 })); 31 32 let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); 33 let addr = listener.local_addr().unwrap(); 34 35 tokio::spawn(async move { 36 axum::serve(listener, app).await.unwrap(); 37 }); 38 39 (format!("http://{}", addr), rx) 40} 41 42#[tokio::test] 43async fn test_proxy_via_header() { 44 let app_url = common::base_url().await; 45 let (upstream_url, mut rx) = spawn_mock_upstream().await; 46 let client = Client::new(); 47 48 let res = client 49 .get(format!("{}/xrpc/com.example.test", app_url)) 50 .header("atproto-proxy", &upstream_url) 51 .header("Authorization", "Bearer test-token") 52 .send() 53 .await 54 .unwrap(); 55 56 assert_eq!(res.status(), StatusCode::OK); 57 58 let (method, uri, auth) = rx.recv().await.expect("Upstream should receive request"); 59 assert_eq!(method, "GET"); 60 assert_eq!(uri, "/xrpc/com.example.test"); 61 assert_eq!(auth, Some("Bearer test-token".to_string())); 62} 63 64#[tokio::test] 65#[ignore] 66async fn test_proxy_via_env_var() { 67 let (upstream_url, mut rx) = spawn_mock_upstream().await; 68 69 unsafe { 70 std::env::set_var("APPVIEW_URL", &upstream_url); 71 } 72 73 let app_url = common::base_url().await; 74 let client = Client::new(); 75 76 let res = client 77 .get(format!("{}/xrpc/com.example.envtest", app_url)) 78 .send() 79 .await 80 .unwrap(); 81 82 assert_eq!(res.status(), StatusCode::OK); 83 84 let (method, uri, _) = rx.recv().await.expect("Upstream should receive request"); 85 assert_eq!(method, "GET"); 86 assert_eq!(uri, "/xrpc/com.example.envtest"); 87} 88 89#[tokio::test] 90#[ignore] 91async fn test_proxy_missing_config() { 92 unsafe { 93 std::env::remove_var("APPVIEW_URL"); 94 } 95 96 let app_url = common::base_url().await; 97 let client = Client::new(); 98 99 let res = client 100 .get(format!("{}/xrpc/com.example.fail", app_url)) 101 .send() 102 .await 103 .unwrap(); 104 105 assert_eq!(res.status(), StatusCode::BAD_GATEWAY); 106} 107 108#[tokio::test] 109async fn test_proxy_auth_signing() { 110 let app_url = common::base_url().await; 111 let (upstream_url, mut rx) = spawn_mock_upstream().await; 112 let client = Client::new(); 113 114 let (access_jwt, did) = common::create_account_and_login(&client).await; 115 116 let res = client 117 .get(format!("{}/xrpc/com.example.signed", app_url)) 118 .header("atproto-proxy", &upstream_url) 119 .header("Authorization", format!("Bearer {}", access_jwt)) 120 .send() 121 .await 122 .unwrap(); 123 124 assert_eq!(res.status(), StatusCode::OK); 125 126 let (method, uri, auth) = rx.recv().await.expect("Upstream receive"); 127 assert_eq!(method, "GET"); 128 assert_eq!(uri, "/xrpc/com.example.signed"); 129 130 let received_token = auth.expect("No auth header").replace("Bearer ", ""); 131 assert_ne!(received_token, access_jwt, "Token should be replaced"); 132 133 let parts: Vec<&str> = received_token.split('.').collect(); 134 assert_eq!(parts.len(), 3); 135 136 let payload_bytes = URL_SAFE_NO_PAD.decode(parts[1]).expect("payload b64"); 137 let claims: serde_json::Value = serde_json::from_slice(&payload_bytes).expect("payload json"); 138 139 assert_eq!(claims["iss"], did); 140 assert_eq!(claims["sub"], did); 141 assert_eq!(claims["aud"], upstream_url); 142 assert_eq!(claims["lxm"], "com.example.signed"); 143}