forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

tests.rs172 lines, 5.6 KiBHistoryRawDownload
1//! The machine API through the router: who may call it, the stream's resume rule, and the photo path.
2
3use http::header::{AUTHORIZATION, WWW_AUTHENTICATE};
4use im_core::model::Client;
5use im_core::store::clients::NewClient;
6use im_core::store::new_id;
7use im_core::time::unix_now;
8use im_core::token::{generate_token, hash_token};
9use serde_json::{Value, json};
10use topcoat::router::{Body, StatusCode, to_bytes};
11
12use crate::boot::{router, test_state};
13use crate::oidc::signing;
14use crate::state::AppState;
15
16/// A family client (`in`) that may ask for the directory scope, and a secret it is not given here.
17struct Fixture {
18 _dir: tempfile::TempDir,
19 state: AppState,
20 client: Client,
21}
22
23async fn fixture() -> Fixture {
24 let (dir, state) = test_state().await;
25 signing::maintain(&state.store, &state.key)
26 .await
27 .expect("signing key");
28 let client = state
29 .store
30 .create_client(&NewClient {
31 name: "Files",
32 secret_hash: &hash_token(&generate_token()),
33 redirect_uris: &[],
34 post_logout_redirect_uris: &[],
35 backchannel_logout_uri: None,
36 allowed_scopes: "directory.read files.service",
37 service_key: Some("in"),
38 })
39 .await
40 .expect("client");
41 Fixture {
42 _dir: dir,
43 state,
44 client,
45 }
46}
47
48/// The claims of a client-credentials token that `im` would issue to `client` for `aud` and `scope`.
49fn machine_claims(state: &AppState, client: &Client, aud: &str, scope: &str) -> Value {
50 let now = unix_now();
51 json!({
52 "iss": state.config.base_url,
53 "sub": format!("client:{}", client.id),
54 "azp": client.id,
55 "aud": aud,
56 "scope": scope,
57 "svc": "in",
58 "iat": now,
59 "exp": now + 600,
60 "jti": generate_token(),
61 })
62}
63
64/// Signs `claims` with the instance's own key.
65async fn mint(state: &AppState, claims: &Value) -> String {
66 signing::sign(&state.store, &state.key, claims)
67 .await
68 .expect("token")
69}
70
71/// Sends a GET with a bearer token through the whole router: status, headers and body text.
72async fn get(state: &AppState, path: &str, token: &str) -> (StatusCode, http::HeaderMap, String) {
73 let request = http::Request::get(path)
74 .header(AUTHORIZATION, format!("Bearer {token}"))
75 .body(Body::empty())
76 .expect("request");
77 let response = router(state.clone()).handle(request).await;
78 let status = response.status();
79 let headers = response.headers().clone();
80 let bytes = to_bytes(response.into_body(), 1024 * 1024)
81 .await
82 .expect("body");
83 (
84 status,
85 headers,
86 String::from_utf8_lossy(&bytes).into_owned(),
87 )
88}
89
90#[tokio::test]
91async fn a_user_access_token_is_refused_on_the_directory() {
92 let f = fixture().await;
93 // A person's access token: their id as `sub`, a session as `sid`, and no `svc`.
94 let claims = json!({
95 "iss": f.state.config.base_url,
96 "sub": new_id(),
97 "aud": "im",
98 "azp": f.client.id,
99 "scope": "openid profile",
100 "sid": new_id(),
101 "iat": unix_now(),
102 "exp": unix_now() + 600,
103 });
104 let token = mint(&f.state, &claims).await;
105 let (status, headers, _) = get(&f.state, "/api/v1/directory", &token).await;
106 assert_eq!(status, StatusCode::UNAUTHORIZED);
107 assert_eq!(headers[WWW_AUTHENTICATE], "Bearer error=\"invalid_token\"");
108}
109
110#[tokio::test]
111async fn a_token_for_another_audience_is_refused() {
112 let f = fixture().await;
113 let claims = machine_claims(&f.state, &f.client, "in", "directory.read");
114 let token = mint(&f.state, &claims).await;
115 let (status, _, _) = get(&f.state, "/api/v1/directory", &token).await;
116 assert_eq!(status, StatusCode::UNAUTHORIZED);
117}
118
119#[tokio::test]
120async fn a_token_without_the_directory_scope_gets_403() {
121 let f = fixture().await;
122 let claims = machine_claims(&f.state, &f.client, "im", "files.service");
123 let token = mint(&f.state, &claims).await;
124 let (status, headers, body) = get(&f.state, "/api/v1/directory", &token).await;
125 assert_eq!(status, StatusCode::FORBIDDEN);
126 assert_eq!(
127 headers[WWW_AUTHENTICATE],
128 "Bearer error=\"insufficient_scope\""
129 );
130 assert!(body.contains("insufficient_scope"), "{body}");
131}
132
133#[tokio::test]
134async fn a_cursor_from_the_future_gets_a_resync_and_the_stream_ends() {
135 let f = fixture().await;
136 let claims = machine_claims(&f.state, &f.client, "im", "directory.read");
137 let token = mint(&f.state, &claims).await;
138 let (status, _, body) = get(&f.state, "/api/v1/directory/stream?cursor=999", &token).await;
139 assert_eq!(status, StatusCode::OK);
140 assert!(body.contains("event: resync"), "{body}");
141 assert!(!body.contains("event: member"), "{body}");
142}
143
144#[tokio::test]
145async fn a_photo_path_with_an_encoded_slash_is_not_found() {
146 let f = fixture().await;
147 let claims = machine_claims(&f.state, &f.client, "im", "directory.read");
148 let token = mint(&f.state, &claims).await;
149 let (status, _, _) = get(
150 &f.state,
151 "/api/v1/people/..%2f..%2fetc%2fpasswd/photo",
152 &token,
153 )
154 .await;
155 assert_eq!(status, StatusCode::NOT_FOUND);
156}
157
158#[tokio::test]
159async fn a_token_of_a_disabled_client_is_refused() {
160 let f = fixture().await;
161 let claims = machine_claims(&f.state, &f.client, "im", "directory.read");
162 let token = mint(&f.state, &claims).await;
163 assert!(
164 f.state
165 .store
166 .disable_client(&f.client.id)
167 .await
168 .expect("disable")
169 );
170 let (status, _, _) = get(&f.state, "/api/v1/directory", &token).await;
171 assert_eq!(status, StatusCode::UNAUTHORIZED);
172}