Merge remote-tracking branch 'remotes/origin/develop' into auth-improvements
[akkoma] / lib / pleroma / web / plugs / session_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.SessionAuthenticationPlug do
6 @moduledoc """
7 Authenticates user by session-stored `:user_id` and request-contained username.
8 Username can be provided via HTTP Basic Auth (the password is not checked and can be anything).
9 """
10
11 import Plug.Conn
12
13 alias Pleroma.Helpers.AuthHelper
14
15 def init(options) do
16 options
17 end
18
19 def call(%{assigns: %{user: %Pleroma.User{}}} = conn, _), do: conn
20
21 def call(conn, _) do
22 with saved_user_id <- get_session(conn, :user_id),
23 %{auth_user: %{id: ^saved_user_id}} <- conn.assigns do
24 conn
25 |> assign(:user, conn.assigns.auth_user)
26 |> AuthHelper.skip_oauth()
27 else
28 _ -> conn
29 end
30 end
31 end