Skip to main content

Django support for 'visitors' - who are neither anonymous nor authenticated.

Project description

Django Visitors

Django app for managing temporary session-based users.

Background

This package has been extracted out of django-request-token as a specific use case - that of temporary site "visitors". It enables a type of ephemeral user who is neither anonymous nor authenticated, but somewhere in between - known for the duration of their session.

Motivation

We've been using django-request-token for a while, and have issued over 100,000 tokens. A recent analysis showed two main use cases - single-use "magic links" for logging people in, and a more involved case where we invite unregistered users on to the platform to perform some action - providing a reference perhaps, or collaborating on something with (registered) users. The former we have extracted out into django-magic-links - and this package addresses the latter.

What is a "visitor"?

In the standard Django model you have the concept of an AnonymousUser, and an authenticated User - someone who has logged in. We have a third, intermediate, type of user - which we have historically referred to as a "Temp User", which is someone we know of, but who has not yet registered.

The canonical example of this is leaving a reference: user A on the site invites user B to leave a reference for them. They (A) give us B's name and email, we invite them to click on a link and fill out a form. That's it. We store their name and email so that we can contact them, but it's ephemeral - we don't need it, and we don't use it. Storing this data in the User table made sense (as it has name and email fields), but it led to a lot of user_type=TEMP munging to determine who is a 'real' user on the site.

What we really want is to 'stash' this information somewhere outside of the auth system, and to enable these temp users to have restricted access to specific areas of the application, for a limited period, after which we can forget about them and clear out the data.

We call these users "visitors".

Use Case - request a reference

Fred is a registered user on the site, and would like a reference from Ginger, his dance partner.

  1. Fred fills out the reference request form:
   Name: Ginger
   Email: ginger@[...].com
   Message: ...
   Scope: REFERENCE_REQUEST [hidden field]
  1. We save this information, and generate a unique link which we send to Ginger, along with the message.

  2. Ginger clicks on the link, at which point we recognise that this is someone we know about - a "visitor" - but who is in all other respects an AnonymousUser.

  3. We stash the visitor info in the standard session object - so that even though Ginger is not authenticated, we know who she is, and more importantly we know why she's here (REFERENCE_REQUEST).

  4. Ginger submits the reference - which may be a multi-step process, involving GETs and POSTs, all of which are guarded by a decorator that restricts access to visitors with the appropriate Scope (just like django-request-token).

  5. At the final step we can (optionally) choose to clear the session info immediately, effectively removing all further access.

Implementation

This code has been extracted out of django-request-token and simplified. It stores the visitor data in the Visitor model, and on each successful first request (where the token is 'processed' and the session filled) we record a VisitorLog record. This includes HTTP request info (session_key, referer, client IP, user-agent). This information is for analytics only - for instance determining whether links are being shared.

Configuration

Django Settings

  1. Add visitors to INSTALLED_APPS
  2. Add visitors.middleware.VisitorRequestMiddleware to MIDDLEWARE
  3. Add visitors.middleware.VisitorSessionMiddleware to MIDDLEWARE

Environment Settings

  • VISITOR_SESSION_KEY: session key used to stash visitor info ("visitor:session")

  • VISITOR_QUERYSTRING_KEY: querystring param used on tokenised links ("vid")

Usage

Once you have the package configured, you should use the allow_visitor decorator to protect views that you want to restricted to visitors only. The decorator requires a scope kwarg, which must match the Visitor.scope value set when the Visitor object is created.

@allow_visitor(scope="foo")
def protected_view(request):
   pass

By default the decorator will allow visitors with the correct scope, deny anonymous users, and also allow authenticated users. If you want more control over the access, you can pass a callable as the bypass param:

# prevent authenticated users from bypassing the check
@allow_visitor(scope="foo", bypass=lambda u: False)
def deny_authenticated_users(request):
   pass

If you don't care about the scope (you should), you can use "*" to allow all visitors access:

@allow_visitor(scope="*")
def allow_all_scopes(request):
   pass

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

django-visitors-0.1.1.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

django_visitors-0.1.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file django-visitors-0.1.1.tar.gz.

File metadata

  • Download URL: django-visitors-0.1.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.6 Darwin/20.2.0

File hashes

Hashes for django-visitors-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f7bd128157982ad236eda7148ab2019c214cfd7f20a559a30c83d6a5998a844f
MD5 92c4e7f6c7d629ef58240a2b4d275037
BLAKE2b-256 0dafbae7ef408c1779dd60afc6b7e91ea25b4e667de83895921bd499f990d329

See more details on using hashes here.

File details

Details for the file django_visitors-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_visitors-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.6 Darwin/20.2.0

File hashes

Hashes for django_visitors-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e10bc7643cdeaa1cc62024e89fdf105d0fe63f095da72b623faa48617f50c837
MD5 acef3110b1a71ca17a6f5e6f2083d9af
BLAKE2b-256 80139574ea2077decdb4040b2aa84cf8273b5b5f4146e3558973c92c58b66537

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