chronoscope/lib/chronoscope/nts/certificate.ex

36 lines
1017 B
Elixir

defmodule Chronoscope.NTS.Certificate do
def expiration_date(certificate) do
{:Validity, _, {:utcTime, expiration}} =
certificate
|> X509.Certificate.from_der!()
|> X509.Certificate.validity()
cert_time_to_iso8601(expiration)
end
def cert_time_to_iso8601(cert_time) do
captures =
Regex.named_captures(
~r/^(?<year>\d\d)(?<month>\d\d)(?<day>\d\d)(?<hour>\d\d)(?<minute>\d\d)(?<second>\d\d)Z$/,
to_string(cert_time)
)
"#{short_year_to_full_year(captures["year"])}-#{captures["month"]}-#{captures["day"]}T#{captures["hour"]}:#{captures["minute"]}:#{captures["second"]}Z"
end
defp short_year_to_full_year(short_year) do
{century, current_year} =
DateTime.utc_now().year
|> to_string()
|> String.split_at(-2)
if String.to_integer(short_year) >= String.to_integer(current_year) do
century <> short_year
else
century
|> String.to_integer()
|> then(&"#{&1 + 1}#{short_year}")
end
end
end