Merge pull request 'purge chat and shout endpoints' (#97) from purge-chat into develop
[akkoma] / lib / pleroma / web / mastodon_api / controllers / auth_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AuthController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
9
10 alias Pleroma.Helpers.AuthHelper
11 alias Pleroma.Helpers.UriHelper
12 alias Pleroma.User
13 alias Pleroma.Web.OAuth.App
14 alias Pleroma.Web.OAuth.Authorization
15 alias Pleroma.Web.OAuth.Token
16 alias Pleroma.Web.OAuth.Token.Strategy.Revoke, as: RevokeToken
17 alias Pleroma.Web.TwitterAPI.TwitterAPI
18
19 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
20
21 plug(Pleroma.Web.Plugs.RateLimiter, [name: :password_reset] when action == :password_reset)
22
23 @local_mastodon_name "Mastodon-Local"
24
25 @doc "GET /web/login"
26 # Local Mastodon FE login callback action
27 def login(conn, %{"code" => auth_token} = params) do
28 with {:ok, app} <- local_mastofe_app(),
29 {:ok, auth} <- Authorization.get_by_token(app, auth_token),
30 {:ok, oauth_token} <- Token.exchange_token(app, auth) do
31 redirect_to =
32 conn
33 |> local_mastodon_post_login_path()
34 |> UriHelper.modify_uri_params(%{"access_token" => oauth_token.token})
35
36 conn
37 |> AuthHelper.put_session_token(oauth_token.token)
38 |> redirect(to: redirect_to)
39 else
40 _ -> redirect_to_oauth_form(conn, params)
41 end
42 end
43
44 def login(conn, params) do
45 with %{assigns: %{user: %User{}, token: %Token{app_id: app_id}}} <- conn,
46 {:ok, %{id: ^app_id}} <- local_mastofe_app() do
47 redirect(conn, to: local_mastodon_post_login_path(conn))
48 else
49 _ -> redirect_to_oauth_form(conn, params)
50 end
51 end
52
53 defp redirect_to_oauth_form(conn, _params) do
54 with {:ok, app} <- local_mastofe_app() do
55 path =
56 Routes.o_auth_path(conn, :authorize,
57 response_type: "code",
58 client_id: app.client_id,
59 redirect_uri: ".",
60 scope: Enum.join(app.scopes, " ")
61 )
62
63 redirect(conn, to: path)
64 end
65 end
66
67 @doc "DELETE /auth/sign_out"
68 def logout(conn, _) do
69 conn =
70 with %{assigns: %{token: %Token{} = oauth_token}} <- conn,
71 session_token = AuthHelper.get_session_token(conn),
72 {:ok, %Token{token: ^session_token}} <- RevokeToken.revoke(oauth_token) do
73 AuthHelper.delete_session_token(conn)
74 else
75 _ -> conn
76 end
77
78 redirect(conn, to: "/")
79 end
80
81 @doc "POST /auth/password"
82 def password_reset(conn, params) do
83 nickname_or_email = params["email"] || params["nickname"]
84
85 TwitterAPI.password_reset(nickname_or_email)
86
87 json_response(conn, :no_content, "")
88 end
89
90 defp local_mastodon_post_login_path(conn) do
91 case get_session(conn, :return_to) do
92 nil ->
93 Routes.masto_fe_path(conn, :index, ["getting-started"])
94
95 return_to ->
96 delete_session(conn, :return_to)
97 return_to
98 end
99 end
100
101 @spec local_mastofe_app() :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
102 def local_mastofe_app do
103 App.get_or_make(
104 %{client_name: @local_mastodon_name, redirect_uris: "."},
105 ["read", "write", "follow", "push", "admin"]
106 )
107 end
108 end