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.Plugs.AdminSecretAuthenticationPlug do
8 alias Pleroma.Helpers.AuthHelper
10 alias Pleroma.Web.Plugs.RateLimiter
16 def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
26 defp authenticate(%{params: %{"admin_token" => admin_token}} = conn) do
27 if admin_token == secret_token() do
28 assign_admin_user(conn)
30 handle_bad_token(conn)
34 defp authenticate(conn) do
35 token = secret_token()
37 case get_req_header(conn, "x-admin-token") do
38 blank when blank in [[], [""]] -> conn
39 [^token] -> assign_admin_user(conn)
40 _ -> handle_bad_token(conn)
45 case Pleroma.Config.get(:admin_token) do
46 blank when blank in [nil, ""] -> nil
51 defp assign_admin_user(conn) do
53 |> assign(:user, %User{is_admin: true})
54 |> AuthHelper.skip_oauth()
57 defp handle_bad_token(conn) do
58 RateLimiter.call(conn, name: :authentication)