diff --git a/composeApp/src/commonMain/kotlin/com/monster0506/divvy/data/api/HttpClientFactory.kt b/composeApp/src/commonMain/kotlin/com/monster0506/divvy/data/api/HttpClientFactory.kt index 558bd14..22f0ac0 100644 --- a/composeApp/src/commonMain/kotlin/com/monster0506/divvy/data/api/HttpClientFactory.kt +++ b/composeApp/src/commonMain/kotlin/com/monster0506/divvy/data/api/HttpClientFactory.kt @@ -54,7 +54,9 @@ fun createHttpClient( } defaultRequest { url(config.baseUrl.trimEnd('/') + "/") - header(API_KEY_HEADER, config.apiKey) + // With no key (a fresh release install) the header is left out, so the server answers 401 and the + // app asks for the key. + if (config.apiKey.isNotBlank()) header(API_KEY_HEADER, config.apiKey) } } val client = if (engine != null) HttpClient(engine, configure) else HttpClient(configure) diff --git a/composeApp/src/commonTest/kotlin/com/monster0506/divvy/connection/CheckConnectionTest.kt b/composeApp/src/commonTest/kotlin/com/monster0506/divvy/connection/CheckConnectionTest.kt index 5ede190..e11d610 100644 --- a/composeApp/src/commonTest/kotlin/com/monster0506/divvy/connection/CheckConnectionTest.kt +++ b/composeApp/src/commonTest/kotlin/com/monster0506/divvy/connection/CheckConnectionTest.kt @@ -58,4 +58,20 @@ class CheckConnectionTest { val client = createHttpClient(ApiConfig("http://test", "key"), MockEngine { error("connection refused") }) assertEquals(ConnectionStatus.Unreachable, client.use { it.checkConnection() }) } + + /** A fresh release install has no key; servers before #309 answered an empty key header with a 500. */ + @Test + fun noKeyIsReportedSoTheAppAsksForIt() = runTest { + val client = createHttpClient( + ApiConfig("http://test", ""), + MockEngine { request -> + when { + request.url.encodedPath == "/health" -> respond("""{"status":"ok"}""", HttpStatusCode.OK) + API_KEY_HEADER in request.headers -> respond("", HttpStatusCode.InternalServerError) + else -> respond("", HttpStatusCode.Unauthorized) + } + }, + ) + assertEquals(ConnectionStatus.KeyRejected, client.use { it.checkConnection() }) + } } diff --git a/docs/api.md b/docs/api.md index 0a62145..4f04f33 100644 --- a/docs/api.md +++ b/docs/api.md @@ -3,7 +3,7 @@ - GET /households/{id} -> 200 | 404 - PATCH /households/{id} {"name": "..."} -> 200 | 400 | 404 Name is trimmed and must be 1-80 characters (400 otherwise). Unknown or malformed ids return 404. -Errors use the standard ApiError body. All routes require the X-Api-Key header. +Errors use the standard ApiError body. All household routes require the `X-Api-Key` header; missing, empty, whitespace-only or incorrect keys return 401. `GET /auth/check` returns 204 for an accepted key and 401 otherwise. Clients with no key should omit the header until one is entered. People are returned as `{"id", "householdId", "displayName", "archivedAt", "createdAt", "updatedAt", "venmoUsername", "shortName", "emoji", "color", "photoUpdatedAt"}`; `venmoUsername`, `shortName`, `emoji`, `color` and `photoUpdatedAt` diff --git a/server/src/main/kotlin/com/monster0506/divvy/server/Application.kt b/server/src/main/kotlin/com/monster0506/divvy/server/Application.kt index 11eb76e..d5ab2c1 100644 --- a/server/src/main/kotlin/com/monster0506/divvy/server/Application.kt +++ b/server/src/main/kotlin/com/monster0506/divvy/server/Application.kt @@ -97,7 +97,8 @@ fun Application.module(connectDatabase: Boolean = true) { install(Authentication) { bearer(API_KEY_AUTH) { authHeader { call -> - call.request.header(API_KEY_HEADER)?.let { HttpAuthHeader.Single("Bearer", it) } + // An empty header counts as no key; Bearer with an empty token would otherwise fail with a 500. + call.request.header(API_KEY_HEADER)?.takeIf { it.isNotBlank() }?.let { HttpAuthHeader.Single("Bearer", it) } } authenticate { credential -> val expectedKey = config.apiKey diff --git a/server/src/test/kotlin/com/monster0506/divvy/server/ApplicationTest.kt b/server/src/test/kotlin/com/monster0506/divvy/server/ApplicationTest.kt index 6f5c112..968ff72 100644 --- a/server/src/test/kotlin/com/monster0506/divvy/server/ApplicationTest.kt +++ b/server/src/test/kotlin/com/monster0506/divvy/server/ApplicationTest.kt @@ -119,11 +119,13 @@ class ApplicationTest { } @Test - fun authCheckReturns401WithWrongOrMissingKey() = testApplication { + fun authCheckReturns401WithWrongMissingOrBlankKey() = testApplication { testConfig() application { module(connectDatabase = false) } assertEquals(HttpStatusCode.Unauthorized, client.get("/auth/check") { header("X-Api-Key", "nope") }.status) assertEquals(HttpStatusCode.Unauthorized, client.get("/auth/check").status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/auth/check") { header("X-Api-Key", "") }.status) + assertEquals(HttpStatusCode.Unauthorized, client.get("/auth/check") { header("X-Api-Key", " ") }.status) } @Test