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.2.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

casbin_graphql_authz-1.2.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casbin_graphql_authz-1.2.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for casbin_graphql_authz-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ea5fa0019852c24d499e00a91d9f60dade21b4006c7493c5223672f6f43ac8bc
MD5 e6f2c4f03a640d9f783cd50f217f0fb3
BLAKE2b-256 a973368737882b7a771884d85bc30a0eb0202871eb31069960a885313751b50b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casbin_graphql_authz-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 26cfea8757bdd063e3cca1557f995116bb8f079038b8a0ff49b4134b8d985172
MD5 33f43f9530a3aa099a2493c47f5ac750
BLAKE2b-256 06de0ddf7e0c5ac478eb5c2673a70b0e91044fd7f79e7c4448cc6a666f6e8da0

See more details on using hashes here.

Supported by

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