Skip to main content

Framework-agnostic authorization gates inspired by Laravel

Project description

Scutum

Scutum is a lightweight authorization library inspired by Laravel's Policies and Gates. It provides an easy way to manage permissions using decorators, making access control more structured and maintainable.

Installation

pip install scutum

Usage

Defining Actions

An action is a set of rules that determine whether a user can manipulate a particular resource.

from scutum import gate

@gate.register("edit")
def edit_post(user, post):
    return user.id == post.author_id

Defining Policies

A policy is a named class whose methods are actions that will be registered along with the class name.

from scutum import Policy, gate

@gate.policy("user")
class UserPolicy(Policy):
    def create(self, user, *args, **kwargs):
        return user.is_admin

Checking Permissions

Through the gate it is possible to check the permissions of registered policies.

from scutum import Policy, gate

@gate.policy("user")
class UserPolicy(Policy):
    def create(self, user, *args, **kwargs):
        return user.is_admin

    def delete(self, user, *args, **kwargs):
        return user.is_admin

def create_user():
    user = current_user()

    if gate.allowed("user:create", user): # If action is allowed
        return "You are authorized to create a user"

    if gate.denied("user:create", user): # If action is denied
        return "You are not authorized to create a user"

    if gate.any(["user:create", "user:delete"], user): # If any action
        return "You have permission to create or delete a user."

    if gate.none(["user:create", "user:delete"], user): # If none action
        return "You do not have permission to create or delete a user."

    response = gate.check("user:create", user) # bool or Response
    
    return response

Responses

Responses are classes that can be returned within an action or policy for an allowed or denied action and can define details such as status code and response body.

from scutum import Response, gate

@guard.register("update")
def update_user(authenticated_user, user):
    if authenticated_user.is_admin:
        return Response.allow("User authorized")

    return Response.deny("This action is not authorized")

Extensions

You can explore example projects demonstrating how to integrate this library with popular Python web frameworks:

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scutum-0.2.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scutum-0.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file scutum-0.2.0.tar.gz.

File metadata

  • Download URL: scutum-0.2.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for scutum-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e387f1a000af1376565d05d9745467e1a6a44318ea857dedaa36be49dfd25738
MD5 21e12330586b035549e04e9c0833c829
BLAKE2b-256 4672207be3b8526cb1d08426e18e2a73195868134ef96437f0b894b42894d6ab

See more details on using hashes here.

File details

Details for the file scutum-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: scutum-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for scutum-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1d702d29967c8528f3234e4466e15e4c8f110030940648fc9d5fc6fe62e1b05
MD5 def0c8d7b83d45d9f70db039d64fbceb
BLAKE2b-256 e3ac2adc4754b0c52ff9a393510f3f516df9fb788c768a8e41ca2ab423aeea96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page