1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Web.OAuth.Token
10 import ExUnit.CaptureLog
13 @skip if !Code.ensure_loaded?(:eldap), do: :skip
15 clear_config_all([:ldap, :enabled]) do
16 Pleroma.Config.put([:ldap, :enabled], true)
19 clear_config_all(Pleroma.Web.Auth.Authenticator) do
20 Pleroma.Config.put(Pleroma.Web.Auth.Authenticator, Pleroma.Web.Auth.LDAPAuthenticator)
24 test "authorizes the existing user using LDAP credentials" do
25 password = "testpassword"
26 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
27 app = insert(:oauth_app, scopes: ["read", "write"])
29 host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
30 port = Pleroma.Config.get([:ldap, :port])
35 open: fn [^host], [{:port, ^port}, {:ssl, false} | _] -> {:ok, self()} end,
36 simple_bind: fn _connection, _dn, ^password -> :ok end,
37 close: fn _connection ->
38 send(self(), :close_connection)
45 |> post("/oauth/token", %{
46 "grant_type" => "password",
47 "username" => user.nickname,
48 "password" => password,
49 "client_id" => app.client_id,
50 "client_secret" => app.client_secret
53 assert %{"access_token" => token} = json_response(conn, 200)
55 token = Repo.get_by(Token, token: token)
57 assert token.user_id == user.id
58 assert_received :close_connection
63 test "creates a new user after successful LDAP authorization" do
64 password = "testpassword"
66 app = insert(:oauth_app, scopes: ["read", "write"])
68 host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
69 port = Pleroma.Config.get([:ldap, :port])
74 open: fn [^host], [{:port, ^port}, {:ssl, false} | _] -> {:ok, self()} end,
75 simple_bind: fn _connection, _dn, ^password -> :ok end,
76 equalityMatch: fn _type, _value -> :ok end,
77 wholeSubtree: fn -> :ok end,
78 search: fn _connection, _options ->
80 {:eldap_search_result, [{:eldap_entry, '', [{'mail', [to_charlist(user.email)]}]}],
83 close: fn _connection ->
84 send(self(), :close_connection)
91 |> post("/oauth/token", %{
92 "grant_type" => "password",
93 "username" => user.nickname,
94 "password" => password,
95 "client_id" => app.client_id,
96 "client_secret" => app.client_secret
99 assert %{"access_token" => token} = json_response(conn, 200)
101 token = Repo.get_by(Token, token: token) |> Repo.preload(:user)
103 assert token.user.nickname == user.nickname
104 assert_received :close_connection
109 test "falls back to the default authorization when LDAP is unavailable" do
110 password = "testpassword"
111 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
112 app = insert(:oauth_app, scopes: ["read", "write"])
114 host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
115 port = Pleroma.Config.get([:ldap, :port])
120 open: fn [^host], [{:port, ^port}, {:ssl, false} | _] -> {:error, 'connect failed'} end,
121 simple_bind: fn _connection, _dn, ^password -> :ok end,
122 close: fn _connection ->
123 send(self(), :close_connection)
132 |> post("/oauth/token", %{
133 "grant_type" => "password",
134 "username" => user.nickname,
135 "password" => password,
136 "client_id" => app.client_id,
137 "client_secret" => app.client_secret
140 assert %{"access_token" => token} = json_response(conn, 200)
142 token = Repo.get_by(Token, token: token)
144 assert token.user_id == user.id
147 assert log =~ "Could not open LDAP connection: 'connect failed'"
148 refute_received :close_connection
153 test "disallow authorization for wrong LDAP credentials" do
154 password = "testpassword"
155 user = insert(:user, password_hash: Comeonin.Pbkdf2.hashpwsalt(password))
156 app = insert(:oauth_app, scopes: ["read", "write"])
158 host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
159 port = Pleroma.Config.get([:ldap, :port])
164 open: fn [^host], [{:port, ^port}, {:ssl, false} | _] -> {:ok, self()} end,
165 simple_bind: fn _connection, _dn, ^password -> {:error, :invalidCredentials} end,
166 close: fn _connection ->
167 send(self(), :close_connection)
174 |> post("/oauth/token", %{
175 "grant_type" => "password",
176 "username" => user.nickname,
177 "password" => password,
178 "client_id" => app.client_id,
179 "client_secret" => app.client_secret
182 assert %{"error" => "Invalid credentials"} = json_response(conn, 400)
183 assert_received :close_connection