forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

mod.rs53 lines, 1.7 KiBHistoryRawDownload
1//! The machine API of `im` (`docs/contracts.md` sections 3 and 4): the people directory and its change
2//! stream, the profile photos, and the service switcher list.
3//!
4//! Every route here needs a client-credentials token that `im` issued to a family service, with the
5//! audience `im` and the scope the route needs (`machine_auth.rs`). Errors are JSON with a real status
6//! (contracts section 7): `{"error": ..., "message": ...}`.
7
8pub mod directory;
9pub mod family;
10pub mod machine_auth;
11pub mod photo;
12
13#[cfg(test)]
14mod tests;
15
16use http::header::WWW_AUTHENTICATE;
17use serde_json::json;
18use topcoat::router::response::Response;
19use topcoat::router::{HeaderValue, StatusCode};
20
21use crate::oidc::json_response;
22
23/// The `WWW-Authenticate` challenge of a token that is missing or not valid for this service.
24pub const TOKEN_CHALLENGE: &str = "Bearer error=\"invalid_token\"";
25
26/// The `WWW-Authenticate` challenge of a valid token that lacks the scope a route needs.
27pub const SCOPE_CHALLENGE: &str = "Bearer error=\"insufficient_scope\"";
28
29/// A machine error: the status, the stable word, a message for logs, and the challenge of a 401 or 403.
30pub fn api_error(
31 status: StatusCode,
32 error: &str,
33 message: &str,
34 challenge: Option<&'static str>,
35) -> Response {
36 let mut response = json_response(status, &json!({"error": error, "message": message}));
37 if let Some(challenge) = challenge {
38 response
39 .headers_mut()
40 .insert(WWW_AUTHENTICATE, HeaderValue::from_static(challenge));
41 }
42 response
43}
44
45/// `404 not_found` for a route that has no such thing.
46pub fn not_found() -> Response {
47 api_error(
48 StatusCode::NOT_FOUND,
49 "not_found",
50 "There is nothing at this address.",
51 None,
52 )
53}