Rajska v0.0.1 Rajska View Source

Rajska is an elixir authorization library for Absinthe.

It provides the following middlewares:

Installation

The package can be installed by adding rajska to your list of dependencies in mix.exs:

def deps do
  [
    {:rajska, "~> 0.0.1"},
  ]
end

Usage

Create your Authorization module, which will implement the Rajska.Authorization behaviour and contain the logic to validate user permissions and will be called by Rajska middlewares. Rajska provides some helper functions by default, such as Rajska.Authorization.is_role_authorized?/2, Rajska.Authorization.has_user_access?/3 and Rajska.Authorization.is_field_authorized?/3, but you can override them with your application needs.

defmodule Authorization do
  use Rajska,
    roles: [:user, :admin]
end

Note: if you pass a non Keyword list to roles, as above, Rajska will assume your roles are in ascending order and the last one is the super role. You can override this behavior by defining your own Rajska.Authorization.is_super_role?/1 function or passing a Keyword list in the format [user: 0, admin: 1].

Add your Authorization module to your Absinthe.Schema context/1 callback and the desired middlewares to the middleware/3 callback:

def context(ctx), do: Map.put(ctx, :authorization, Authorization)

def middleware(middleware, field, %Absinthe.Type.Object{identifier: identifier})
when identifier in [:query, :mutation, :subscription] do
  middleware
  |> Rajska.add_query_authorization(field, Authorization)
  |> Rajska.add_object_authorization()
end

def middleware(middleware, field, object) do
  Rajska.add_field_authorization(middleware, field, object)
end

You can also add all Rajska middlewares at once by calling Rajska.Schema.add_middlewares/4:

def context(ctx), do: Map.put(ctx, :authorization, Authorization)

def middleware(middleware, field, object) do
  Rajska.add_middlewares(middleware, field, object, Authorization)
end

Since Scope Authorization middleware must be used with Query Authorization, it is automatically called when adding the former.

Link to this section Summary

Link to this section Functions

Link to this function

add_field_authorization(middleware, field, object) View Source

See Rajska.Schema.add_field_authorization/3.

Link to this function

add_middlewares(middleware, field, object, authorization) View Source

See Rajska.Schema.add_middlewares/4.

Link to this function

add_object_authorization(middleware) View Source

See Rajska.Schema.add_object_authorization/1.

Link to this function

add_query_authorization(middleware, field, authorization) View Source

See Rajska.Schema.add_query_authorization/3.