Commit 484783abb48e6305a8d814e2866728df1e708c3b

Thomas de Grivel 2022-12-01T15:12:44

refactor totp

diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 4477485..af02699 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -365,18 +365,7 @@ defmodule Kmxgit.UserManager do
     end
   end
 
-  @doc """
-  Returns a URL that be rendered with a QR code.
-  It meets the Google Authenticator specification
-  at https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
-  ## Examples
-      iex> generate_totp_enrolment_url(user)
-  """
-  def totp_enrolment_url(%User{email: email, totp_secret: secret}) do
-    "otpauth://totp/kmxgit:#{email}?secret=#{secret}&issuer=kmxgit&algorithm=SHA1&digits=6&period=30"
-  end
-
-  def update_user_totp(user = %User{}, params) do
+  def totp_update(user = %User{}, params) do
     case user
     |> User.totp_changeset(params)
     |> Repo.update()
@@ -388,11 +377,11 @@ defmodule Kmxgit.UserManager do
     end
   end
 
-  def verify_user_totp(user = %User{}, token) do
+  def totp_verify(user = %User{}, token) do
     User.totp_verify(user, token || 0)
   end
 
-  def delete_user_totp(user = %User{}) do
+  def totp_delete(user = %User{}) do
     case user
     |> User.totp_changeset(:delete)
     |> Repo.update()
diff --git a/lib/kmxgit_web/controllers/user_controller.ex b/lib/kmxgit_web/controllers/user_controller.ex
index 0dc62ff..e2e550f 100644
--- a/lib/kmxgit_web/controllers/user_controller.ex
+++ b/lib/kmxgit_web/controllers/user_controller.ex
@@ -98,8 +98,19 @@ defmodule KmxgitWeb.UserController do
     "data:#{type};base64,#{Base.encode64(data)}"
   end
 
+  @doc """
+  Returns a URL that be rendered with a QR code.
+  It meets the Google Authenticator specification
+  at https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
+  ## Examples
+      iex> totp_enrolment_url(user)
+  """
+  def totp_enrolment_url(%User{email: email, totp_secret: secret}) do
+    "otpauth://totp/kmxgit:#{email}?secret=#{secret}&issuer=kmxgit&algorithm=SHA1&digits=6&period=30"
+  end
+
   defp totp_enrolment_qrcode_src(user) do
-    UserManager.totp_enrolment_url(user)
+    totp_enrolment_url(user)
     |> QRCodeEx.encode()
     |> QRCodeEx.svg()
     |> img_src_data("image/svg+xml")
@@ -128,7 +139,7 @@ defmodule KmxgitWeb.UserController do
     if params["login"] == User.login(current_user) do
       user = current_user
       IO.inspect(params)
-      case UserManager.update_user_totp(user, params["user"]) do
+      case UserManager.totp_update(user, params["user"]) do
         {:ok, user} ->
           conn
           |> put_flash(:info, "Enroled 2FA (TOTP) successfuly.")
@@ -151,7 +162,7 @@ defmodule KmxgitWeb.UserController do
     current_user = conn.assigns.current_user
     if params["login"] == User.login(current_user) do
       user = current_user
-      case UserManager.delete_user_totp(user) do
+      case UserManager.totp_delete(user) do
         {:ok, user} ->
           conn
           |> put_flash(:info, "Removed 2FA (TOTP) successfuly.")
diff --git a/lib/kmxgit_web/controllers/user_session_controller.ex b/lib/kmxgit_web/controllers/user_session_controller.ex
index c53a3be..95ada98 100644
--- a/lib/kmxgit_web/controllers/user_session_controller.ex
+++ b/lib/kmxgit_web/controllers/user_session_controller.ex
@@ -33,7 +33,7 @@ defmodule KmxgitWeb.UserSessionController do
     end
     totp = user_params["totp"]
     if user do
-      if user.totp_last == 0 || totp && UserManager.verify_user_totp(user, totp) do
+      if user.totp_last == 0 || totp && UserManager.totp_verify(user, totp) do
         UserAuth.log_in_user(conn, user, user_params)
       else
         changeset = UserManager.change_user(%User{}, user_params)
diff --git a/lib/kmxgit_web/router.ex b/lib/kmxgit_web/router.ex
index 232310d..cd5bfe6 100644
--- a/lib/kmxgit_web/router.ex
+++ b/lib/kmxgit_web/router.ex
@@ -31,7 +31,7 @@ defmodule KmxgitWeb.Router do
   end
 
   pipeline :recaptcha do
-    plug PlugRecaptcha2, recaptcha_secret: Application.get_env(:kmxgit, :recaptcha_secret)
+    plug PlugRecaptcha2, recaptcha_secret: Application.compile_env(:kmxgit, :recaptcha_secret)
   end
 
   pipeline :admin do