Skip to main content

Authorization middleware for GraphQL.

Project description

graphql-authz

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

PyPi Build Status codecov

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 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.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

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

graphql-authz-0.1.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

graphql_authz-0.1.2-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file graphql-authz-0.1.2.tar.gz.

File metadata

  • Download URL: graphql-authz-0.1.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for graphql-authz-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8d30bd3ff8a356b316867b5d8861439c9f8feec192d085b17025f76d361a9e24
MD5 af188ee8d51da58f234cdfb6ac6210c9
BLAKE2b-256 9adfe370a5f5eb1a585804d2650b2387dcc6789f17b87861cd91792096748689

See more details on using hashes here.

File details

Details for the file graphql_authz-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: graphql_authz-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for graphql_authz-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cc448a68a9d803a2a9323da566df1a40ac650ec3c8131f01c340d6535567bb13
MD5 a36dfacaea56f57cc2ae4a255616b1e4
BLAKE2b-256 965452c696867e744f7ad8bdc8c0535c3267738da097bca61dffd2573e044186

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