diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/plugs/cookie_auth_plug.ex | 28 | ||||
-rw-r--r-- | lib/pleroma/web/plugs/ensure_user_key_plug.ex | 10 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 4 |
3 files changed, 33 insertions, 9 deletions
diff --git a/lib/pleroma/web/plugs/cookie_auth_plug.ex b/lib/pleroma/web/plugs/cookie_auth_plug.ex new file mode 100644 index 000000000..dd5153cd4 --- /dev/null +++ b/lib/pleroma/web/plugs/cookie_auth_plug.ex @@ -0,0 +1,28 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.Plugs.CookieAuthPlug do + alias Pleroma.User + import Plug.Conn + + def init(opts) do + opts + end + + # If the user is already assigned (by a bearer token, probably), skip ahead. + def call(%{assigns: %{user: _}} = conn, _), do: conn + + # Authenticate with a session cookie, if available. + # For staticly-rendered pages (like the OAuth form) + # this is the only way it can authenticate. + def call(conn, _) do + with user_id <- get_session(conn, :user_id), + true <- is_binary(user_id), + %User{} = user <- User.get_by_id(user_id) do + assign(conn, :user, user) + else + _ -> conn + end + end +end diff --git a/lib/pleroma/web/plugs/ensure_user_key_plug.ex b/lib/pleroma/web/plugs/ensure_user_key_plug.ex index 537ea8562..70d3091f0 100644 --- a/lib/pleroma/web/plugs/ensure_user_key_plug.ex +++ b/lib/pleroma/web/plugs/ensure_user_key_plug.ex @@ -3,7 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Plugs.EnsureUserKeyPlug do - alias Pleroma.User import Plug.Conn def init(opts) do @@ -13,12 +12,7 @@ defmodule Pleroma.Web.Plugs.EnsureUserKeyPlug do def call(%{assigns: %{user: _}} = conn, _), do: conn def call(conn, _) do - with user_id <- get_session(conn, :user_id), - true <- is_binary(user_id), - %User{} = user <- User.get_by_id(user_id) do - assign(conn, :user, user) - else - _ -> assign(conn, :user, nil) - end + conn + |> assign(:user, nil) end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d19519186..768d35528 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -33,7 +33,9 @@ defmodule Pleroma.Web.Router do pipeline :oauth do plug(:fetch_session) plug(Pleroma.Web.Plugs.OAuthPlug) + plug(Pleroma.Web.Plugs.CookieAuthPlug) plug(Pleroma.Web.Plugs.UserEnabledPlug) + plug(Pleroma.Web.Plugs.EnsureUserKeyPlug) end pipeline :expect_authentication do @@ -317,7 +319,7 @@ defmodule Pleroma.Web.Router do scope "/oauth", Pleroma.Web.OAuth do scope [] do - pipe_through([:oauth, :after_auth]) + pipe_through(:oauth) get("/authorize", OAuthController, :authorize) post("/authorize", OAuthController, :create_authorization) end |