Assign :user whenever :user_id is in session, pass to OAuth form

This commit is contained in:
Alex Gleason 2020-07-19 15:48:26 -05:00
parent 0fc2f5346d
commit b829226cbf
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 36 additions and 4 deletions

View File

@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Plugs.EnsureUserKeyPlug do
alias Pleroma.User
import Plug.Conn
def init(opts) do
@ -12,7 +13,12 @@ defmodule Pleroma.Plugs.EnsureUserKeyPlug do
def call(%{assigns: %{user: _}} = conn, _), do: conn
def call(conn, _) do
conn
|> assign(:user, nil)
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
end
end

View File

@ -276,7 +276,7 @@ defmodule Pleroma.Web.Router do
scope "/oauth", Pleroma.Web.OAuth do
scope [] do
pipe_through(:oauth)
pipe_through([:oauth, :after_auth])
get("/authorize", OAuthController, :authorize)
end

View File

@ -4,8 +4,23 @@
defmodule Pleroma.Plugs.EnsureUserKeyPlugTest do
use Pleroma.Web.ConnCase, async: true
alias Pleroma.Plugs.EnsureUserKeyPlug
import Pleroma.Factory
@session_opts [
store: :cookie,
key: "_test",
signing_salt: "cooldude"
]
setup %{conn: conn} do
conn =
conn
|> Plug.Session.call(Plug.Session.init(@session_opts))
|> fetch_session()
%{conn: conn}
end
test "if the conn has a user key set, it does nothing", %{conn: conn} do
conn =
@ -19,6 +34,17 @@ defmodule Pleroma.Plugs.EnsureUserKeyPlugTest do
assert conn == ret_conn
end
test "if the session has a user_id, it sets the user", %{conn: conn} do
user = insert(:user)
conn =
conn
|> put_session(:user_id, user.id)
|> EnsureUserKeyPlug.call(%{})
assert conn.assigns[:user] == user
end
test "if the conn has no key set, it sets it to nil", %{conn: conn} do
conn =
conn