Coder Social home page Coder Social logo

django-forwarded's Introduction

Django-Forwarded

PyPi

Support HTTP "Forwarded" header in your Django applications.

This middleware for Django adds support for the Forwarded header which is standardized by Internet Engineering Task Force (IETF) in RFC 7239 and summarized in Mozilla Developer Network (MDN) article of the same name. This header is used by many reverse HTTP proxies to pass client identification details (such as IPv4 or IPv6) to the backend application.

Installation

Install the middleware with your favorite Python package manager

Pip:

$ pip install django-forwarded

Pipenv:

$ pipenv install django-forwarded

Usage

Activate

Add the middleware to Django's MIDDLEWARE setting.

MIDDLEWARE = (
    ... # some middlewares
    "django_forwarded.Forwarded",
    ... # more middlewares
)

Configure

You can configure Django-Forwarded by adding appropriate variable to your Django application configuration file.

With TRUSTED_PROXY_LIST

Just specify a list of trusted proxies with the TRUSTED_PROXY_LIST variable in the file. This is the recommended way to specify the proxies.

TRUSTED_PROXY_LIST = [
    '2001:db8::10',
    '10.2.3.100',
]

With TRUSTED_PROXY_DEPTH

Just specify the number of trusted proxies with the TRUSTED_PROXY_DEPTH variable. This is useful when you have no control over them and their IP(s) might change frequently over the lifetime of your application.

TRUSTED_PROXY_DEPTH = 2  # trusts maximum of 2 proxies

Note: It is not as secure as specifying a list.

Access client IP

The middleware identifies the correct client IP from the user supplied configuration and the header received from the proxies. This information is then placed in the request.META['REMOTE_ADDR'] field.

def some_function(request):
    print(request.META['REMOTE_ADDR'])

Testing

To execute the included test suit, run the following commands in a terminal:

$ cd <path to the django-forwarded>
$ pip install -r requirements.txt
$ DJANGO_SETTINGS_MODULE=tests.settings python3 -m unittest  # run the tests

django-forwarded's People

Contributors

feandil avatar liviuvalsan avatar shreyanshk avatar

Stargazers

 avatar

Watchers

 avatar  avatar

django-forwarded's Issues

It's not possible to install the package via PIP

$ pip install django-forwarded
Collecting django-forwarded
  Using cached Django-Forwarded-0.0.2.tar.gz (5.2 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [12 lines of output]
      fatal: not a git repository (or any parent up to mount point /)
      Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-o58uuvlh/django-forwarded_59b01ae41f874d20b7283d61105838ee/setup.py", line 7, in <module>
          version = subprocess.check_output(["git", "describe", "--tags"]).decode()[:-1]
        File "/usr/lib64/python3.9/subprocess.py", line 424, in check_output
          return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
        File "/usr/lib64/python3.9/subprocess.py", line 528, in run
          raise CalledProcessError(retcode, process.args,
      subprocess.CalledProcessError: Command '['git', 'describe', '--tags']' returned non-zero exit status 128.
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.