X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fplugs%2Fauthentication_plug.ex;h=089028d770cf2c8f8ac1e53e91ce143afe00e975;hb=5f625c91361f68186a95354bbcff108435ce4d07;hp=beb02eb88bc5150ca2102c9e2ae3044c726e40be;hpb=72f7baa6548dbd5cc5ae4c37d9c2e53126203449;p=akkoma diff --git a/lib/pleroma/plugs/authentication_plug.ex b/lib/pleroma/plugs/authentication_plug.ex index beb02eb88..089028d77 100644 --- a/lib/pleroma/plugs/authentication_plug.ex +++ b/lib/pleroma/plugs/authentication_plug.ex @@ -1,63 +1,51 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Plugs.AuthenticationPlug do alias Comeonin.Pbkdf2 import Plug.Conn alias Pleroma.User + require Logger - def init(options) do - options - end + def init(options), do: options - def call(%{assigns: %{user: %User{}}} = conn, _), do: conn - - def call(conn, opts) do - with {:ok, username, password} <- decode_header(conn), - {:ok, user} <- opts[:fetcher].(username), - saved_user_id <- get_session(conn, :user_id), - {:ok, verified_user} <- verify(user, password, saved_user_id) - do - conn - |> assign(:user, verified_user) - |> put_session(:user_id, verified_user.id) - else - _ -> conn |> halt_or_continue(opts) - end + def checkpw(password, "$6" <> _ = password_hash) do + :crypt.crypt(password, password_hash) == password_hash end - # Short-circuit if we have a cookie with the id for the given user. - defp verify(%{id: id} = user, _password, id) do - {:ok, user} + def checkpw(password, "$pbkdf2" <> _ = password_hash) do + Pbkdf2.checkpw(password, password_hash) end - defp verify(nil, _password, _user_id) do - Pbkdf2.dummy_checkpw - :error + def checkpw(_password, _password_hash) do + Logger.error("Password hash not recognized") + false end - defp verify(user, password, _user_id) do - if Pbkdf2.checkpw(password, user.password_hash) do - {:ok, user} - else - :error - end - end + def call(%{assigns: %{user: %User{}}} = conn, _), do: conn - defp decode_header(conn) do - with ["Basic " <> header] <- get_req_header(conn, "authorization"), - {:ok, userinfo} <- Base.decode64(header), - [username, password] <- String.split(userinfo, ":", parts: 2) - do - {:ok, username, password} + def call( + %{ + assigns: %{ + auth_user: %{password_hash: password_hash} = auth_user, + auth_credentials: %{password: password} + } + } = conn, + _ + ) do + if Pbkdf2.checkpw(password, password_hash) do + conn + |> assign(:user, auth_user) + else + conn end end - defp halt_or_continue(conn, %{optional: true}) do - conn |> assign(:user, nil) - end - - defp halt_or_continue(conn, _) do + def call(%{assigns: %{auth_credentials: %{password: _}}} = conn, _) do + Pbkdf2.dummy_checkpw() conn - |> put_resp_content_type("application/json") - |> send_resp(403, Poison.encode!(%{error: "Invalid credentials."})) - |> halt end + + def call(conn, _), do: conn end