//! Checks the client-credentials tokens that family services present to `im` (contracts 2.6). //! //! `im` verifies its own tokens: the signature against the published keys, `iss` exactly, `aud` exactly //! the string `im` (an array is refused, because `im` never issues one), `exp` and `iat`. Then the token //! must be a machine token: `sub` is `client:`, `svc` names a family key, and it carries no `sid`. //! The client it names must still exist, be enabled, and be registered under that family key. A user's //! access token and an ID token fail one of these checks, so they never open a machine route. use http::header::AUTHORIZATION; use im_core::jwt; use im_core::oauth::is_valid_family_key; use im_core::time::unix_now; use serde_json::Value; use topcoat::Result; use topcoat::context::Cx; use topcoat::router::StatusCode; use topcoat::router::request::headers; use topcoat::router::response::Response; use super::{SCOPE_CHALLENGE, TOKEN_CHALLENGE, api_error}; use crate::oidc::userinfo::bearer; use crate::oidc::{issuer, signing}; use crate::state::AppState; /// The scope of the people directory, its stream and the photos (contracts 2.6 and 3). pub const DIRECTORY_READ: &str = "directory.read"; /// The audience every directory, photo and family route requires. pub const MACHINE_AUDIENCE: &str = "im"; /// A verified machine caller. #[derive(Debug, Clone)] pub struct Machine { /// The client the token was issued to (`azp`). pub client_id: String, /// The family key of the caller (`svc`). pub caller: String, /// When the token expires, in Unix seconds. A stream ends no later than this. pub expires_at: i64, } /// Verifies the bearer token of the request for `scope`. The outer `Result` is a server failure; the /// inner one is the refusal, ready to send as the answer. pub async fn require( app: &AppState, cx: &Cx, scope: &str, ) -> Result> { let Some(token) = bearer(headers(cx).get(AUTHORIZATION)) else { return Ok(Err(refused())); }; let Some(claims) = signing::verify_access(&app.store, token).await? else { return Ok(Err(refused())); }; // The audience must be exactly the string `im`. `check_claims` also checks it, and the exp and iat. if jwt::single_audience(&claims) != Some(MACHINE_AUDIENCE) { return Ok(Err(refused())); } if jwt::check_claims(&claims, issuer(app), MACHINE_AUDIENCE, unix_now()).is_err() { return Ok(Err(refused())); } let text = |name: &str| claims.get(name).and_then(Value::as_str); let (Some(client_id), Some(sub), Some(caller), Some(expires_at)) = ( text("azp"), text("sub"), text("svc"), claims.get("exp").and_then(Value::as_i64), ) else { return Ok(Err(refused())); }; // A person's token carries a session (`sid`) and a person's id as `sub`; neither is a machine token. if sub != format!("client:{client_id}") || claims.contains_key("sid") || !is_valid_family_key(caller) { return Ok(Err(refused())); } let Some(client) = app.store.client_by_id(client_id).await? else { return Ok(Err(refused())); }; if client.is_disabled() || client.service_key.as_deref() != Some(caller) { return Ok(Err(refused())); } let granted = text("scope").unwrap_or_default(); if !granted.split_whitespace().any(|word| word == scope) { return Ok(Err(api_error( StatusCode::FORBIDDEN, "insufficient_scope", "The token does not grant the scope this route needs.", Some(SCOPE_CHALLENGE), ))); } Ok(Ok(Machine { client_id: client.id, caller: caller.to_owned(), expires_at, })) } /// `401 invalid_token`. One answer for every reason, so the reason is not disclosed. fn refused() -> Response { api_error( StatusCode::UNAUTHORIZED, "invalid_token", "The access token is missing, invalid, expired, or not issued for this service.", Some(TOKEN_CHALLENGE), ) }