Commit c5f0b131034bdd432201439b0bceea26f946b274

Thomas de Grivel 2022-02-04T13:25:21

fix warnings, remove unused Guardian code

diff --git a/config/.guardian.secret_key b/config/.guardian.secret_key
deleted file mode 100644
index cd6fc54..0000000
--- a/config/.guardian.secret_key
+++ /dev/null
@@ -1 +0,0 @@
-sdkjfhlkLKJHDjhbq/sfjhbsqef394P84FDSQ9283U4SDK//FGUHksl+jdhfglsiudhrfglkuh/3LOIRUd
\ No newline at end of file
diff --git a/config/config.exs b/config/config.exs
index ae90067..f639307 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -28,10 +28,6 @@ config :kmxgit, KmxgitWeb.Endpoint,
   pubsub_server: Kmxgit.PubSub,
   live_view: [signing_salt: "0HhihW2z"]
 
-config :kmxgit, Kmxgit.UserManager.Guardian,
-  issuer: "kmxgit",
-  secret_key: File.read!("config/.guardian.secret_key")
-
 config :dart_sass,
   path: "sass",
   version: "1.39.0",
diff --git a/lib/discord.ex b/lib/discord.ex
index 863fcee..a4f84fa 100644
--- a/lib/discord.ex
+++ b/lib/discord.ex
@@ -20,17 +20,31 @@ defmodule Discord do
     else
       inspect(params.reason)
     end
-    stack = Stack.to_string(params.stack)
-    headers = headers_to_string(conn.req_headers)
-    message = %{content: """
-URI : `#{req_path}`
-User : `#{user}`
-#{params.kind}
-```#{reason}```
-"""}
+    stack = Errors.stack_to_string(params.stack)
+    no_reason = error_message(req_path, user, params, "", nil)
+    reason_max_len = 2000 - String.length(no_reason)
+    discord_reason = reason |> String.slice(0..reason_max_len)
+    no_stack = error_message(req_path, user, params, discord_reason, nil)
+    stack_max_len = 2000 - String.length(no_stack) - 8
+    content = if stack_max_len > 0 do
+      stack = stack |> String.slice(0..stack_max_len)
+      error_message(req_path, user, params, discord_reason, stack)
+    else
+      no_stack
+    end
+    message = %{content: content}
     IO.inspect(message)
     json = Jason.encode!(message)
     HTTPoison.post(webhook, json, [{"Content-Type", "application/json"}])
     |> IO.inspect()
   end
+
+  def error_message(req_path, user, params, reason, stack) do
+    """
+URI : `#{req_path}`
+User : `#{user}`
+#{params.kind}
+```#{reason}```#{if stack do "\n\n```#{stack}```" end}
+"""
+  end
 end
diff --git a/lib/errors.ex b/lib/errors.ex
new file mode 100644
index 0000000..60ee837
--- /dev/null
+++ b/lib/errors.ex
@@ -0,0 +1,17 @@
+defmodule Errors do
+
+  def stack_to_string(stack), do: stack_to_string(stack, [])
+
+  def stack_to_string([], acc), do: acc |> Enum.reverse() |> Enum.join("\n")
+  def stack_to_string([{module, fun, arity, [file: file, line: line]} | rest], acc) do
+    str = "#{module}.#{fun}/#{arity}\n    #{file}:#{line}"
+    stack_to_string(rest, [str | acc])
+  end
+  def stack_to_string([{module, fun, arity, _} | rest], acc) do
+    str = "#{inspect(module)}.#{inspect(fun)}/#{inspect(arity)}"
+    stack_to_string(rest, [str | acc])
+  end
+  def stack_to_string([elt | rest], acc) do
+    stack_to_string(rest, [inspect(elt) | acc])
+  end
+end
diff --git a/lib/kmxgit/plug/current_user.ex b/lib/kmxgit/plug/current_user.ex
deleted file mode 100644
index 922fdaa..0000000
--- a/lib/kmxgit/plug/current_user.ex
+++ /dev/null
@@ -1,9 +0,0 @@
-defmodule Kmxgit.Plug.CurrentUser do
-  import Plug.Conn
-
-  def init(default), do: default
-
-  def call(conn, _) do
-    assign(conn, :current_user, Guardian.Plug.current_resource(conn))
-  end
-end
diff --git a/lib/kmxgit/user_manager/guardian.ex b/lib/kmxgit/user_manager/guardian.ex
deleted file mode 100644
index fd9dfab..0000000
--- a/lib/kmxgit/user_manager/guardian.ex
+++ /dev/null
@@ -1,16 +0,0 @@
-defmodule Kmxgit.UserManager.Guardian do
-  use Guardian, otp_app: :kmxgit
-
-  alias Kmxgit.UserManager
-
-  def subject_for_token(user, _claims) do
-    {:ok, to_string(user.id)}
-  end
-
-  def resource_from_claims(%{"sub" => id}) do
-    case UserManager.get_user(id) do
-      nil -> {:error, :resource_not_found}
-      user -> {:ok, user}
-    end
-  end
-end
diff --git a/lib/kmxgit/user_manager/pipeline.ex b/lib/kmxgit/user_manager/pipeline.ex
deleted file mode 100644
index b88ddc0..0000000
--- a/lib/kmxgit/user_manager/pipeline.ex
+++ /dev/null
@@ -1,14 +0,0 @@
-defmodule Kmxgit.UserManager.Pipeline do
-  use Guardian.Plug.Pipeline,
-    otp_app: :kmxgit,
-    error_handler: KmxgitWeb.SessionController,
-    module: Kmxgit.UserManager.Guardian
-
-  # If there is a session token, restrict it to an access token and validate it
-  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
-  # If there is an authorization header, restrict it to an access token and validate it
-  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
-  # Load the user if either of the verifications worked
-  plug Guardian.Plug.LoadResource, allow_blank: true
-  plug Kmxgit.Plug.CurrentUser
-end
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index 3fdf459..120ed7b 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -5,11 +5,10 @@ defmodule KmxgitWeb.PageController do
 
   alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager
-  alias Kmxgit.Repo
   alias Kmxgit.RepositoryManager
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.UserManager
-  alias Kmxgit.UserManager.{Guardian, User}
+  alias Kmxgit.UserManager.User
   alias KmxgitWeb.UserAuth
 
   def auth(conn, _params) do
diff --git a/lib/stack.ex b/lib/stack.ex
deleted file mode 100644
index 9c5c9ca..0000000
--- a/lib/stack.ex
+++ /dev/null
@@ -1,17 +0,0 @@
-defmodule Stack do
-
-  def to_string(stack), do: to_string(stack, [])
-
-  def to_string([], acc), do: acc |> Enum.reverse() |> Enum.join("\n")
-  def to_string([{module, fun, arity, [file: file, line: line]} | rest], acc) do
-    str = "#{module}.#{fun}/#{arity}\n    #{file}:#{line}"
-    to_string(rest, [str | acc])
-  end
-  def to_string([{module, fun, arity, _} | rest], acc) do
-    str = "#{inspect(module)}.#{inspect(fun)}/#{inspect(arity)}"
-    to_string(rest, [str | acc])
-  end
-  def to_string([elt | rest], acc) do
-    to_string(rest, [inspect(elt) | acc])
-  end
-end
diff --git a/mix.exs b/mix.exs
index afc54ab..c9d6ff0 100644
--- a/mix.exs
+++ b/mix.exs
@@ -44,7 +44,6 @@ defmodule Kmxgit.MixProject do
       {:floki, ">= 0.30.0", only: :test},
       {:gen_smtp, "~> 1.1"},
       {:gettext, "~> 0.18"},
-      {:guardian, "~> 2.0"},
       {:jason, "~> 1.2"},
       {:mogrify, "~> 0.9.1"},
       {:phoenix, "~> 1.6.2"},