Merge remote-tracking branch 'upstream/develop' into registration-workflow
[akkoma] / lib / pleroma / web / plugs / legacy_authentication_plug.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.Plugs.LegacyAuthenticationPlug do
6 import Plug.Conn
7
8 alias Pleroma.User
9
10 def init(options) do
11 options
12 end
13
14 def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
15
16 def call(
17 %{
18 assigns: %{
19 auth_user: %{password_hash: "$6$" <> _ = password_hash} = auth_user,
20 auth_credentials: %{password: password}
21 }
22 } = conn,
23 _
24 ) do
25 with ^password_hash <- :crypt.crypt(password, password_hash),
26 {:ok, user} <-
27 User.reset_password(auth_user, %{password: password, password_confirmation: password}) do
28 conn
29 |> assign(:auth_user, user)
30 |> assign(:user, user)
31 |> Pleroma.Web.Plugs.OAuthScopesPlug.skip_plug()
32 else
33 _ ->
34 conn
35 end
36 end
37
38 def call(conn, _) do
39 conn
40 end
41 end