X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fauth%2Fldap_authenticator.ex;h=f63a66c039fc56585b6b920f7420c4a160765dff;hb=12bb7fc0dc94068d8337f10fc337ab23c8c7e77e;hp=65abd7f38d3f285adf8a0fec1e8f395b17f782f8;hpb=e17a9a1f6680bfc464a1433fcff37b6d61cc5340;p=akkoma
diff --git a/lib/pleroma/web/auth/ldap_authenticator.ex b/lib/pleroma/web/auth/ldap_authenticator.ex
index 65abd7f38..f63a66c03 100644
--- a/lib/pleroma/web/auth/ldap_authenticator.ex
+++ b/lib/pleroma/web/auth/ldap_authenticator.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Auth.LDAPAuthenticator do
@@ -7,50 +7,39 @@ defmodule Pleroma.Web.Auth.LDAPAuthenticator do
require Logger
+ import Pleroma.Web.Auth.Authenticator,
+ only: [fetch_credentials: 1, fetch_user: 1]
+
@behaviour Pleroma.Web.Auth.Authenticator
@base Pleroma.Web.Auth.PleromaAuthenticator
@connection_timeout 10_000
@search_timeout 10_000
- defdelegate get_registration(conn, params), to: @base
-
- defdelegate create_from_registration(conn, params, registration), to: @base
-
- def get_user(%Plug.Conn{} = conn, params) do
- if Pleroma.Config.get([:ldap, :enabled]) do
- {name, password} =
- case params do
- %{"authorization" => %{"name" => name, "password" => password}} ->
- {name, password}
-
- %{"grant_type" => "password", "username" => name, "password" => password} ->
- {name, password}
- end
-
- case ldap_user(name, password) do
- %User{} = user ->
- {:ok, user}
+ 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 <- ldap_user(name, password) do
+ {:ok, user}
+ else
+ {:error, {:ldap_connection_error, _}} ->
+ # When LDAP is unavailable, try default authenticator
+ @base.get_user(conn)
- {:error, {:ldap_connection_error, _}} ->
- # When LDAP is unavailable, try default authenticator
- @base.get_user(conn, params)
+ {:ldap, _} ->
+ @base.get_user(conn)
- error ->
- error
- end
- else
- # Fall back to default authenticator
- @base.get_user(conn, params)
+ error ->
+ error
end
end
- def handle_error(%Plug.Conn{} = _conn, error) do
- error
- end
-
- def auth_template, do: nil
-
defp ldap_user(name, password) do
ldap = Pleroma.Config.get(:ldap, [])
host = Keyword.get(ldap, :host, "localhost")
@@ -98,7 +87,7 @@ defmodule Pleroma.Web.Auth.LDAPAuthenticator do
case :eldap.simple_bind(connection, "#{uid}=#{name},#{base}", password) do
:ok ->
- case User.get_by_nickname_or_email(name) do
+ case fetch_user(name) do
%User{} = user ->
user