2019-02-09 15:09:08 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-02-09 15:09:08 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 08:57:27 +02:00
|
|
|
defmodule Pleroma.Web.Plugs.OAuthScopesPlug do
|
2019-02-09 15:09:08 +01:00
|
|
|
import Plug.Conn
|
2019-07-10 11:25:58 +02:00
|
|
|
import Pleroma.Web.Gettext
|
2019-02-09 15:09:08 +01:00
|
|
|
|
2019-12-06 18:33:47 +01:00
|
|
|
alias Pleroma.Config
|
2020-12-06 11:59:10 +01:00
|
|
|
alias Pleroma.Helpers.AuthHelper
|
2019-09-15 17:22:08 +02:00
|
|
|
|
2020-04-15 20:19:16 +02:00
|
|
|
use Pleroma.Web, :plug
|
|
|
|
|
2019-02-15 17:54:37 +01:00
|
|
|
def init(%{scopes: _} = options), do: options
|
2019-02-09 15:09:08 +01:00
|
|
|
|
2020-04-21 15:29:19 +02:00
|
|
|
@impl true
|
2020-04-15 20:19:16 +02:00
|
|
|
def perform(%Plug.Conn{assigns: assigns} = conn, %{scopes: scopes} = options) do
|
2019-02-15 17:54:37 +01:00
|
|
|
op = options[:op] || :|
|
2019-02-09 15:09:08 +01:00
|
|
|
token = assigns[:token]
|
2019-12-06 18:33:47 +01:00
|
|
|
|
2019-12-11 09:42:02 +01:00
|
|
|
scopes = transform_scopes(scopes, options)
|
2019-12-15 20:32:42 +01:00
|
|
|
matched_scopes = (token && filter_descendants(scopes, token.scopes)) || []
|
2019-02-15 17:54:37 +01:00
|
|
|
|
|
|
|
cond do
|
2019-12-15 20:32:42 +01:00
|
|
|
token && op == :| && Enum.any?(matched_scopes) ->
|
2019-02-15 17:54:37 +01:00
|
|
|
conn
|
|
|
|
|
2019-12-15 20:32:42 +01:00
|
|
|
token && op == :& && matched_scopes == scopes ->
|
2019-02-15 17:54:37 +01:00
|
|
|
conn
|
|
|
|
|
|
|
|
options[:fallback] == :proceed_unauthenticated ->
|
2020-12-06 11:59:10 +01:00
|
|
|
AuthHelper.drop_auth_info(conn)
|
2019-02-15 17:54:37 +01:00
|
|
|
|
|
|
|
true ->
|
2019-09-08 14:00:03 +02:00
|
|
|
missing_scopes = scopes -- matched_scopes
|
2019-07-10 11:25:58 +02:00
|
|
|
permissions = Enum.join(missing_scopes, " #{op} ")
|
|
|
|
|
|
|
|
error_message =
|
|
|
|
dgettext("errors", "Insufficient permissions: %{permissions}.", permissions: permissions)
|
2019-02-15 17:54:37 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_resp_content_type("application/json")
|
2019-07-10 11:25:58 +02:00
|
|
|
|> send_resp(:forbidden, Jason.encode!(%{error: error_message}))
|
2019-02-15 17:54:37 +01:00
|
|
|
|> halt()
|
2019-02-09 15:09:08 +01:00
|
|
|
end
|
|
|
|
end
|
2019-09-08 14:00:03 +02:00
|
|
|
|
2020-09-19 18:16:55 +02:00
|
|
|
@doc "Keeps those of `scopes` which are descendants of `supported_scopes`"
|
2019-09-08 14:00:03 +02:00
|
|
|
def filter_descendants(scopes, supported_scopes) do
|
|
|
|
Enum.filter(
|
|
|
|
scopes,
|
|
|
|
fn scope ->
|
|
|
|
Enum.find(
|
|
|
|
supported_scopes,
|
|
|
|
&(scope == &1 || String.starts_with?(scope, &1 <> ":"))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-09-15 17:22:08 +02:00
|
|
|
|
2019-12-11 09:42:02 +01:00
|
|
|
@doc "Transforms scopes by applying supported options (e.g. :admin)"
|
|
|
|
def transform_scopes(scopes, options) do
|
|
|
|
if options[:admin] do
|
|
|
|
Config.oauth_admin_scopes(scopes)
|
|
|
|
else
|
|
|
|
scopes
|
|
|
|
end
|
|
|
|
end
|
2019-02-09 15:09:08 +01:00
|
|
|
end
|