Allow moderators to get the admin scope again
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Wed, 8 Mar 2023 17:39:35 +0000 (17:39 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Wed, 8 Mar 2023 17:39:35 +0000 (17:39 +0000)
Fixes #463

lib/pleroma/web/o_auth/scopes.ex
test/pleroma/web/o_auth/o_auth_controller_test.exs

index 344ecd631d9f5ed075079f8e0d06ff24e87e4e8b..a170eb33b4d7f787df1a26e937e69574a4304797 100644 (file)
@@ -71,6 +71,8 @@ defmodule Pleroma.Web.OAuth.Scopes do
   """
   def filter_admin_scopes(scopes, %Pleroma.User{is_admin: true}), do: scopes
 
+  def filter_admin_scopes(scopes, %Pleroma.User{is_moderator: 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)
index 303bc2cf2efda5dbf4db7ec3e724bf176442261f..9924023fe8b9112470aadec83a4efc48f16285c1 100644 (file)
@@ -728,6 +728,42 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
       assert auth.scopes == scopes_subset
     end
 
+    test "redirects with oauth authorization, " <>
+         "granting requested app-supported scopes to moderators" do
+      app_scopes = ["read", "write", "admin", "secret_scope"]
+      app = insert(:oauth_app, scopes: app_scopes)
+      redirect_uri = OAuthController.default_redirect_uri(app)
+      scopes_subset = ["read:subscope", "write", "admin"]
+      admin = insert(:user, is_moderator: true)
+
+      # In case scope param is missing, expecting _all_ app-supported scopes to be granted
+      conn =
+        post(
+          build_conn(),
+          "/oauth/authorize",
+          %{
+            "authorization" => %{
+              "name" => admin.nickname,
+              "password" => "test",
+              "client_id" => app.client_id,
+              "redirect_uri" => redirect_uri,
+              "scope" => scopes_subset,
+              "state" => "statepassed"
+            }
+          }
+        )
+
+      target = redirected_to(conn)
+      assert target =~ redirect_uri
+
+      query = URI.parse(target).query |> URI.query_decoder() |> Map.new()
+
+      assert %{"state" => "statepassed", "code" => code} = query
+      auth = Repo.get_by(Authorization, token: code)
+      assert auth
+      assert auth.scopes == scopes_subset
+    end
+
     test "redirects with oauth authorization, " <>
            "granting requested app-supported scopes for non-admin users" do
       app_scopes = ["read", "write", "secret_scope", "admin"]