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.3.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.3.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casbin_graphql_authz-1.3.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.3.0.tar.gz
Algorithm Hash digest
SHA256 29f7c2cdab6321c6e67ad4f1a6cec875f3ffd017e6dda7d8d57d8da915d7ee07
MD5 2f05f62895a503d163049f900f323fbb
BLAKE2b-256 bceb718ac6e8e5474afc0b2cc8c4033c950f19ebf24235c1f326b79cd79b7bb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casbin_graphql_authz-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3dd181a4059619cd14bd4200349d83e110c68606dc73568b49815002819b9453
MD5 0742e37dd3a427c4840bdfa80822f69a
BLAKE2b-256 9e112fc6609aeb084fe50fa9973876f9ab610cab782106f05cddd8aa5beec816

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