X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fplugs%2Fauthentication_plug.ex;h=da4ed4226620215ea1ca50f3e98588fc5088aaeb;hb=f1890d2cacfa09dd22b06a8d041c04dbeba9e138;hp=90bd07b911bcca4dcb36872120b500fafcc0f562;hpb=142e8f8f3eea4915ea7d52123384c3d43454c098;p=akkoma diff --git a/lib/pleroma/plugs/authentication_plug.ex b/lib/pleroma/plugs/authentication_plug.ex index 90bd07b91..da4ed4226 100644 --- a/lib/pleroma/plugs/authentication_plug.ex +++ b/lib/pleroma/plugs/authentication_plug.ex @@ -1,51 +1,39 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Plugs.AuthenticationPlug do + alias Comeonin.Pbkdf2 import Plug.Conn + alias Pleroma.User def init(options) do options end - def call(conn, opts) do - with {:ok, username, password} <- decode_header(conn), - {:ok, user} <- opts[:fetcher].(username), - {:ok, verified_user} <- verify(user, password) - do - conn |> assign(:user, verified_user) - else - _ -> conn |> halt_or_continue(opts) - end - end - - defp verify(nil, _password) do - Comeonin.Pbkdf2.dummy_checkpw - :error - end + def call(%{assigns: %{user: %User{}}} = conn, _), do: conn - defp verify(user, password) do - if Comeonin.Pbkdf2.checkpw(password, user.password_hash) do - {:ok, user} + 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 - :error + conn end end - defp decode_header(conn) do - with ["Basic " <> header] <- get_req_header(conn, "authorization"), - {:ok, userinfo} <- Base.decode64(header), - [username, password] <- String.split(userinfo, ":") - do - { :ok, username, password } - 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