Reorganize the LDAP module

This commit is contained in:
Mark Felder 2024-10-11 15:51:56 -04:00
parent 6bc70b8b2a
commit 1da057e6a4

View file

@ -15,6 +15,14 @@ defmodule Pleroma.LDAP do
GenServer.start_link(__MODULE__, [], name: __MODULE__) GenServer.start_link(__MODULE__, [], name: __MODULE__)
end end
def bind_user(name, password) do
GenServer.call(__MODULE__, {:bind_user, name, password})
end
def change_password(name, password, new_password) do
GenServer.call(__MODULE__, {:change_password, name, password, new_password})
end
@impl true @impl true
def init(state) do def init(state) do
case {Config.get(Pleroma.Web.Auth.Authenticator), Config.get([:ldap, :enabled])} do case {Config.get(Pleroma.Web.Auth.Authenticator), Config.get([:ldap, :enabled])} do
@ -47,33 +55,16 @@ defmodule Pleroma.LDAP do
def handle_info(:connect, _state), do: do_handle_connect() def handle_info(:connect, _state), do: do_handle_connect()
def handle_info({:bind_after_reconnect, name, password, from}, state) do def handle_info({:bind_after_reconnect, name, password, from}, state) do
result = bind_user(state[:handle], name, password) result = do_bind_user(state[:handle], name, password)
GenServer.reply(from, result) GenServer.reply(from, result)
{:noreply, state} {:noreply, state}
end end
defp do_handle_connect do
state =
case connect() do
{:ok, handle} ->
:eldap.controlling_process(handle, self())
Process.link(handle)
[handle: handle]
_ ->
Logger.error("Failed to connect to LDAP. Retrying in 5000ms")
Process.send_after(self(), :connect, 5_000)
[]
end
{:noreply, state}
end
@impl true @impl true
def handle_call({:bind_user, name, password}, from, state) do def handle_call({:bind_user, name, password}, from, state) do
case bind_user(state[:handle], name, password) do case do_bind_user(state[:handle], name, password) do
:needs_reconnect -> :needs_reconnect ->
Process.send(self(), {:bind_after_reconnect, name, password, from}, []) Process.send(self(), {:bind_after_reconnect, name, password, from}, [])
{:noreply, state, {:continue, :connect}} {:noreply, state, {:continue, :connect}}
@ -100,12 +91,21 @@ defmodule Pleroma.LDAP do
:ok :ok
end end
def bind_user(name, password) do defp do_handle_connect do
GenServer.call(__MODULE__, {:bind_user, name, password}) state =
end case connect() do
{:ok, handle} ->
:eldap.controlling_process(handle, self())
Process.link(handle)
[handle: handle]
def change_password(name, password, new_password) do _ ->
GenServer.call(__MODULE__, {:change_password, name, password, new_password}) Logger.error("Failed to connect to LDAP. Retrying in 5000ms")
Process.send_after(self(), :connect, 5_000)
[]
end
{:noreply, state}
end end
defp connect do defp connect do
@ -171,7 +171,7 @@ defmodule Pleroma.LDAP do
end end
end end
defp bind_user(handle, name, password) do defp do_bind_user(handle, name, password) do
dn = make_dn(name) dn = make_dn(name)
case :eldap.simple_bind(handle, dn, password) do case :eldap.simple_bind(handle, dn, password) do