Giter VIP home page Giter VIP logo

django-viewpass's Introduction

Viewpass

Overview

This is a system for allowing the creation and use of URLs that contain a token that allows a site visitor, even an anonymous visitor, access to pages that might not normally be allowed to visit.

For example, say you have a report page at /report/ that normally requires the permission 'reporting.view_report' to view. However, you'd like to be able to send emails to some friends to allow them to view this page without needing an account.

Viewpass is:

  • a utility to craft URLs with embedded, cryptographically-signed information about a permission
  • a Django middleware that intercepts URLs with these kind of URLs, and stores information about that permission onto the user object.

Installing

Add 'viewpass' to the INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'viewpass',
]

Add it to the middleware classes: This need to be after the Authentication Middleware (since we need a user!). A good arrangement could be:

MIDDLEWARE_CLASSES = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'viewpass.middleware.ViewPassMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Add viewpass to the AUTHENTICATION_BACKENDS:

AUTHENTICATION_BACKENDS = [
    'viewpass.backends.ViewPassBackend',
    'django.contrib.auth.backends.ModelBackend'
]

Generating Viewpass URLs

You can generate the proper URL for them by using the method util.get_viewpass_url, like:

get_viewpass_url('/report/', 'reporting.view_report')

This URL will look like /report/?viewpass=STRING1:STRING2 and visitors can use this URL to visit the /report/ page.

The STRING1 and STRING2 are encoded versions of the timestamp/path/permission-to-grant and a signed copy of the path/permission-to-grant, respectively. This prevents any tampering by users trying to re-use a viewpass URL to view another page or to change the permission granted. (The timestamp is not used but can be used to create time-limited URLs; you can edit the signing.loads() call in viewpass.middleware.ViewPassMiddleware to add a max_age=<num_seconds> parameter).

As a convenience, you can also use the template tag to generate the proper URL. On the template that is used by /report/, you can include:

{% load viewpass_tags %}
<a href="{% viewpass_url 'reporting.view_report' %}">Get public URL</a>

This will show a link with the viewpass URL on it.

Credit

Viewpass is written and maintained by Joel Burton <[email protected]>.

django-viewpass's People

Contributors

joelburton avatar

Watchers

 avatar  avatar

Forkers

reduxionist

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.