All Projects → venuu → Jsonapi Authorization

venuu / Jsonapi Authorization

Licence: mit
Authorization for JSONAPI::Resource

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Jsonapi Authorization

Xxl Sso
A distributed single-sign-on framework.(分布式单点登录框架XXL-SSO)
Stars: ✭ 1,635 (+1157.69%)
Mutual labels:  authorization
Yup Oauth2
An oauth2 client implementation providing the Device, Installed and Service Account flows.
Stars: ✭ 122 (-6.15%)
Mutual labels:  authorization
Ember Jsonapi Resources
Lightweight persistence for an Ember CLI app following the JSON API 1.0 spec
Stars: ✭ 127 (-2.31%)
Mutual labels:  json-api
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (-10.77%)
Mutual labels:  json-api
Jwt
Jwt.Net, a JWT (JSON Web Token) implementation for .NET
Stars: ✭ 1,694 (+1203.08%)
Mutual labels:  authorization
Fosite
Extensible security first OAuth 2.0 and OpenID Connect SDK for Go.
Stars: ✭ 1,738 (+1236.92%)
Mutual labels:  authorization
Cakephp Tinyauth
CakePHP TinyAuth plugin for an easy and fast user authentication and authorization. Single or multi role. DB or config file based.
Stars: ✭ 114 (-12.31%)
Mutual labels:  authorization
Authorization
A toolset for authorizing access to graph types for GraphQL .NET.
Stars: ✭ 127 (-2.31%)
Mutual labels:  authorization
Roles Permissions Laravel
Roles and Permissions implementation on Laravel 5.4
Stars: ✭ 121 (-6.92%)
Mutual labels:  authorization
Arduinosim800l
Arduino HTTP & FTP client for SIM800L/SIM800 boards to perform GET and POST requests to a JSON API as well as FTP uploads.
Stars: ✭ 127 (-2.31%)
Mutual labels:  json-api
Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+1225.38%)
Mutual labels:  authorization
Serverless Architectures Aws
The code repository for the Serverless Architectures on AWS book
Stars: ✭ 120 (-7.69%)
Mutual labels:  authorization
Contenta vue nuxt
Start in minutes a Drupal 8 with JSON API and Vue.js : a Nuxt.js ( Vue.js SSR ) consumer for Contenta CMS
Stars: ✭ 125 (-3.85%)
Mutual labels:  json-api
Coronavirus Tracker Api
🦠 A simple and fast (< 200ms) API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak. It's written in python using the 🔥 FastAPI framework. Supports multiple sources!
Stars: ✭ 1,577 (+1113.08%)
Mutual labels:  json-api
Caddy Auth Jwt
JWT Authorization Plugin for Caddy v2
Stars: ✭ 127 (-2.31%)
Mutual labels:  authorization
Rbac.dev
A collection of good practices and tools for Kubernetes RBAC
Stars: ✭ 115 (-11.54%)
Mutual labels:  authorization
Node Rate Limiter Flexible
Node.js rate limit requests by key with atomic increments in single process or distributed environment.
Stars: ✭ 1,950 (+1400%)
Mutual labels:  authorization
Katharsis Framework
Katharsis adds powerful layer for RESTful endpoints providing implementenation of JSON:API standard
Stars: ✭ 129 (-0.77%)
Mutual labels:  json-api
Laravel Auth
A powerful authentication, authorization and verification package built on top of Laravel. It provides developers with Role Based Access Control, Two-Factor Authentication, Social Authentication, and much more, compatible Laravel’s standard API and fully featured out of the box.
Stars: ✭ 128 (-1.54%)
Mutual labels:  authorization
Hydra
OpenID Certified™ OpenID Connect and OAuth Provider written in Go - cloud native, security-first, open source API security for your infrastructure. SDKs for any language. Compatible with MITREid.
Stars: ✭ 11,884 (+9041.54%)
Mutual labels:  authorization

JSONAPI::Authorization

Build Status Gem Version

NOTE: This README is the documentation for JSONAPI::Authorization. If you are viewing this at the project page on Github you are viewing the documentation for the master branch. This may contain information that is not relevant to the release you are using. Please see the README for the version you are using.


JSONAPI::Authorization adds authorization to the jsonapi-resources (JR) gem using Pundit.

The core design principle of JSONAPI::Authorization is:

Prefer being overly restrictive rather than too permissive by accident.

What follows is that we want to have:

  1. Whitelist over blacklist -approach for authorization
  2. Fall back on a more strict authorization

Caveats

Make sure to test for authorization in your application, too. We should have coverage of all operations, though. If that isn't the case, please open an issue.

If you're using custom processors, make sure that they extend JSONAPI::Authorization::AuthorizingProcessor, or authorization will not be performed for that resource.

This gem should work out-of-the box for simple cases. The default authorizer might be overly restrictive for cases where you are touching relationships.

If you are modifying relationships, you should read the relationship authorization documentation.

Installation

Add this line to your application's Gemfile:

gem 'jsonapi-authorization'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsonapi-authorization

Compatibility

We aim to support the same Ruby and Ruby on Rails versions as jsonapi-resources does. If that's not the case, please open an issue.

Versioning and changelog

jsonapi-authorization follows Semantic Versioning. We prefer to make more major version bumps when we do changes that are likely to be backwards incompatible. That holds true even when it's likely the changes would be backwards compatible for a majority of our users.

Given the nature of an authorization library, it is likely that most changes are major version bumps.

Whenever we do changes, we strive to write good changelogs in the GitHub releases page.

Usage

First make sure you have a Pundit policy specified for every backing model that your JR resources use.

Hook up this gem as the default processor for JR, and optionally allow rescuing from Pundit::NotAuthorizedError to output better errors for unauthorized requests:

# config/initializers/jsonapi-resources.rb
JSONAPI.configure do |config|
  config.default_processor_klass = JSONAPI::Authorization::AuthorizingProcessor
  config.exception_class_whitelist = [Pundit::NotAuthorizedError]
end

Make all your JR controllers specify the user in the context and rescue errors thrown by unauthorized requests:

class BaseResourceController < ActionController::Base
  include JSONAPI::ActsAsResourceController
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

  private

  def context
    {user: current_user}
  end

  def user_not_authorized
    head :forbidden
  end
end

Have your JR resources include the JSONAPI::Authorization::PunditScopedResource module.

class BaseResource < JSONAPI::Resource
  include JSONAPI::Authorization::PunditScopedResource
  abstract
end

Policies

To check whether an action is allowed JSONAPI::Authorization calls the respective actions of your pundit policies (index?, show?, create?, update?, destroy?).

For relationship operations by default update? is being called for all affected resources. For a finer grained control you can define methods to authorize relationship changes. For example:

class ArticlePolicy

  # (...)

  def add_to_comments?(new_comments)
    record.published && new_comments.all? { |comment| comment.author == user }
  end

  def replace_comments?(new_comments)
    allowed = record.comments.all? { |comment| new_comments.include?(comment) || add_to_comments?([comment])}
    allowed && new_comments.all? { |comment| record.comments.include?(comment) || remove_from_comments?(comment) }
  end

  def remove_from_comments?(comment)
    comment.author == user || user.admin?
  end
end

For thorough documentation about custom policy methods, check out the relationship authorization docs.

Configuration

You can use a custom authorizer class by specifying a configure block in an initializer file. If using a custom authorizer class, be sure to require them at the top of the initializer before usage.

JSONAPI::Authorization.configure do |config|
  config.authorizer = MyCustomAuthorizer
end

By default JSONAPI::Authorization uses the :user key from the JSONAPI context hash as the Pundit user. If you would like to use :current_user or some other key, it can be configured as well.

JSONAPI::Authorization.configure do |config|
  config.pundit_user = :current_user
  # or a block can be provided
  config.pundit_user = ->(context){ context[:current_user] }
end

Troubleshooting

"Unable to find policy" exception for a request

The exception might look like this for resource class ArticleResource that is backed by Article model:

unable to find policy `ArticlePolicy` for `Article'

This means that you don't have a policy class created for your model. Create one and the error should go away.

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Credits

Originally based on discussion and code samples by @barelyknown and others in cerebris/jsonapi-resources#16.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/venuu/jsonapi-authorization.

Contributors

Thanks goes to these wonderful people (emoji key):

Vesa Laakso
Vesa Laakso

💻 📖 🚇 ⚠️ 🐛 💬 👀
Emil Sågfors
Emil Sågfors

💻 📖 🚇 ⚠️ 🐛 💬 👀
Matthias Grundmann
Matthias Grundmann

💻 📖 ⚠️ 💬
Thibaud Guillaume-Gentil
Thibaud Guillaume-Gentil

💻
Daniel Schweighöfer
Daniel Schweighöfer

💻
Bruno Sofiato
Bruno Sofiato

💻
Adam Robertson
Adam Robertson

📖
Greg Fisher
Greg Fisher

💻 ⚠️
Sam
Sam

💻 ⚠️
Justas Palumickas
Justas Palumickas

🐛 💻 ⚠️
Nicholas Rutherford
Nicholas Rutherford

💻 ⚠️ 🚇
Matthijsy
Matthijsy

🐛 ⚠️ 💻
brianswko
brianswko

🐛 ⚠️ 💻

This project follows the all-contributors specification. Contributions of any kind welcome!

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].