diff options
author | Sadposter <hannah+pleroma@coffee-and-dreams.uk> | 2019-12-10 16:54:25 +0000 |
---|---|---|
committer | Sadposter <hannah+pleroma@coffee-and-dreams.uk> | 2019-12-10 16:54:25 +0000 |
commit | 8cfaab8f04cae6fcc20a37cdb463ee0c93b71217 (patch) | |
tree | 5ea3e43a73796f264508b583051f6f2143071f00 /lib/pleroma/web/oauth/scopes.ex | |
parent | 5abee19b63aa1577f8895b48ae69f0a51edb4dc3 (diff) | |
parent | aac0187ec13078d2756db1671e644d6eb9c0947b (diff) | |
download | pleroma-8cfaab8f04cae6fcc20a37cdb463ee0c93b71217.tar.gz |
Merge branch 'develop' into 'domain-block-precedence'
# Conflicts:
# lib/pleroma/user.ex
Diffstat (limited to 'lib/pleroma/web/oauth/scopes.ex')
-rw-r--r-- | lib/pleroma/web/oauth/scopes.ex | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/lib/pleroma/web/oauth/scopes.ex b/lib/pleroma/web/oauth/scopes.ex index 48bd14407..5e04652c2 100644 --- a/lib/pleroma/web/oauth/scopes.ex +++ b/lib/pleroma/web/oauth/scopes.ex @@ -7,6 +7,9 @@ defmodule Pleroma.Web.OAuth.Scopes do Functions for dealing with scopes. """ + alias Pleroma.Plugs.OAuthScopesPlug + alias Pleroma.User + @doc """ Fetch scopes from request params. @@ -53,15 +56,36 @@ defmodule Pleroma.Web.OAuth.Scopes do @doc """ Validates scopes. """ - @spec validate(list() | nil, list()) :: + @spec validate(list() | nil, list(), User.t()) :: {:ok, list()} | {:error, :missing_scopes | :unsupported_scopes} - def validate([], _app_scopes), do: {:error, :missing_scopes} - def validate(nil, _app_scopes), do: {:error, :missing_scopes} + def validate(blank_scopes, _app_scopes, _user) when blank_scopes in [nil, []], + do: {:error, :missing_scopes} - def validate(scopes, app_scopes) do - case Pleroma.Plugs.OAuthScopesPlug.filter_descendants(scopes, app_scopes) do + def validate(scopes, app_scopes, %User{} = user) do + with {:ok, _} <- ensure_scopes_support(scopes, app_scopes), + {:ok, scopes} <- authorize_admin_scopes(scopes, app_scopes, user) do + {:ok, scopes} + end + end + + defp ensure_scopes_support(scopes, app_scopes) do + case OAuthScopesPlug.filter_descendants(scopes, app_scopes) do ^scopes -> {:ok, scopes} _ -> {:error, :unsupported_scopes} end end + + defp authorize_admin_scopes(scopes, app_scopes, %User{} = user) do + if user.is_admin || !contains_admin_scopes?(scopes) || !contains_admin_scopes?(app_scopes) do + {:ok, scopes} + else + {:error, :unsupported_scopes} + end + end + + def contains_admin_scopes?(scopes) do + scopes + |> OAuthScopesPlug.filter_descendants(["admin"]) + |> Enum.any?() + end end |