1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AuthController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
11 alias Pleroma.Web.OAuth.App
12 alias Pleroma.Web.OAuth.Authorization
13 alias Pleroma.Web.OAuth.Token
14 alias Pleroma.Web.TwitterAPI.TwitterAPI
16 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
18 plug(Pleroma.Web.Plugs.RateLimiter, [name: :password_reset] when action == :password_reset)
20 @local_mastodon_name "Mastodon-Local"
23 def login(%{assigns: %{user: %User{}}} = conn, _params) do
24 redirect(conn, to: local_mastodon_root_path(conn))
27 # Local Mastodon FE login init action
28 def login(conn, %{"code" => auth_token}) do
29 with {:ok, app} <- get_or_make_app(),
30 {:ok, auth} <- Authorization.get_by_token(app, auth_token),
31 {:ok, token} <- Token.exchange_token(app, auth) do
33 |> put_session(:oauth_token, token.token)
34 |> redirect(to: local_mastodon_root_path(conn))
38 # Local Mastodon FE callback action
40 with {:ok, app} <- get_or_make_app() do
42 o_auth_path(conn, :authorize,
43 response_type: "code",
44 client_id: app.client_id,
46 scope: Enum.join(app.scopes, " ")
49 redirect(conn, to: path)
53 @doc "DELETE /auth/sign_out"
54 def logout(conn, _) do
60 @doc "POST /auth/password"
61 def password_reset(conn, params) do
62 nickname_or_email = params["email"] || params["nickname"]
64 TwitterAPI.password_reset(nickname_or_email)
66 json_response(conn, :no_content, "")
69 defp local_mastodon_root_path(conn) do
70 case get_session(conn, :return_to) do
72 masto_fe_path(conn, :index, ["getting-started"])
75 delete_session(conn, :return_to)
80 @spec get_or_make_app() :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
81 defp get_or_make_app do
82 %{client_name: @local_mastodon_name, redirect_uris: "."}
83 |> App.get_or_make(["read", "write", "follow", "push", "admin"])