From 17772cfc4e43411e71ef0a44cbc080c5d052b326 Mon Sep 17 00:00:00 2001 From: Madhavendra Rathore Date: Fri, 15 May 2026 15:01:42 +0000 Subject: [PATCH] krn-napi-binding: reclassify OAuth token-exchange failures as Unauthenticated Per src/kernel_error.rs:74-77 doc-comment, Unauthenticated is the intended ErrorCode for "Server returned HTTP 401 (or OIDC discovery rejected the token request)". M2M and U2M token-exchange paths were using Error::io() (-> Internal), which is reserved for kernel-side I/O / parsing failures. The misclassification caused authentication failures to surface in NodeJS as HiveDriverError instead of AuthenticationError (D-015). Fix: m2m.rs:270 and u2m.rs:433 swap Error::io() -> Error::unauthenticated(). No kernel unit tests assert ErrorCode::Internal on these paths (verified by grep) so no test updates are needed. The U2M test `test_u2m_cache_miss_with_expired_refresh` asserts only `result.is_err()`, which remains true. Deferred (worth a follow-up): distinguish oauth2::RequestTokenError subtypes so 5xx token-endpoint failures get NetworkError while invalid_client / invalid_grant / unauthorized_client stay Unauthenticated. The current change correctly handles the dominant case (bad-credentials -> 400/401 from server) and matches the existing kernel doc-comment intent. Signed-off-by: Madhavendra Rathore --- src/auth/oauth/m2m.rs | 2 +- src/auth/oauth/u2m.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/auth/oauth/m2m.rs b/src/auth/oauth/m2m.rs index 1ba757f5..74e93ec8 100644 --- a/src/auth/oauth/m2m.rs +++ b/src/auth/oauth/m2m.rs @@ -287,7 +287,7 @@ impl AuthProvider for ClientCredentialsProvider { .request_async(http_client.inner()) .await .map_err(|e| { - Error::io() + Error::unauthenticated() .message(format!("M2M token exchange failed: {}", e)) .context("client credentials grant") })?; diff --git a/src/auth/oauth/u2m.rs b/src/auth/oauth/u2m.rs index f1c5d7e0..2d2e8b08 100644 --- a/src/auth/oauth/u2m.rs +++ b/src/auth/oauth/u2m.rs @@ -447,7 +447,7 @@ impl AuthProvider for AuthorizationCodeProvider { .request_async(http_client.inner()) .await .map_err(|e| { - Error::io() + Error::unauthenticated() .message(format!("U2M authorization code exchange failed: {}", e)) .context("authorization code grant") })?;