Skip to main content

Inigo Middleware

Project description



Inigo integration for Django and Graphene

Quickstart

Install inigo middleware

pip install inigo-py

Django Settings

MIDDLEWARE = [
    ...
    'inigo_py.DjangoMiddleware',
]

INIGO = {
    'DEBUG': False,
    'TOKEN': 'Your Inigo service token',
    'PATH': '/graphql',
    'JWT': 'authorization',
    'GRAPHENE_SCHEMA': 'app.schema.schema'
    # 'SCHEMA_PATH': './schema.graphql',
}

Configuration options

DEBUG

Optional. Default: False

If not provided, Django DEBUG setting is used.

TOKEN

Required. Obtain a service token from app.inigo.io

GRAPHENE_SCHEMA

Optional. The path to graphene schema instance. If not provided, InigoMiddleware will try to pick it up from GRAPHENE.SCHEMA settings.

SCHEMA_PATH

Optional. The path to graphql schema file.

PATH

Optional. Default: /graphql.

Your graphql route path.

JWT

Optional. Default: authorization.

Name of your authorization header with jwt as a value. See Authorization for more details.


Authentication

Passing Authentication using JWT header

  1. Configure and apply your service.yml
kind: Service
name: <service_name>
spec:
  path_user_id: jwt.user_name
  path_user_profile: jwt.user_profile
  path_user_role: jwt.user_roles
  1. Provide name of the header into Djang configuration
INIGO = {
    'JWT': 'authorization'
}

NOTE. Payload of the decoded jwt should match with the provided above configuration.

Passing Authentication using Context

  1. Configure and apply your service.yml
kind: Service
name: <service_name>
spec:
  path_user_id: ctx.user_name
  path_user_profile: ctx.user_profile
  path_user_role: ctx.user_roles
  1. Configure Django to pass in an InigoContext object.
from inigo_py import InigoContext

# define middleware to pass authentication via ctx
def auth(get_response):
  def middleware(request):

      request.inigo = InigoContext()
      request.inigo.auth = {
          'user_name': 'me',
          'user_profile': 'guest',
          'user_roles': [],
      }

      return get_response(request)

  return middleware

# add middleware to Django settings. Make sure to add it before `inigo_py.DjangoMiddleware` as it's providing info required for correct request processing.
MIDDLEWARE = [
  ...
  'middleware.auth',
  'inigo_py.DjangoMiddleware',
]

Note: if auth object provided on request, JWT header is not used

Logging blocked requests

# define middleware to check the request status after the request was processed by Inigo.
def log_inigo_blocked_requests(get_response):
    def middleware(request):

        resp = get_response(request)

        # NOTE. accessing value from 'request' object
        print(f'request is blocked: { request.inigo.blocked }')

        return resp

    return middleware


# add middleware to Django settings. Make sure to add it before `inigo_py.DjangoMiddleware` as status is available after `get_response` call.
MIDDLEWARE = [
    ...
    'middleware.log_inigo_blocked_requests',
    'inigo_py.DjangoMiddleware',
]

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

License

Distributed under the MIT License.

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

inigo_py-0.21.1.tar.gz (71.1 MB view hashes)

Uploaded Source

Built Distribution

inigo_py-0.21.1-py3-none-any.whl (71.4 MB view hashes)

Uploaded Python 3

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