Commit be961aca69353491837b1b8a6add075243443cc8

Thomas de Grivel 2021-11-28T20:09:18

fix guardian, recaptcha for register

diff --git a/config/config.exs b/config/config.exs
index 68b1887..4075e09 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -8,6 +8,8 @@
 import Config
 
 config :kmxgit,
+  recaptcha_site_key: System.get_env("RECAPTCHA_SITE_KEY"),
+  recaptcha_secret: System.get_env("RECAPTCHA_SECRET"),
   ssh_url: "git@git.kmx.io",
   ecto_repos: [Kmxgit.Repo]
 
diff --git a/lib/kmxgit/user_manager/guardian.ex b/lib/kmxgit/user_manager/guardian.ex
index 9979b70..fd9dfab 100644
--- a/lib/kmxgit/user_manager/guardian.ex
+++ b/lib/kmxgit/user_manager/guardian.ex
@@ -8,9 +8,9 @@ defmodule Kmxgit.UserManager.Guardian do
   end
 
   def resource_from_claims(%{"sub" => id}) do
-    user = UserManager.get_user!(id)
-    {:ok, user}
-  rescue
-    Ecto.NoResultsError -> {:error, :resource_not_found}
+    case UserManager.get_user(id) do
+      nil -> {:error, :resource_not_found}
+      user -> {:ok, user}
+    end
   end
 end
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index d463098..ed6e00d 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -12,7 +12,7 @@ defmodule Kmxgit.UserManager.User do
     field :description, :string, null: true
     field :email, :string, unique: true
     field :encrypted_password, :string
-    field :is_admin, :boolean, null: false
+    field :is_admin, :boolean, null: false, default: false
     field :name, :string
     has_many :owned_repositories, Repository
     field :password, :string, virtual: true, redact: true
diff --git a/lib/kmxgit_web/controllers/registration_controller.ex b/lib/kmxgit_web/controllers/registration_controller.ex
index 82e3959..f14d918 100644
--- a/lib/kmxgit_web/controllers/registration_controller.ex
+++ b/lib/kmxgit_web/controllers/registration_controller.ex
@@ -18,11 +18,11 @@ defmodule KmxgitWeb.RegistrationController do
         |> Guardian.Plug.sign_in(user)
         |> redirect(to: Routes.slug_path(conn, :show, user.slug.slug))
       {:error, changeset} ->
+        IO.inspect(changeset)
         conn
         |> assign(:action, Routes.registration_path(conn, :register))
         |> assign(:changeset, changeset)
         |> render("new.html")
-                  
     end
   end
 end
diff --git a/lib/kmxgit_web/templates/registration/new.html.heex b/lib/kmxgit_web/templates/registration/new.html.heex
index a79ad95..8228fb8 100644
--- a/lib/kmxgit_web/templates/registration/new.html.heex
+++ b/lib/kmxgit_web/templates/registration/new.html.heex
@@ -35,6 +35,8 @@
       <%= error_tag f, :password_confirmation %>
     </div>
 
+    <%= render "recaptcha.html", assigns %>
+
     <div class="mb-3">
       <%= submit "Submit", class: "btn btn-primary" %>
     </div>
diff --git a/lib/kmxgit_web/templates/registration/recaptcha.html.heex b/lib/kmxgit_web/templates/registration/recaptcha.html.heex
new file mode 100644
index 0000000..1bb0939
--- /dev/null
+++ b/lib/kmxgit_web/templates/registration/recaptcha.html.heex
@@ -0,0 +1,9 @@
+<%= tag :input, type: 'hidden', name: 'recaptcha' %>
+<script src={"https://www.google.com/recaptcha/api.js?render=#{recaptcha_site_key()}"}></script>
+<script>
+ grecaptcha.ready(function() {
+   grecaptcha.execute('<%= recaptcha_site_key() %>', {action: 'register'}).then(function(token) {
+     $('input[name="recaptcha"]').val(token);
+   });
+ });
+</script>
diff --git a/lib/kmxgit_web/views/registration_view.ex b/lib/kmxgit_web/views/registration_view.ex
index c70837e..f743ad7 100644
--- a/lib/kmxgit_web/views/registration_view.ex
+++ b/lib/kmxgit_web/views/registration_view.ex
@@ -1,3 +1,7 @@
 defmodule KmxgitWeb.RegistrationView do
   use KmxgitWeb, :view
+
+  def recaptcha_site_key do
+    Application.get_env :kmxgit, :recaptcha_site_key
+  end
 end