Merge branch 'docs/direct_conversation_id' into 'develop'
[akkoma] / lib / pleroma / web / auth / ldap_authenticator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Auth.LDAPAuthenticator do
6 alias Pleroma.User
7
8 require Logger
9
10 import Pleroma.Web.Auth.Authenticator,
11 only: [fetch_credentials: 1, fetch_user: 1]
12
13 @behaviour Pleroma.Web.Auth.Authenticator
14 @base Pleroma.Web.Auth.PleromaAuthenticator
15
16 @connection_timeout 10_000
17 @search_timeout 10_000
18
19 defdelegate get_registration(conn), to: @base
20 defdelegate create_from_registration(conn, registration), to: @base
21 defdelegate handle_error(conn, error), to: @base
22 defdelegate auth_template, to: @base
23 defdelegate oauth_consumer_template, to: @base
24
25 def get_user(%Plug.Conn{} = conn) do
26 with {:ldap, true} <- {:ldap, Pleroma.Config.get([:ldap, :enabled])},
27 {:ok, {name, password}} <- fetch_credentials(conn),
28 %User{} = user <- ldap_user(name, password) do
29 {:ok, user}
30 else
31 {:error, {:ldap_connection_error, _}} ->
32 # When LDAP is unavailable, try default authenticator
33 @base.get_user(conn)
34
35 {:ldap, _} ->
36 @base.get_user(conn)
37
38 error ->
39 error
40 end
41 end
42
43 defp ldap_user(name, password) do
44 ldap = Pleroma.Config.get(:ldap, [])
45 host = Keyword.get(ldap, :host, "localhost")
46 port = Keyword.get(ldap, :port, 389)
47 ssl = Keyword.get(ldap, :ssl, false)
48 sslopts = Keyword.get(ldap, :sslopts, [])
49
50 options =
51 [{:port, port}, {:ssl, ssl}, {:timeout, @connection_timeout}] ++
52 if sslopts != [], do: [{:sslopts, sslopts}], else: []
53
54 case :eldap.open([to_charlist(host)], options) do
55 {:ok, connection} ->
56 try do
57 if Keyword.get(ldap, :tls, false) do
58 :application.ensure_all_started(:ssl)
59
60 case :eldap.start_tls(
61 connection,
62 Keyword.get(ldap, :tlsopts, []),
63 @connection_timeout
64 ) do
65 :ok ->
66 :ok
67
68 error ->
69 Logger.error("Could not start TLS: #{inspect(error)}")
70 end
71 end
72
73 bind_user(connection, ldap, name, password)
74 after
75 :eldap.close(connection)
76 end
77
78 {:error, error} ->
79 Logger.error("Could not open LDAP connection: #{inspect(error)}")
80 {:error, {:ldap_connection_error, error}}
81 end
82 end
83
84 defp bind_user(connection, ldap, name, password) do
85 uid = Keyword.get(ldap, :uid, "cn")
86 base = Keyword.get(ldap, :base)
87
88 case :eldap.simple_bind(connection, "#{uid}=#{name},#{base}", password) do
89 :ok ->
90 case fetch_user(name) do
91 %User{} = user ->
92 user
93
94 _ ->
95 register_user(connection, base, uid, name, password)
96 end
97
98 error ->
99 error
100 end
101 end
102
103 defp register_user(connection, base, uid, name, password) do
104 case :eldap.search(connection, [
105 {:base, to_charlist(base)},
106 {:filter, :eldap.equalityMatch(to_charlist(uid), to_charlist(name))},
107 {:scope, :eldap.wholeSubtree()},
108 {:attributes, ['mail', 'email']},
109 {:timeout, @search_timeout}
110 ]) do
111 {:ok, {:eldap_search_result, [{:eldap_entry, _, attributes}], _}} ->
112 with {_, [mail]} <- List.keyfind(attributes, 'mail', 0) do
113 params = %{
114 email: :erlang.list_to_binary(mail),
115 name: name,
116 nickname: name,
117 password: password,
118 password_confirmation: password
119 }
120
121 changeset = User.register_changeset(%User{}, params)
122
123 case User.register(changeset) do
124 {:ok, user} -> user
125 error -> error
126 end
127 else
128 _ ->
129 Logger.error("Could not find LDAP attribute mail: #{inspect(attributes)}")
130 {:error, :ldap_registration_missing_attributes}
131 end
132
133 error ->
134 error
135 end
136 end
137 end