All Projects → beam-community → sheriff

beam-community / sheriff

Licence: MIT License
Build simple and robust authorization systems with just Elixir and Plug

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to sheriff

lua-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Lua (OpenResty)
Stars: ✭ 43 (+10.26%)
Mutual labels:  authorization, access-control
Casbin4D
An authorization library that supports access control models like ACL, RBAC, ABAC in Delphi
Stars: ✭ 25 (-35.9%)
Mutual labels:  authorization, access-control
dart-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Dart/Flutter
Stars: ✭ 30 (-23.08%)
Mutual labels:  authorization, access-control
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (+194.87%)
Mutual labels:  authorization, access-control
casbin-ex
An authorization library that supports access control models like ACL, RBAC, ABAC in Elixir
Stars: ✭ 37 (-5.13%)
Mutual labels:  authorization, access-control
rbac-tool
Rapid7 | insightCloudSec | Kubernetes RBAC Power Toys - Visualize, Analyze, Generate & Query
Stars: ✭ 546 (+1300%)
Mutual labels:  authorization, access-control
objection-authorize
isomorphic, "magical" authorization integration with Objection.js 🎉
Stars: ✭ 71 (+82.05%)
Mutual labels:  authorization, access-control
Canary
🐣 Elixir authorization and resource-loading library for Plug applications.
Stars: ✭ 450 (+1053.85%)
Mutual labels:  plug, authorization
dictator
Dictates what your users see. Plug-based authorization.
Stars: ✭ 77 (+97.44%)
Mutual labels:  plug, authorization
lastkeypair
A serverless SSH certificate authority to control access to machines using IAM and Lambda
Stars: ✭ 39 (+0%)
Mutual labels:  authorization, access-control
ficam-playbooks
The Federal Identity Credentials and Access Management program publishes guides and playbooks to help U.S. federal executive agencies implement, maintain, and modernize identity management systems.
Stars: ✭ 30 (-23.08%)
Mutual labels:  authorization, access-control
access-controller
A highly scalable open-source implementation of an access-control engine inspired by Google Zanzibar-"Google’s Consistent, Global Authorization System"
Stars: ✭ 61 (+56.41%)
Mutual labels:  authorization, access-control
role-based-access-control
Role-based authorization || Role-based access-control in React.js
Stars: ✭ 111 (+184.62%)
Mutual labels:  authorization, access-control
caddy-security
🔐 Authentication, Authorization, and Accounting (AAA) App and Plugin for Caddy v2. 💎 Implements Form-Based, Basic, Local, LDAP, OpenID Connect, OAuth 2.0 (Github, Google, Facebook, Okta, etc.), SAML Authentication. MFA/2FA with App Authenticators and Yubico. 💎 Authorization with JWT/PASETO tokens. 🔐
Stars: ✭ 696 (+1684.62%)
Mutual labels:  authorization, access-control
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (+87.18%)
Mutual labels:  plug, authorization
SpringSecurityInEasySteps
Learn Spring Security step by step
Stars: ✭ 13 (-66.67%)
Mutual labels:  authorization, access-control
Caddy Authz
Caddy-authz is a middleware for Caddy that blocks or allows requests based on access control policies.
Stars: ✭ 221 (+466.67%)
Mutual labels:  authorization, access-control
Chi Authz
chi-authz is an authorization middleware for Chi
Stars: ✭ 248 (+535.9%)
Mutual labels:  authorization, access-control
server
AuthzForce Server (Community Edition)
Stars: ✭ 48 (+23.08%)
Mutual labels:  authorization, access-control
speedle-plus
Speedle+ is an open source project for access management. It is based on Speedle open source project and maintained by previous Speedle maintainers.
Stars: ✭ 45 (+15.38%)
Mutual labels:  authorization, access-control

Build Status

Sheriff is a simple minimal-dependency way to manage policy based authorization for Plug based applications, including Phoenix.

If you're looking for authentication check out Guardian or Ueberauth for third-party oauth.

Installation

The latest on hex.pm:

def deps do
  [{:sheriff, "~> 1.0"}]
end

If you prefer living on the edge use master:

def deps do
  [{:sheriff, github: "beam-community/sheriff"}]
end

Current User

By default Sheriff will look for the current user in the :current_user key within Plug.Conn.private. Since this may not be compatible with all applications, we've provided a way to reconfigure the key:

config :sheriff,
  resource_key: :your_desired_resource_key

Resource Loading

Resource loaders are responsible for retrieving the requested resources. A global loader can be specified in your application configuration or individual loaders can be supplied on a per plug basis.

Sheriff ships with a convenient Sheriff.ResourceLoader behaviour:

defmodule Example.UserLoader do
  @behaviour Sheriff.ResourceLoader

  def fetch_resource(:show, %{"id" => id}), do: Repo.get(User, id)
  def fetch_resource(:index, _params), do: Repo.all(User)
end

Laws

In Sheriff authorization is handled with laws; which are modules that implement the Sheriff.Law behaviour:

defmodule Example.UserLaw do
  @behaviour Sheriff.Law

  alias Example.User

  # Admins can see all the things!
  def legal?(%User{role: "admin"}, _request, _resource), do: true

  # Users can access themselves
  def legal?(%User{id: id}, _request, %User{id: id}), do: true

  # Team admin can view team members
  def legal?(%User{role: "team_admin", team_id: id}, :show, resources) do
    Enum.all?(resources, &(&1.team_id == team_id))
  end

  # No match, no access
  def legal?(_, _, _), do: false
end

Plugs

There are two plugs that serve as the workhorses of Sheriff, these need to occur after Plug.Parser:

  • Sheriff.LoadResource - Uses the configured ResourceLoader to fetch the target resource
  • Sheriff.LawEnforcer - Apply a given Law against the current user, target resource, and request.

When defining our authorization pipeline we could use something like this:

plug Sheriff.LoadResource, loader: Example.UserLoader
plug Sheriff.LawEnforcer, law: Example.UserLaw

Note: The Sheriff.LoadResource and the Sheriff.ResourceLoader behaviour are for simple use-cases. For more involved resource fetching you may find it's necessary to implement your own plug, make sure the loaded resources are assigned to the connection under the :sheriff_resource key.

Error Handling

Sheriff has just three error scenarios we need to address:

  • The requested resource is missing
  • The current user is not authenticated
  • The current user is not authorized to perform the requested action

To handle these cases we'll want to provide an error handler for Sheriff. Our handler can be a module that implements resource_missing/1, unauthenticated/1, and unauthorized/1; the Sheriff.Handler behaviour is optional.

Sheriff makes no assumptions so we need to tell it which module to use as a handler:

config :sheriff,
  handler: Example.ErrorHandler

That's it!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].