//! The machine API through the router: who may call it, the stream's resume rule, and the photo path. use http::header::{AUTHORIZATION, WWW_AUTHENTICATE}; use im_core::model::Client; use im_core::store::clients::NewClient; use im_core::store::new_id; use im_core::time::unix_now; use im_core::token::{generate_token, hash_token}; use serde_json::{Value, json}; use topcoat::router::{Body, StatusCode, to_bytes}; use crate::boot::{router, test_state}; use crate::oidc::signing; use crate::state::AppState; /// A family client (`in`) that may ask for the directory scope, and a secret it is not given here. struct Fixture { _dir: tempfile::TempDir, state: AppState, client: Client, } async fn fixture() -> Fixture { let (dir, state) = test_state().await; signing::maintain(&state.store, &state.key) .await .expect("signing key"); let client = state .store .create_client(&NewClient { name: "Files", secret_hash: &hash_token(&generate_token()), redirect_uris: &[], post_logout_redirect_uris: &[], backchannel_logout_uri: None, allowed_scopes: "directory.read files.service", service_key: Some("in"), }) .await .expect("client"); Fixture { _dir: dir, state, client, } } /// The claims of a client-credentials token that `im` would issue to `client` for `aud` and `scope`. fn machine_claims(state: &AppState, client: &Client, aud: &str, scope: &str) -> Value { let now = unix_now(); json!({ "iss": state.config.base_url, "sub": format!("client:{}", client.id), "azp": client.id, "aud": aud, "scope": scope, "svc": "in", "iat": now, "exp": now + 600, "jti": generate_token(), }) } /// Signs `claims` with the instance's own key. async fn mint(state: &AppState, claims: &Value) -> String { signing::sign(&state.store, &state.key, claims) .await .expect("token") } /// Sends a GET with a bearer token through the whole router: status, headers and body text. async fn get(state: &AppState, path: &str, token: &str) -> (StatusCode, http::HeaderMap, String) { let request = http::Request::get(path) .header(AUTHORIZATION, format!("Bearer {token}")) .body(Body::empty()) .expect("request"); let response = router(state.clone()).handle(request).await; let status = response.status(); let headers = response.headers().clone(); let bytes = to_bytes(response.into_body(), 1024 * 1024) .await .expect("body"); ( status, headers, String::from_utf8_lossy(&bytes).into_owned(), ) } #[tokio::test] async fn a_user_access_token_is_refused_on_the_directory() { let f = fixture().await; // A person's access token: their id as `sub`, a session as `sid`, and no `svc`. let claims = json!({ "iss": f.state.config.base_url, "sub": new_id(), "aud": "im", "azp": f.client.id, "scope": "openid profile", "sid": new_id(), "iat": unix_now(), "exp": unix_now() + 600, }); let token = mint(&f.state, &claims).await; let (status, headers, _) = get(&f.state, "/api/v1/directory", &token).await; assert_eq!(status, StatusCode::UNAUTHORIZED); assert_eq!(headers[WWW_AUTHENTICATE], "Bearer error=\"invalid_token\""); } #[tokio::test] async fn a_token_for_another_audience_is_refused() { let f = fixture().await; let claims = machine_claims(&f.state, &f.client, "in", "directory.read"); let token = mint(&f.state, &claims).await; let (status, _, _) = get(&f.state, "/api/v1/directory", &token).await; assert_eq!(status, StatusCode::UNAUTHORIZED); } #[tokio::test] async fn a_token_without_the_directory_scope_gets_403() { let f = fixture().await; let claims = machine_claims(&f.state, &f.client, "im", "files.service"); let token = mint(&f.state, &claims).await; let (status, headers, body) = get(&f.state, "/api/v1/directory", &token).await; assert_eq!(status, StatusCode::FORBIDDEN); assert_eq!( headers[WWW_AUTHENTICATE], "Bearer error=\"insufficient_scope\"" ); assert!(body.contains("insufficient_scope"), "{body}"); } #[tokio::test] async fn a_cursor_from_the_future_gets_a_resync_and_the_stream_ends() { let f = fixture().await; let claims = machine_claims(&f.state, &f.client, "im", "directory.read"); let token = mint(&f.state, &claims).await; let (status, _, body) = get(&f.state, "/api/v1/directory/stream?cursor=999", &token).await; assert_eq!(status, StatusCode::OK); assert!(body.contains("event: resync"), "{body}"); assert!(!body.contains("event: member"), "{body}"); } #[tokio::test] async fn a_photo_path_with_an_encoded_slash_is_not_found() { let f = fixture().await; let claims = machine_claims(&f.state, &f.client, "im", "directory.read"); let token = mint(&f.state, &claims).await; let (status, _, _) = get( &f.state, "/api/v1/people/..%2f..%2fetc%2fpasswd/photo", &token, ) .await; assert_eq!(status, StatusCode::NOT_FOUND); } #[tokio::test] async fn a_token_of_a_disabled_client_is_refused() { let f = fixture().await; let claims = machine_claims(&f.state, &f.client, "im", "directory.read"); let token = mint(&f.state, &claims).await; assert!( f.state .store .disable_client(&f.client.id) .await .expect("disable") ); let (status, _, _) = get(&f.state, "/api/v1/directory", &token).await; assert_eq!(status, StatusCode::UNAUTHORIZED); }