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
7 import Pleroma.Web.Gettext
11 def init(%{scopes: _} = options), do: options
13 def call(%Plug.Conn{assigns: assigns} = conn, %{scopes: scopes} = options) do
14 op = options[:op] || :|
15 token = assigns[:token]
21 op == :| && scopes -- token.scopes != scopes ->
24 op == :& && scopes -- token.scopes == [] ->
27 options[:fallback] == :proceed_unauthenticated ->
30 |> assign(:token, nil)
33 missing_scopes = scopes -- token.scopes
34 permissions = Enum.join(missing_scopes, " #{op} ")
37 dgettext("errors", "Insufficient permissions: %{permissions}.", permissions: permissions)
40 |> put_resp_content_type("application/json")
41 |> send_resp(:forbidden, Jason.encode!(%{error: error_message}))