Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
}
}
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading