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

Support absinthe 1.5 #30

Merged
merged 10 commits into from
Nov 30, 2020
3 changes: 2 additions & 1 deletion lib/middlewares/field_authorization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ defmodule Rajska.FieldAuthorization do
}

def call(resolution, [object: %Type.Object{fields: fields} = object, field: field]) do
field_private? = fields[field] |> Type.meta(:private) |> field_private?(resolution.source)
{private_config, _binding} = fields[field] |> Type.meta(:private) |> Code.eval_quoted()
field_private? = field_private?(private_config, resolution.source)
scope? = get_scope!(object)

default_rule = Rajska.apply_auth_mod(resolution.context, :default_rule)
Expand Down
26 changes: 15 additions & 11 deletions lib/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ defmodule Rajska.Schema do
Field.t(),
module()
) :: [Middleware.spec(), ...]
def add_query_authorization(
[{{QueryAuthorization, :call}, config} = query_authorization | middleware] = _middleware,
%Field{name: query_name},
authorization
) do
validate_query_auth_config!(config, authorization, query_name)

[query_authorization | middleware]
end
def add_query_authorization(middleware, %Field{name: query_name}, authorization) do
middleware
|> Enum.find(&find_middleware/1)
|> case do
{{QueryAuthorization, :call}, config} ->
validate_query_auth_config!(config, authorization, query_name)

{{Absinthe.Resolution, :call}, _config} ->
raise "No permission specified for query #{query_name}"
end

def add_query_authorization(_middleware, %Field{name: name}, _authorization) do
raise "No permission specified for query #{name}"
middleware
end

def find_middleware({{QueryAuthorization, :call}, _config}), do: true
gabrielpra1 marked this conversation as resolved.
Show resolved Hide resolved
def find_middleware({{Absinthe.Resolution, :call}, _config}), do: true
def find_middleware({_middleware, _config}), do: false

@spec add_object_authorization([Middleware.spec(), ...]) :: [Middleware.spec(), ...]
def add_object_authorization([{{QueryAuthorization, :call}, _} = query_authorization | middleware]) do
[query_authorization, ObjectAuthorization] ++ middleware
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Rajska.MixProject do
[
{:ex_doc, "~> 0.19", only: :dev, runtime: false},
{:credo, "~> 1.1.0", only: [:dev, :test], runtime: false},
{:absinthe, "~> 1.4.0"},
{:absinthe, github: "absinthe-graphql/absinthe", branch: "master"},
{:excoveralls, "~> 0.11", only: :test},
{:hammer, "~> 6.0", optional: true},
{:mock, "~> 0.3.0", only: :test},
Expand Down
4 changes: 3 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"absinthe": {:hex, :absinthe, "1.4.16", "0933e4d9f12652b12115d5709c0293a1bf78a22578032e9ad0dad4efee6b9eb1", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "076b8bd9552f4966ba1242f412f6c439b731169a36a0ddaaffcd3893828f5bf6"},
"absinthe": {:git, "https://github.com/absinthe-graphql/absinthe.git", "a26b37ec28438c55e89ed558f363e827c24ee631", [branch: "master"]},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"credo": {:hex, :credo, "1.1.5", "caec7a3cadd2e58609d7ee25b3931b129e739e070539ad1a0cd7efeeb47014f4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d0bbd3222607ccaaac5c0340f7f525c627ae4d7aee6c8c8c108922620c5b6446"},
Expand All @@ -10,6 +10,7 @@
"hammer": {:hex, :hammer, "6.0.0", "72ec6fff10e9d63856968988a22ee04c4d6d5248071ddccfbda50aa6c455c1d7", [:mix], [{:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "d8e1ec2e534c4aae508b906759e077c3c1eb3e2b9425235d4b7bbab0b016210a"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
"kaffy": {:git, "https://github.com/absinthe-graphql/absinthe.git", "a26b37ec28438c55e89ed558f363e827c24ee631", [branch: "master"]},
gabrielpra1 marked this conversation as resolved.
Show resolved Hide resolved
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"meck": {:hex, :meck, "0.8.13", "ffedb39f99b0b99703b8601c6f17c7f76313ee12de6b646e671e3188401f7866", [:rebar3], [], "hexpm", "d34f013c156db51ad57cc556891b9720e6a1c1df5fe2e15af999c84d6cebeb1a"},
Expand All @@ -20,5 +21,6 @@
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm", "dad79704ce5440f3d5a3681c8590b9dc25d1a561e8f5a9c995281012860901e3"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}
6 changes: 3 additions & 3 deletions test/middlewares/object_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ defmodule Rajska.ObjectAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(all_query_with_user_object(), __MODULE__.Schema, context: %{current_user: nil})
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object company",
path: ["allQuery"]
}
Expand All @@ -146,7 +146,7 @@ defmodule Rajska.ObjectAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(user_query_with_admin_object(), __MODULE__.Schema, context: %{current_user: %{role: :user}})
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object wallet_balance",
path: ["userQuery"]
}
Expand Down Expand Up @@ -185,7 +185,7 @@ defmodule Rajska.ObjectAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(fragment_query_admin(), __MODULE__.Schema, context: %{current_user: %{role: :user}})
assert [
%{
locations: [%{column: 0, line: 13}],
locations: [%{column: 3, line: 13}],
message: "Not authorized to access object wallet_balance",
path: ["userQuery"]
}
Expand Down
20 changes: 10 additions & 10 deletions test/middlewares/object_scope_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query(1), context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -220,7 +220,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_with_company(1), context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -239,7 +239,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(query, context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "Not authorized to access object object_scope_user",
}
] == errors
Expand All @@ -249,7 +249,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_company_wallet(2), context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 8}],
locations: [%{column: 7, line: 8}],
message: "Not authorized to access object wallet",
}
] == errors
Expand All @@ -261,7 +261,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_company_wallet(2), context(:user, 1))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -279,7 +279,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_no_company(2), context(:user, 1))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -299,7 +299,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_companies_list(2), context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 8}],
locations: [%{column: 7, line: 8}],
message: "Not authorized to access object wallet",
}
] == errors
Expand All @@ -311,7 +311,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(all_query_companies_list(2), context(:user, 1))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -321,7 +321,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(users_query(), context(:user, 2))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user",
}
] == errors
Expand All @@ -335,7 +335,7 @@ defmodule Rajska.ObjectScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = run_pipeline(user_query_with_rule(), context(:admin, 1))
assert [
%{
locations: [%{column: 0, line: 2}],
locations: [%{column: 3, line: 2}],
message: "Not authorized to access object user_rule",
}
] == errors
Expand Down
6 changes: 3 additions & 3 deletions test/middlewares/query_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Rajska.QueryAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(admin_query(), __MODULE__.Schema, context: %{current_user: nil})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "unauthorized",
path: ["adminQuery"]
}
Expand All @@ -62,7 +62,7 @@ defmodule Rajska.QueryAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(admin_query(), __MODULE__.Schema, context: %{current_user: %{role: :user}})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "unauthorized",
path: ["adminQuery"]
}
Expand All @@ -80,7 +80,7 @@ defmodule Rajska.QueryAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(user_query(), __MODULE__.Schema, context: %{current_user: nil})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "unauthorized",
path: ["userQuery"]
}
Expand Down
6 changes: 3 additions & 3 deletions test/middlewares/query_scope_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ defmodule Rajska.QueryScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(user_scoped_query, __MODULE__.Schema, context: %{current_user: user})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "Not authorized to access this user",
path: ["userScopedQuery"]
}
Expand Down Expand Up @@ -243,7 +243,7 @@ defmodule Rajska.QueryScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(custom_arg_scoped_query, __MODULE__.Schema, context: %{current_user: user})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "Not authorized to access this user",
path: ["customArgScopedQuery"]
}
Expand All @@ -257,7 +257,7 @@ defmodule Rajska.QueryScopeAuthorizationTest do
assert {:ok, %{errors: errors}} = Absinthe.run(custom_nested_arg_scoped_query, __MODULE__.Schema, context: %{current_user: user})
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "Not authorized to access this user",
path: ["customNestedArgScopedQuery"]
}
Expand Down
2 changes: 1 addition & 1 deletion test/middlewares/rate_limiter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule Rajska.RateLimiterTest do
assert {:ok, %{errors: errors}} = Absinthe.run(query(:error_msg), __MODULE__.Schema, @default_context)
assert [
%{
locations: [%{column: 0, line: 1}],
locations: [%{column: 3, line: 1}],
message: "Rate limit exceeded",
path: ["error_msg"]
}
Expand Down