From 0ebacbec94668abd8c3e2a82ce4cce86f76e8f94 Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Sun, 22 Jun 2025 19:52:59 +0200 Subject: [PATCH] initial commit --- .tool-versions | 2 ++ totp-code | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 .tool-versions create mode 100755 totp-code diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..2254cc0 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +elixir 1.18.1-otp-27 +erlang 27.2 diff --git a/totp-code b/totp-code new file mode 100755 index 0000000..f50b37e --- /dev/null +++ b/totp-code @@ -0,0 +1,57 @@ +#!/usr/bin/env -S ERL_FLAGS=+B elixir +Mix.install([{:nimble_totp, ">= 1.0.0"},{:yaml_elixir, "~> 2.9"}]) + +if System.get_env("DEPS_ONLY") == "true" do + System.halt(0) + Process.sleep(:infinity) +end + +defmodule TOTP do + @moduledoc """ + + ## Usage + + $ totp-code get-code identifier + + $ totp-code list-identifiers + + $ totp-code set-secret identifier secret # men at work + + """ + def main([]), do: cmd("help", []) + def main(argv) do + [command | args] = argv + cmd(command, args) + end + + defp cmd("get-code", [identifier]) do + get_secret(identifier) + |> Base.decode32!() + |> NimbleTOTP.verification_code() + |> IO.puts() + end + + defp cmd("list-identifiers", _) do + read_config() + |> Map.get("secrets") + |> Map.keys() + |> Enum.join("\n") + |> IO.puts() + end + + defp cmd("help", _), do: IO.puts(@moduledoc) + + defp read_config() do + System.user_home() + |> Path.join(".totp-code.conf") + |> YamlElixir.read_from_file!() + end + + defp get_secret(identifier) do + read_config() + |> Map.get("secrets") + |> Map.get(identifier) + end +end + +TOTP.main(System.argv())