2019-02-09 14:09:08 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Plugs.OAuthScopesPlug do
|
|
|
|
import Plug.Conn
|
2019-07-10 09:25:58 +00:00
|
|
|
import Pleroma.Web.Gettext
|
2019-02-09 14:09:08 +00:00
|
|
|
|
|
|
|
@behaviour Plug
|
|
|
|
|
2019-02-15 16:54:37 +00:00
|
|
|
def init(%{scopes: _} = options), do: options
|
2019-02-09 14:09:08 +00:00
|
|
|
|
2019-02-15 16:54:37 +00:00
|
|
|
def call(%Plug.Conn{assigns: assigns} = conn, %{scopes: scopes} = options) do
|
|
|
|
op = options[:op] || :|
|
2019-02-09 14:09:08 +00:00
|
|
|
token = assigns[:token]
|
2019-02-15 16:54:37 +00:00
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(token) ->
|
|
|
|
conn
|
|
|
|
|
|
|
|
op == :| && scopes -- token.scopes != scopes ->
|
|
|
|
conn
|
|
|
|
|
|
|
|
op == :& && scopes -- token.scopes == [] ->
|
|
|
|
conn
|
|
|
|
|
|
|
|
options[:fallback] == :proceed_unauthenticated ->
|
|
|
|
conn
|
|
|
|
|> assign(:user, nil)
|
|
|
|
|> assign(:token, nil)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
missing_scopes = scopes -- token.scopes
|
2019-07-10 09:25:58 +00:00
|
|
|
permissions = Enum.join(missing_scopes, " #{op} ")
|
|
|
|
|
|
|
|
error_message =
|
|
|
|
dgettext("errors", "Insufficient permissions: %{permissions}.", permissions: permissions)
|
2019-02-15 16:54:37 +00:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_content_type("application/json")
|
2019-07-10 09:25:58 +00:00
|
|
|
|> send_resp(:forbidden, Jason.encode!(%{error: error_message}))
|
2019-02-15 16:54:37 +00:00
|
|
|
|> halt()
|
2019-02-09 14:09:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|