Skip to main content

graphql-authz is an casbin authorization middleware for GraphQL

Project description

graphql-authz

Build Status Coverage Status Version PyPI - Wheel Pyversions Download Discord

GraphQL-Authz is a Python port of GraphQL-Authz, the Casbin authorization middleware implementation in Node.js.

This package should be used with GraphQL-core 3, providing the capability to limit access to each GraphQL resource with the authorization middleware.

Installation

Install the package using pip.

pip install casbin-graphql-authz

Get Started

Limit the access to each GraphQL resource with a policy. For example, given this policy for an RBAC model:

p, authorized_user, hello, query

Authorization can be enforced using:

import casbin
from authz.middleware import enforcer_middleware

from graphql import (
    graphql_sync,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLField,
    GraphQLString,
)


schema = GraphQLSchema(
    query=GraphQLObjectType(
        name="RootQueryType",
        fields={
            "hello": GraphQLField(
                GraphQLString,
                resolve=lambda obj, info: "world")
        }))

enforcer = casbin.Enforcer("model_file.conf", "policy_file.csv")
authorization_middleware = enforcer_middleware(enforcer)

query = """{ hello }"""

# Authorized user ("authorized_user") has access to data
response = graphql_sync(
    schema,
    query,
    middleware=[authorization_middleware],
    context_value={"role": "authorized_user"}
)
assert response.data == {"hello": "world"}

# Unauthorized users ("unauthorized_user") are rejected
response = graphql_sync(
    schema,
    query,
    middleware=[authorization_middleware],
    context_value={"role": "unauthorized_user"}
)
assert response.errors[0].message == "unauthorized_user can not query hello"

For more interesting scenarios see tests folder.

Credits

Implementation was heavily inspired by the Node.js middleware GraphQL-Authz.

Authorization enforcement is based on Casbin authorization library.

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

casbin_graphql_authz-1.5.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

casbin_graphql_authz-1.5.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file casbin_graphql_authz-1.5.0.tar.gz.

File metadata

  • Download URL: casbin_graphql_authz-1.5.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for casbin_graphql_authz-1.5.0.tar.gz
Algorithm Hash digest
SHA256 5669241e786b3d7492f062fa9df56c44deefb90e0aad762cf2b22ba4a1f309ef
MD5 89018fbd8aa99091cc2d3d76fe2bb8fd
BLAKE2b-256 3b00f72c64841dc6ed783e97da3072f9b0bffbb6ebbdd686d67cb20d8ca98766

See more details on using hashes here.

File details

Details for the file casbin_graphql_authz-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_graphql_authz-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9cf33134964c1854e3596736a9878aa1819775fc3158e67eeb0042daa32a4bed
MD5 2525bf3301c215f77615ea2b8b26c2fd
BLAKE2b-256 1279b5a5e5cee66d049c057618b7cf78c2d88b87f3b555bfda70454c394b22f7

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