Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(object-authorization): Fix object authorization for interfaces #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/middlewares/object_authorization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Rajska.ObjectAuthorization do

# When is a Scalar, Custom or Enum type, authorize.
defp authorize_object(%type{} = object, fields, resolution)
when type in [Scalar, Custom, Type.Enum, Type.Enum.Value, Type.Union] do
when type in [Scalar, Custom, Type.Enum, Type.Enum.Value, Type.Union, Type.Interface] do
put_result(true, fields, resolution, object)
end

Expand Down Expand Up @@ -118,6 +118,13 @@ defmodule Rajska.ObjectAuthorization do
authorize(schema_node, selections ++ tail, resolution)
end

defp find_associations(
[%{schema_node: %Type.Interface{} = schema_node, selections: selections} | tail],
resolution
) do
authorize(schema_node, selections ++ tail, resolution)
end

defp find_associations(
[%{schema_node: schema_node, selections: selections} | tail],
resolution
Expand Down
38 changes: 38 additions & 0 deletions test/middlewares/object_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ defmodule Rajska.ObjectAuthorizationTest do
{:ok, %{name: "bob"}}
end
end

field :interface_query, :interface do
middleware Rajska.QueryAuthorization, [permit: :all, scope: false]
resolve fn _, _ ->
{:ok, %{name: "bob"}}
end
end
end

object :wallet_balance do
meta :authorize, :admin
interfaces([:interface])

field :total, :integer
end
Expand All @@ -80,6 +88,7 @@ defmodule Rajska.ObjectAuthorizationTest do

object :user do
meta :authorize, :all
interfaces([:interface])

field :email, :string
field :name, :string
Expand All @@ -94,6 +103,13 @@ defmodule Rajska.ObjectAuthorizationTest do
%{total: _}, _ -> :wallet_balance
end
end

interface :interface do
resolve_type fn
%{name: _}, _ -> :user
%{total: _}, _ -> :wallet_balance
end
end
end

test "Public query with public object works for everyone" do
Expand Down Expand Up @@ -174,6 +190,13 @@ defmodule Rajska.ObjectAuthorizationTest do
refute Map.has_key?(result, :errors)
end

test "Works for interfaces" do
{:ok, result} = Absinthe.run(interface_query(), __MODULE__.Schema, context: %{current_user: %{role: :admin}})

assert %{data: %{"interfaceQuery" => %{"name" => "bob"}}} = result
refute Map.has_key?(result, :errors)
end

test "Works when using fragments and user has access" do
{:ok, result} = Absinthe.run(fragment_query_user(), __MODULE__.Schema, context: %{current_user: %{role: :user}})

Expand Down Expand Up @@ -266,6 +289,21 @@ defmodule Rajska.ObjectAuthorizationTest do
"""
end

defp interface_query do
"""
{
interfaceQuery {
... on User {
name
}
... on WalletBalance {
total
}
}
}
"""
end

defp fragment_query_user do
"""
fragment userFields on User {
Expand Down