1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.OAuthScopesPlug do
10 def init(%{scopes: _} = options), do: options
12 def call(%Plug.Conn{assigns: assigns} = conn, %{scopes: scopes} = options) do
13 op = options[:op] || :|
14 token = assigns[:token]
20 op == :| && scopes -- token.scopes != scopes ->
23 op == :& && scopes -- token.scopes == [] ->
26 options[:fallback] == :proceed_unauthenticated ->
29 |> assign(:token, nil)
32 missing_scopes = scopes -- token.scopes
33 error_message = "Insufficient permissions: #{Enum.join(missing_scopes, " #{op} ")}."
36 |> put_resp_content_type("application/json")
37 |> send_resp(403, Jason.encode!(%{error: error_message}))