From 4c89219fb89c83b293c759bab3b4a93656a16234 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Tue, 23 Aug 2022 18:16:45 +0100 Subject: [PATCH] extract get_or_exchange token --- .../controllers/auth_controller.ex | 10 +--------- lib/pleroma/web/o_auth/o_auth_controller.ex | 20 +++---------------- lib/pleroma/web/o_auth/token.ex | 8 ++++++++ 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex index 13e4e8bc8..a9ccaa982 100644 --- a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex @@ -22,21 +22,13 @@ defmodule Pleroma.Web.MastodonAPI.AuthController do @local_mastodon_name "Mastodon-Local" - defp get_or_exchange_token(%Authorization{} = auth, %App{} = app, %User{} = user) do - if auth.used do - Token.get_preeexisting_by_app_and_user(app, user) - else - Token.exchange_token(app, auth) - end - end - @doc "GET /web/login" # Local Mastodon FE login callback action def login(conn, %{"code" => auth_token} = params) do with {:ok, app} <- local_mastofe_app(), {:ok, auth} <- Authorization.get_by_token(app, auth_token), %User{} = user <- User.get_cached_by_id(auth.user_id), - {:ok, oauth_token} <- get_or_exchange_token(auth, app, user) do + {:ok, oauth_token} <- Token.get_or_exchange_token(auth, app, user) do redirect_to = conn |> local_mastodon_post_login_path() diff --git a/lib/pleroma/web/o_auth/o_auth_controller.ex b/lib/pleroma/web/o_auth/o_auth_controller.ex index 34ee218ab..455af11d7 100644 --- a/lib/pleroma/web/o_auth/o_auth_controller.ex +++ b/lib/pleroma/web/o_auth/o_auth_controller.ex @@ -291,23 +291,9 @@ def token_exchange(%Plug.Conn{} = conn, %{"grant_type" => "authorization_code"} with {:ok, app} <- Token.Utils.fetch_app(conn), fixed_token = Token.Utils.fix_padding(params["code"]), {:ok, auth} <- Authorization.get_by_token(app, fixed_token), - %User{} = user <- User.get_cached_by_id(auth.user_id) do - if auth.used do - # reuse token, we already have a valid one - with {:ok, token} <- Token.get_preeexisting_by_app_and_user(app, user) do - after_token_exchange(conn, %{user: user, token: token}) - else - error -> - handle_token_exchange_error(conn, error) - end - else - with {:ok, token} <- Token.exchange_token(app, auth) do - after_token_exchange(conn, %{user: user, token: token}) - else - error -> - handle_token_exchange_error(conn, error) - end - end + %User{} = user <- User.get_cached_by_id(auth.user_id), + {:ok, token} <- Token.get_or_exchange_token(auth, app, user) do + after_token_exchange(conn, %{user: user, token: token}) else error -> handle_token_exchange_error(conn, error) diff --git a/lib/pleroma/web/o_auth/token.ex b/lib/pleroma/web/o_auth/token.ex index 33bfd876d..c9398aeaa 100644 --- a/lib/pleroma/web/o_auth/token.ex +++ b/lib/pleroma/web/o_auth/token.ex @@ -96,6 +96,14 @@ defp put_refresh_token(changeset, attrs) do |> unique_constraint(:refresh_token) end + def get_or_exchange_token(%Authorization{} = auth, %App{} = app, %User{} = user) do + if auth.used do + get_preeexisting_by_app_and_user(app, user) + else + exchange_token(app, auth) + end + end + defp put_valid_until(changeset, attrs) do valid_until = Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), lifespan()))