X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fweb%2Fo_auth%2Fscopes.ex;h=ccd8d4665d8a4d0bd5f2f1fa9d58dc66fe05e1a8;hb=52d8183787db739fc10dd3e95141e274d4fb2bcc;hp=90b9a04715050dabc0266b97beabff80fa19e3e9;hpb=78cea44553c936813ffc2c4c47984a64255096ef;p=akkoma
diff --git a/lib/pleroma/web/o_auth/scopes.ex b/lib/pleroma/web/o_auth/scopes.ex
index 90b9a0471..ccd8d4665 100644
--- a/lib/pleroma/web/o_auth/scopes.ex
+++ b/lib/pleroma/web/o_auth/scopes.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth.Scopes do
@@ -56,12 +56,31 @@ defmodule Pleroma.Web.OAuth.Scopes do
@doc """
Validates scopes.
"""
- @spec validate(list() | nil, list()) ::
- {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes}
- def validate(blank_scopes, _app_scopes) when blank_scopes in [nil, []],
+ @spec validate(list() | nil, list(), Pleroma.User.t()) ::
+ {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes, :user_is_not_an_admin}
+ def validate(blank_scopes, _app_scopes, _user) when blank_scopes in [nil, []],
do: {:error, :missing_scopes}
- def validate(scopes, app_scopes) do
+ def validate(scopes, app_scopes, %Pleroma.User{is_admin: is_admin}) do
+ if !is_admin && contains_admin_scopes?(scopes) do
+ {:error, :user_is_not_an_admin}
+ else
+ validate_scopes_are_supported(scopes, app_scopes)
+ end
+ end
+
+ @spec filter_admin_scopes([String.t()], Pleroma.User.t()) :: [String.t()]
+ @doc """
+ Remove admin scopes for non-admins
+ """
+ def filter_admin_scopes(scopes, %Pleroma.User{is_admin: true}), do: scopes
+
+ def filter_admin_scopes(scopes, _user) do
+ drop_scopes = OAuthScopesPlug.filter_descendants(scopes, ["admin"])
+ Enum.reject(scopes, fn scope -> Enum.member?(drop_scopes, scope) end)
+ end
+
+ defp validate_scopes_are_supported(scopes, app_scopes) do
case OAuthScopesPlug.filter_descendants(scopes, app_scopes) do
^scopes -> {:ok, scopes}
_ -> {:error, :unsupported_scopes}