1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.AuthenticationPlug do
6 alias Pleroma.Plugs.OAuthScopesPlug
13 def init(options), do: options
15 def checkpw(password, "$6" <> _ = password_hash) do
16 :crypt.crypt(password, password_hash) == password_hash
19 def checkpw(password, "$2" <> _ = password_hash) do
20 # Handle bcrypt passwords for Mastodon migration
21 Bcrypt.verify_pass(password, password_hash)
24 def checkpw(password, "$pbkdf2" <> _ = password_hash) do
25 Pbkdf2.verify_pass(password, password_hash)
28 def checkpw(_password, _password_hash) do
29 Logger.error("Password hash not recognized")
33 def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
38 auth_user: %{password_hash: password_hash} = auth_user,
39 auth_credentials: %{password: password}
44 if checkpw(password, password_hash) do
46 |> assign(:user, auth_user)
47 |> OAuthScopesPlug.skip_plug()
53 def call(%{assigns: %{auth_credentials: %{password: _}}} = conn, _) do
54 Pbkdf2.no_user_verify()
58 def call(conn, _), do: conn