Python authorization libraries

Updated Nov 2026

Python authorization libraries decide whether an authenticated user may perform an action on a resource. A policy engine evaluates each request against roles, attributes, or relationships stored in a policy model. These libraries let apps and AI agents enforce permissions in one place instead of scattering checks through the code.

Popularity

OpenFGA has the most monthly downloads among dedicated authorization libraries in this comparison, at 3.2M.

LibraryMonthly downloadsStarsLast releaseLicense
Authlib
137.9M
5.5KNov 2026BSD-3-Clause
Django
51.9M
91.8KNov 2026BSD-3-Clause
Django REST framework
30.2M
30.2KOct 2026BSD-3-Clause
Django OAuth Toolkit
6.8M
3.3KAug 2026BSD-2-Clause-Views
OpenFGA
3.2M
88Nov 2026Apache-2.0
PyCasbin
2.4M
1.8KFeb 2026Apache-2.0
Authzed
1.3M
51Jul 2026Apache-2.0
django-guardian
1.1M
3.9KOct 2026BSD-2-Clause; CC-BY-4.0 (icons)
Downloads: PyPI, last thirty days. Stars and releases: GitHub. Amber: no release in over a year.

Libraries and code examples

Builds OAuth and OpenID Connect servers and clients.

Downloads / mo
137.9M
Stars
5.5K
Last release
1.9.9
License
BSD-3-Clause

Quickstart

From resource_protector.py
$ pip install Authlib
from authlib.oauth2.rfc6750 import BearerTokenValidator

validator = BearerTokenValidator()
print(validator.scope_insufficient("read:docs", ["read:docs"]))

Authlib examples

From open-source projects on GitHub

Provides a high-level Python web framework.

Downloads / mo
51.9M
Stars
91.8K
Last release
6.1.3
License
BSD-3-Clause

Quickstart

From models.py
$ python -m pip install Django
from django.conf import settings
settings.configure(
    SECRET_KEY="quickstart-example-only",
    INSTALLED_APPS=["django.contrib.auth", "django.contrib.contenttypes"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
import django
django.setup()
from django.core.management import call_command
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission

call_command("migrate", verbosity=0)
user = get_user_model().objects.create_user(username="library_demo")
permission = Permission.objects.get(content_type__app_label="auth", codename="view_user")
user.user_permissions.add(permission)
print(user.has_perm("auth.view_user"))

Django examples

From open-source projects on GitHub

Django REST framework

Builds web APIs for Django.

Downloads / mo
30.2M
Stars
30.2K
Last release
3.18.3
License
BSD-3-Clause

Quickstart

From permissions.py
$ pip install djangorestframework
from django.conf import settings
settings.configure(
    SECRET_KEY="quickstart-example-only",
    INSTALLED_APPS=["django.contrib.auth", "django.contrib.contenttypes", "rest_framework"],
)
import django
django.setup()

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from rest_framework.views import APIView

class AdminView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        return Response({"status": "ok"})

response = AdminView.as_view()(APIRequestFactory().get("/"))
print(response.status_code)

Django REST framework examples

From open-source projects on GitHub
def retrieve(self, request, *args, **kwargs):
    instance = self.get_object()
    serializer = self.get_serializer(instance)
    response = Response(serializer.data)
    if etag := self._get_etag(instance):
        response['ETag'] = etag
    return response

Django OAuth Toolkit

Runs an OAuth authorization server inside Django.

Downloads / mo
6.8M
Stars
3.3K
Last release
3.4.1
License
BSD-2-Clause-Views

Quickstart

From README.md
$ pip install django-oauth-toolkit
import os
import django
django.setup()

from django.http import HttpResponse
from django.test import RequestFactory
from oauth2_provider.views.generic import ProtectedResourceView

class ApiEndpoint(ProtectedResourceView):
    def get(self, request, *args, **kwargs):
        return HttpResponse("Hello, OAuth2!")

request = RequestFactory().get(
    "/api/hello", secure=True, HTTP_HOST="localhost",
    HTTP_AUTHORIZATION="Bearer " + os.environ["OAUTH_ACCESS_TOKEN"],
)
print(ApiEndpoint.as_view()(request).status_code)

Provides an authorization engine inspired by Google Zanzibar.

Downloads / mo
3.2M
Stars
88
Last release
0.10.6
License
Apache-2.0

Quickstart

From README.md
$ pip3 install openfga_sdk
import asyncio
import os
from openfga_sdk import ClientConfiguration, OpenFgaClient
from openfga_sdk.client import ClientCheckRequest

async def main():
    configuration = ClientConfiguration(
        api_url=os.environ["FGA_API_URL"],
        store_id=os.environ["FGA_STORE_ID"],
        authorization_model_id=os.environ["FGA_AUTHORIZATION_MODEL_ID"],
    )
    async with OpenFgaClient(configuration) as client:
        response = await client.check(ClientCheckRequest(
            user="user:anne", relation="viewer", object="document:roadmap",
        ))
        print(response.allowed)

asyncio.run(main())

Enforces authorization policies using configurable access-control models.

Downloads / mo
2.4M
Stars
1.8K
Last release
2.8.0
License
Apache-2.0

Strengths

  • Supports multiple access-control models. Choose role-based, attribute-based, or domain-scoped policy models.
  • Separates models from policy data. Define the authorization model separately from stored policy rules.

Limitations

  • Authentication stays with the application. Authenticate the caller before enforcing authorization policies.

Quickstart

From README.md
$ pip install pycasbin
import casbin
from casbin.model import Model

model = Model()
model.load_model_from_text("""[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act""")
enforcer = casbin.Enforcer(model)
enforcer.add_policy("alice", "data1", "read")
print(enforcer.enforce("alice", "data1", "read"))

PyCasbin examples

From open-source projects on GitHub

Provides the client library for SpiceDB.

Downloads / mo
1.3M
Stars
51
Last release
1.25.0
License
Apache-2.0

Quickstart

From README.md
$ pip install authzed
import os
from authzed.api.v1 import (
    Client, CheckPermissionRequest, CheckPermissionResponse, ObjectReference, SubjectReference,
)
from grpcutil import bearer_token_credentials

client = Client(
    os.environ["AUTHZED_ENDPOINT"],
    bearer_token_credentials(os.environ["AUTHZED_API_TOKEN"]),
)
response = client.CheckPermission(CheckPermissionRequest(
    resource=ObjectReference(object_type="blog/post", object_id="1"),
    permission="reader",
    subject=SubjectReference(object=ObjectReference(object_type="blog/user", object_id="emilia")),
))
print(response.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION)

django-guardian

Adds per-object permissions to Django’s authorization backend.

Downloads / mo
1.1M
Stars
3.9K
Last release
3.5.2
License
BSD-2-Clause; CC-BY-4.0 (icons)

Strengths

  • Stores permissions per object. Assign a user permission for one object without granting it globally.
  • Supports users and groups. Assign object permissions to individual users or Django groups.

Limitations

  • Generic relations need deletion cleanup. Delete object-permission rows when deleting objects linked through generic relations.

Quickstart

From README.md
$ pip install django-guardian
from django.conf import settings
settings.configure(
    SECRET_KEY="quickstart-example-only",
    INSTALLED_APPS=["django.contrib.auth", "django.contrib.contenttypes", "guardian"],
    AUTHENTICATION_BACKENDS=["django.contrib.auth.backends.ModelBackend", "guardian.backends.ObjectPermissionBackend"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
import django
django.setup()
from django.core.management import call_command
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from guardian.shortcuts import assign_perm

call_command("migrate", verbosity=0)
user = get_user_model().objects.create_user(username="library_demo")
group = Group.objects.create(name="editable_demo")
assign_perm("change_group", user, group)
print(user.has_perm("auth.change_group", group))

django-guardian examples

From open-source projects on GitHub

FAQ

PyCasbin suits apps that want role or attribute policies in a config file. OpenFGA and Authzed suit relationship-based permissions shared across services. django-guardian suits Django apps that need per-object permissions.

Role-based access control grants permissions through roles assigned to users. Attribute-based access control evaluates attributes of the user, the resource, and the request, such as department or time of day.

Authentication confirms who a user is. Authorization decides what that user may do after sign-in.

SpiceDB is an open-source permissions database inspired by Google Zanzibar. Authzed is its Python client library.

OpenFGA is an open-source authorization engine inspired by Google Zanzibar. Its Python SDK writes relationship tuples and checks permissions.

Relationship-based access control grants access from relationships between objects, such as a user who owns a folder. OpenFGA and SpiceDB both implement this model.

For coding agents

Found your authorization library? Help your agent use it.

Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.

Run in your terminal

claude plugin install exa@claude-plugins-official