From cf8c677c251ca11b46ced27333a15277478d81a6 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Mon, 1 Apr 2024 16:22:54 -0400 Subject: [PATCH] Refactor code --- lib/chronoscope/nts.ex | 1 + .../nts/key_establishment_client.ex | 14 +-- .../nts/key_establishment_response.ex | 2 +- .../controllers/page_html/home.html.heex | 17 +++- test/chronoscope/nts/certificate_test.exs | 5 +- .../nts/key_establishment_response_test.exs | 86 ++++++++++--------- 6 files changed, 72 insertions(+), 53 deletions(-) diff --git a/lib/chronoscope/nts.ex b/lib/chronoscope/nts.ex index cd4edf9..2d76414 100644 --- a/lib/chronoscope/nts.ex +++ b/lib/chronoscope/nts.ex @@ -19,6 +19,7 @@ defmodule Chronoscope.NTS do |> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end) end + @impl true def key_establishment(host, port) do name = "#{host}:#{port}" diff --git a/lib/chronoscope/nts/key_establishment_client.ex b/lib/chronoscope/nts/key_establishment_client.ex index 67b606d..f7f5107 100644 --- a/lib/chronoscope/nts/key_establishment_client.ex +++ b/lib/chronoscope/nts/key_establishment_client.ex @@ -37,12 +37,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do receive do {:ssl, _socket, response} -> :ssl.close(socket) - - case KeyEstablishmentResponse.parse(response) do - {:ok, parsed_response} -> {:ok, Map.put(parsed_response, :cert_expiration, Certificate.expiration_date(peercert))} - # todo - indicate errors in server response - error -> error - end + parse_response(response, peercert) msg -> :ssl.close(socket) @@ -56,6 +51,13 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do end end + defp parse_response(response, peercert) do + response + |> KeyEstablishmentResponse.parse() + |> Map.put(:cert_expiration, Certificate.expiration_date(peercert)) + |> then(&{:ok, &1}) + end + defp handshake_failure_message(error) do cond do error =~ ~r/\{bad_cert,hostname_check_failed\}$/ -> diff --git a/lib/chronoscope/nts/key_establishment_response.ex b/lib/chronoscope/nts/key_establishment_response.ex index 19941a6..5a4bff6 100644 --- a/lib/chronoscope/nts/key_establishment_response.ex +++ b/lib/chronoscope/nts/key_establishment_response.ex @@ -17,7 +17,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do } def parse(response) do - {:ok, parse_response(response, %{})} + parse_response(response, %{}) end defp parse_response([], acc) do diff --git a/lib/chronoscope_web/controllers/page_html/home.html.heex b/lib/chronoscope_web/controllers/page_html/home.html.heex index 60b5231..cbd01fd 100644 --- a/lib/chronoscope_web/controllers/page_html/home.html.heex +++ b/lib/chronoscope_web/controllers/page_html/home.html.heex @@ -1,6 +1,12 @@ <.flash_group flash={@flash} />