2019-10-01 08:54:45 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-01 08:54:45 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.AuthController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2020-09-21 18:56:40 +00:00
|
|
|
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
|
|
|
|
2019-10-01 08:54:45 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.OAuth.App
|
|
|
|
alias Pleroma.Web.OAuth.Authorization
|
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2019-11-11 12:13:06 +00:00
|
|
|
plug(Pleroma.Plugs.RateLimiter, [name: :password_reset] when action == :password_reset)
|
2019-10-01 08:54:45 +00:00
|
|
|
|
2020-04-21 13:29:19 +00:00
|
|
|
@local_mastodon_name "Mastodon-Local"
|
|
|
|
|
2019-10-01 08:54:45 +00:00
|
|
|
@doc "GET /web/login"
|
|
|
|
def login(%{assigns: %{user: %User{}}} = conn, _params) do
|
|
|
|
redirect(conn, to: local_mastodon_root_path(conn))
|
|
|
|
end
|
|
|
|
|
2020-10-07 14:58:45 +00:00
|
|
|
# Local Mastodon FE login init action
|
2019-10-01 08:54:45 +00:00
|
|
|
def login(conn, %{"code" => auth_token}) do
|
|
|
|
with {:ok, app} <- get_or_make_app(),
|
|
|
|
{:ok, auth} <- Authorization.get_by_token(app, auth_token),
|
|
|
|
{:ok, token} <- Token.exchange_token(app, auth) do
|
|
|
|
conn
|
|
|
|
|> put_session(:oauth_token, token.token)
|
|
|
|
|> redirect(to: local_mastodon_root_path(conn))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 14:58:45 +00:00
|
|
|
# Local Mastodon FE callback action
|
2019-10-01 08:54:45 +00:00
|
|
|
def login(conn, _) do
|
|
|
|
with {:ok, app} <- get_or_make_app() do
|
|
|
|
path =
|
|
|
|
o_auth_path(conn, :authorize,
|
|
|
|
response_type: "code",
|
|
|
|
client_id: app.client_id,
|
|
|
|
redirect_uri: ".",
|
|
|
|
scope: Enum.join(app.scopes, " ")
|
|
|
|
)
|
|
|
|
|
|
|
|
redirect(conn, to: path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "DELETE /auth/sign_out"
|
|
|
|
def logout(conn, _) do
|
|
|
|
conn
|
|
|
|
|> clear_session
|
|
|
|
|> redirect(to: "/")
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /auth/password"
|
|
|
|
def password_reset(conn, params) do
|
|
|
|
nickname_or_email = params["email"] || params["nickname"]
|
|
|
|
|
2020-09-02 17:11:24 +00:00
|
|
|
TwitterAPI.password_reset(nickname_or_email)
|
|
|
|
|
2020-09-21 18:56:40 +00:00
|
|
|
json_response(conn, :no_content, "")
|
2019-10-01 08:54:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp local_mastodon_root_path(conn) do
|
|
|
|
case get_session(conn, :return_to) do
|
|
|
|
nil ->
|
2019-10-02 13:05:14 +00:00
|
|
|
masto_fe_path(conn, :index, ["getting-started"])
|
2019-10-01 08:54:45 +00:00
|
|
|
|
|
|
|
return_to ->
|
|
|
|
delete_session(conn, :return_to)
|
|
|
|
return_to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_or_make_app() :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
defp get_or_make_app do
|
|
|
|
%{client_name: @local_mastodon_name, redirect_uris: "."}
|
2020-02-29 00:23:36 +00:00
|
|
|
|> App.get_or_make(["read", "write", "follow", "push", "admin"])
|
2019-10-01 08:54:45 +00:00
|
|
|
end
|
|
|
|
end
|