From a0d8f791508a9bfe50fa05a70091b5503e7f80b6 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 1 Oct 2024 05:04:26 -0400 Subject: [PATCH] switch to JSON for new verify API response --- README.md | 3 +-- .../attestation/server/AttestationServer.java | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ab1bd237..bd245176 100644 --- a/README.md +++ b/README.md @@ -124,8 +124,7 @@ Standard attestation message in the same format as the Auditor app QR code. * Response body: -Returns space-separated values in plain text: ` `. Additional fields -may be added in the future. +Returns JSON with a `verifyInterval` field. ## Logging diff --git a/src/main/java/app/attestation/server/AttestationServer.java b/src/main/java/app/attestation/server/AttestationServer.java index f7c8c085..1e353636 100644 --- a/src/main/java/app/attestation/server/AttestationServer.java +++ b/src/main/java/app/attestation/server/AttestationServer.java @@ -1606,6 +1606,8 @@ public void handlePost(final HttpExchange exchange) throws IOException { private static class VerifyHandler extends AppPostHandler { @Override public void handlePost(final HttpExchange exchange) throws IOException, SQLiteException { + final boolean legacy = exchange.getRequestURI().toString().equals("/verify"); + String authorization = null; try { authorization = getRequestHeaderValue(exchange, "Authorization"); @@ -1669,11 +1671,22 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx return; } - final byte[] result = (BaseEncoding.base64().encode(currentSubscribeKey) + " " + - verifyInterval).getBytes(); - exchange.sendResponseHeaders(200, result.length); - try (final OutputStream output = exchange.getResponseBody()) { - output.write(result); + if (legacy) { + final byte[] result = (BaseEncoding.base64().encode(currentSubscribeKey) + " " + + verifyInterval).getBytes(); + exchange.sendResponseHeaders(200, result.length); + try (final OutputStream output = exchange.getResponseBody()) { + output.write(result); + } + } else { + final JsonObjectBuilder result = Json.createObjectBuilder(); + result.add("verifyInterval", verifyInterval); + exchange.getResponseHeaders().set("Content-Type", "application/json"); + exchange.sendResponseHeaders(200, 0); + try (final OutputStream output = exchange.getResponseBody(); + final JsonWriter writer = Json.createWriter(output)) { + writer.write(result.build()); + } } } }