From dfee3209342a71bb7ebc312fb7ddfed1c1a01dd1 Mon Sep 17 00:00:00 2001 From: Jens Nylander Date: Mon, 24 Aug 2026 20:36:25 +0200 Subject: [PATCH] api: treat a missing OAuth token as a 401, not a 500 The two repositories report 'no such row' differently: api key verify returns Auth, oauth get_by_hash returns NotFound. Matching only on Auth sent every unknown bearer token down the backend-unavailable path. --- crates/sentio-api/src/auth.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/sentio-api/src/auth.rs b/crates/sentio-api/src/auth.rs index e363008..1dea5f6 100644 --- a/crates/sentio-api/src/auth.rs +++ b/crates/sentio-api/src/auth.rs @@ -102,9 +102,15 @@ impl FromRequestParts for AuthContext { scopes: record.scopes, }) } - Err(sentio_core::error::SentioError::Auth(_)) => { - Err(ApiError::Auth("invalid or expired token".into())) - } + // A token that matches no row is a client error, not an + // outage. The two repositories disagree on how they say + // "no such row": PgApiKeyRepository::verify returns Auth, + // PgOAuthTokenRepository::get_by_hash returns NotFound. + // Both mean the same thing here. + Err( + sentio_core::error::SentioError::Auth(_) + | sentio_core::error::SentioError::NotFound { .. }, + ) => Err(ApiError::Auth("invalid or expired token".into())), Err(e) => { tracing::error!("oauth token lookup failed: {e}"); Err(ApiError::Internal( @@ -136,6 +142,7 @@ pub async fn auth_middleware( parts.extensions.insert(ctx); let req = axum::http::Request::from_parts(parts, body); Ok(next.run(req).await) +} // ────────────────────────────────────────────────────────────────────────────── // Bootstrap credential check