Merge branch 'develop' into update-oauth-template
[akkoma] / lib / pleroma / plugs / ensure_public_or_authenticated_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug do
6 import Plug.Conn
7 alias Pleroma.Config
8 alias Pleroma.User
9
10 def init(options) do
11 options
12 end
13
14 def call(conn, _) do
15 public? = Config.get!([:instance, :public])
16
17 case {public?, conn} do
18 {true, _} ->
19 conn
20
21 {false, %{assigns: %{user: %User{}}}} ->
22 conn
23
24 {false, _} ->
25 conn
26 |> put_resp_content_type("application/json")
27 |> send_resp(403, Jason.encode!(%{error: "This resource requires authentication."}))
28 |> halt
29 end
30 end
31 end