1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.RemoteFollowController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Activity
12 alias Pleroma.Object.Fetcher
14 alias Pleroma.Web.Auth.TOTPAuthenticator
15 alias Pleroma.Web.Auth.WrapperAuthenticator
16 alias Pleroma.Web.CommonAPI
18 @status_types ["Article", "Event", "Note", "Video", "Page", "Question"]
20 plug(Pleroma.Web.Plugs.FederatingPlug)
22 # Note: follower can submit the form (with password auth) not being signed in (having no token)
24 Pleroma.Web.Plugs.OAuthScopesPlug,
25 %{fallback: :proceed_unauthenticated, scopes: ["follow", "write:follows"]}
26 when action in [:do_follow]
29 # GET /ostatus_subscribe
31 def follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
33 true -> follow_status(conn, user, acct)
34 _ -> follow_account(conn, user, acct)
38 defp follow_status(conn, _user, acct) do
39 with {:ok, object} <- Fetcher.fetch_object_from_id(acct),
40 %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(object.data["id"]) do
41 redirect(conn, to: Routes.o_status_path(conn, :notice, activity_id))
44 handle_follow_error(conn, error)
48 defp follow_account(conn, user, acct) do
49 with {:ok, followee} <- User.get_or_fetch(acct) do
50 render(conn, follow_template(user), %{error: false, followee: followee, acct: acct})
53 render(conn, follow_template(user), %{error: :error})
57 defp follow_template(%User{} = _user), do: "follow.html"
58 defp follow_template(_), do: "follow_login.html"
61 case Fetcher.fetch_and_contain_remote_object_from_id(acct) do
62 {:ok, %{"type" => type}} when type in @status_types ->
70 # POST /ostatus_subscribe
72 # adds a remote account in followers if user already is signed in.
74 def do_follow(%{assigns: %{user: %User{} = user}} = conn, %{"user" => %{"id" => id}}) do
75 with {:fetch_user, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
76 {:ok, _, _, _} <- CommonAPI.follow(user, followee) do
77 redirect(conn, to: "/users/#{followee.id}")
80 handle_follow_error(conn, error)
84 # POST /ostatus_subscribe
87 # checks login\password and displays step 2 form of MFA if need.
89 def do_follow(conn, %{"authorization" => %{"name" => _, "password" => _, "id" => id}}) do
90 with {_, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
91 {_, {:ok, user}, _} <- {:auth, WrapperAuthenticator.get_user(conn), followee},
92 {_, _, _, false} <- {:mfa_required, followee, user, MFA.require?(user)},
93 {:ok, _, _, _} <- CommonAPI.follow(user, followee) do
94 redirect(conn, to: "/users/#{followee.id}")
97 handle_follow_error(conn, error)
101 # POST /ostatus_subscribe
104 # checks TOTP code. otherwise displays form with errors
106 def do_follow(conn, %{"mfa" => %{"code" => code, "token" => token, "id" => id}}) do
107 with {_, %User{} = followee} <- {:fetch_user, User.get_cached_by_id(id)},
108 {_, _, {:ok, %{user: user}}} <- {:mfa_token, followee, MFA.Token.validate(token)},
109 {_, _, _, {:ok, _}} <-
110 {:verify_mfa_code, followee, token, TOTPAuthenticator.verify(code, user)},
111 {:ok, _, _, _} <- CommonAPI.follow(user, followee) do
112 redirect(conn, to: "/users/#{followee.id}")
115 handle_follow_error(conn, error)
119 def do_follow(%{assigns: %{user: nil}} = conn, _) do
120 Logger.debug("Insufficient permissions: follow | write:follows.")
121 render(conn, "followed.html", %{error: "Insufficient permissions: follow | write:follows."})
124 # GET /authorize_interaction
126 def authorize_interaction(conn, %{"uri" => uri}) do
128 |> redirect(to: Routes.remote_follow_path(conn, :follow, %{acct: uri}))
131 defp handle_follow_error(conn, {:mfa_token, followee, _} = _) do
132 render(conn, "follow_login.html", %{error: "Wrong username or password", followee: followee})
135 defp handle_follow_error(conn, {:verify_mfa_code, followee, token, _} = _) do
136 render(conn, "follow_mfa.html", %{
137 error: "Wrong authentication code",
143 defp handle_follow_error(conn, {:mfa_required, followee, user, _} = _) do
144 {:ok, %{token: token}} = MFA.Token.create(user)
145 render(conn, "follow_mfa.html", %{followee: followee, mfa_token: token, error: false})
148 defp handle_follow_error(conn, {:auth, _, followee} = _) do
149 render(conn, "follow_login.html", %{error: "Wrong username or password", followee: followee})
152 defp handle_follow_error(conn, {:fetch_user, error} = _) do
153 Logger.debug("Remote follow failed with error #{inspect(error)}")
154 render(conn, "followed.html", %{error: "Could not find user"})
157 defp handle_follow_error(conn, {:error, "Could not follow user:" <> _} = _) do
158 render(conn, "followed.html", %{error: "Error following account"})
161 defp handle_follow_error(conn, error) do
162 Logger.debug("Remote follow failed with error #{inspect(error)}")
163 render(conn, "followed.html", %{error: "Something went wrong."})