This adds a GenServer which will keep an LDAP connection open and auto reconnect on failure with a 5 second wait between retries. Another benefit is this prevents parsing the Root CAs for every login attempt as we only need to do it once per connection.
32 lines
1 KiB
Elixir
32 lines
1 KiB
Elixir
# Pleroma: A lightweight social networking server
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
defmodule Pleroma.Web.Auth.LDAPAuthenticator do
|
|
alias Pleroma.User
|
|
|
|
import Pleroma.Web.Auth.Helpers, only: [fetch_credentials: 1]
|
|
|
|
@behaviour Pleroma.Web.Auth.Authenticator
|
|
@base Pleroma.Web.Auth.PleromaAuthenticator
|
|
|
|
defdelegate get_registration(conn), to: @base
|
|
defdelegate create_from_registration(conn, registration), to: @base
|
|
defdelegate handle_error(conn, error), to: @base
|
|
defdelegate auth_template, to: @base
|
|
defdelegate oauth_consumer_template, to: @base
|
|
|
|
def get_user(%Plug.Conn{} = conn) do
|
|
with {:ldap, true} <- {:ldap, Pleroma.Config.get([:ldap, :enabled])},
|
|
{:ok, {name, password}} <- fetch_credentials(conn),
|
|
%User{} = user <- GenServer.call(Pleroma.LDAP, {:bind_user, name, password}) do
|
|
{:ok, user}
|
|
else
|
|
{:ldap, _} ->
|
|
@base.get_user(conn)
|
|
|
|
error ->
|
|
error
|
|
end
|
|
end
|
|
end
|