//! The machine API of `im` (`docs/contracts.md` sections 3 and 4): the people directory and its change //! stream, the profile photos, and the service switcher list. //! //! Every route here needs a client-credentials token that `im` issued to a family service, with the //! audience `im` and the scope the route needs (`machine_auth.rs`). Errors are JSON with a real status //! (contracts section 7): `{"error": ..., "message": ...}`. pub mod directory; pub mod family; pub mod machine_auth; pub mod photo; #[cfg(test)] mod tests; use http::header::WWW_AUTHENTICATE; use serde_json::json; use topcoat::router::response::Response; use topcoat::router::{HeaderValue, StatusCode}; use crate::oidc::json_response; /// The `WWW-Authenticate` challenge of a token that is missing or not valid for this service. pub const TOKEN_CHALLENGE: &str = "Bearer error=\"invalid_token\""; /// The `WWW-Authenticate` challenge of a valid token that lacks the scope a route needs. pub const SCOPE_CHALLENGE: &str = "Bearer error=\"insufficient_scope\""; /// A machine error: the status, the stable word, a message for logs, and the challenge of a 401 or 403. pub fn api_error( status: StatusCode, error: &str, message: &str, challenge: Option<&'static str>, ) -> Response { let mut response = json_response(status, &json!({"error": error, "message": message})); if let Some(challenge) = challenge { response .headers_mut() .insert(WWW_AUTHENTICATE, HeaderValue::from_static(challenge)); } response } /// `404 not_found` for a route that has no such thing. pub fn not_found() -> Response { api_error( StatusCode::NOT_FOUND, "not_found", "There is nothing at this address.", None, ) }