Django app that uses JWT to manage one-time and expiring links to protected URLs.
Project description
Django app that uses JWT to manage one-time and expiring links to protected URLs.
Use Case
The primary use case for this app is authenticating users on a per- request basis, without having to log in to the site first.
The canonical example of this would be unsubscribing from mailing lists - you want to be able to identify the user, and process the request, but requiring the user to log in first presents a barrier.
Using JWT we can ‘pre-authenticate’ access to the URL.
Assumptions
This is not a general purpose link-generating app - it has some implicit assumptions built-in:
The link is being sent to a single user
The recipient is a known Django User
The token generated will only authenticate the initial request
The token will not log the user in
The link is not sensitive, and contains no sensitive data
Implementation
The underlying technology used is JWT - see jwt.io for more details on the specifics. The summary is that it can be used to generate a Base64 encoded and signed token based on an arbitrary JSON payload. The signature is used to ensure that the payload has not been tampered with. The payload itself is not encrypted, and can be decoded by anyone - hence ensuring that no sensitive data is included. (NB You can encrypt the token, but the base assumption is that you’re not sending sensitive data, you just want to ensure that the data you receive is the same data you sent.)
The implementation within Django consists of two parts: adding the token to a URL as it leaves the app (e.g. in an email), and then validating the token when someone clicks on the link and returns to the site.
In order to add the token, you need to create a new RequestToken object, and then call the encode method on it. This will return the 3-part signed JWT (header.payload.signature). You will need to initialise the token with the recipient User, and the target URL - to ensure that the token is only used to access the intended endpoint.
..code ::python
>>> from django_jwt.models import RequestToken >>> # create a new RequestToken, and encode the contents >>> token = RequestToken.objects.create_token( ... user=User.objects.filter(...), ... target_url=reverse('foobar') ... ).encode() >>> token 1234567.qwertyuiop.zxcvbnm >>>
You now have a token that is bound to a taget URL, and an intended recipient. If a user (any user - remmember, we are not authenticating the end user) clicks on this URL, they will hit the endpoint as an unauthenticated user. If the URL requires authentication, the request will fail, as the user is not yet authenticated.
In order to use the JWT instead of full authentication, we must add a decorator to the view function to expand out the token, verify it (against tampering, and in line with the “not before time” and “expiration time” attributes of the token payload) and then set the user.
The decorator does one more important task, once the function has run - it records the use of the token - extracting the source IP and user-agent from the request (for auditing purposes), and updating the token use_count property, along with the response status_code - this enables fine-grained reporting on the use of the tokens.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file django-jwt-expiringlinks-0.1.0.tar.gz
.
File metadata
- Download URL: django-jwt-expiringlinks-0.1.0.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74c6091eda6c239d21a12c650af8a3f412edb4e8c778b8ef1e78ca0501f28e10 |
|
MD5 | 6c04f46b505cba9a253b9e9d0db8ab8b |
|
BLAKE2b-256 | ed91ffcd4a86b46c68ad24e8c89cd8ae5ee93522279dd171d43742e142783133 |
File details
Details for the file django_jwt_expiringlinks-0.1.0-py2-none-any.whl
.
File metadata
- Download URL: django_jwt_expiringlinks-0.1.0-py2-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df870b983085b7e9872ae6788512adf818a1d20764ae6602231a100f6dfbc7e2 |
|
MD5 | 84ba16ae8d487c3944aaa7add51fc6a5 |
|
BLAKE2b-256 | 0fb35b29af4afbb2b2be951b4a041cb5a70bdea56fe4d9ed8846ca5ef67a0bbb |
File details
Details for the file django-jwt-expiringlinks-0.1.0.macosx-10.10-x86_64.tar.gz
.
File metadata
- Download URL: django-jwt-expiringlinks-0.1.0.macosx-10.10-x86_64.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56d83820ec1d87e4ce6d0316aeaf8a568edcb9e69d709dadedcf96b2a4954067 |
|
MD5 | edef4e540484f72469cd54c1314bda64 |
|
BLAKE2b-256 | ebc3a6065bfc0f0d2422e2b0289853bf2084685d3cdaf8cf36d33929aaf5a253 |