django-sqrl-2/README.md

203 lines
4.7 KiB
Markdown
Raw Normal View History

2019-08-14 11:52:59 -05:00
# SQRL for Django
2019-09-04 21:08:57 -05:00
This package allows for developers to easily create SQRL-aware websites with a
few simple steps.
**(This document was written for the future. Things that are not yet implemented
will be ~~struck out~~. Non-functional code snippets will be `#commented out`)**
2019-08-14 12:14:52 -05:00
This is a reboot of [Miki725's django-sqrl](https://github.com/miki725/django-sqrl),
2019-08-14 11:52:59 -05:00
updated for Python 3.7 and Django 2.2. The vast majority of the code is unchanged,
so all credit for this working belongs with them.
2019-09-04 21:08:57 -05:00
* Free software: MIT license
* GitHub: https://gitlab.com/WolfgangAxel/django-sqrl-2
* ~~Documentation: https://django-sqrl-2.readthedocs.org~~
* What is SQRL?: https://sqrl.grc.com/pages/what_is_sqrl/
* SQRL documentation: https://www.grc.com/sqrl/sqrl.htm
## Installing
### SQRL Package
First step is to install `django-sqrl-2` which is easies to do using pip:
```
$ #python3 -m pip install django-sqrl-2
```
2019-09-04 21:08:57 -05:00
### Django settings
Once installed there are a few required changes in Django settings:
* Make sure that some required Django apps are used:
```
INSTALLED_APPS = [
2019-09-14 04:38:14 -05:00
...,
'sqrl',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.staticfiles',
...
]
```
2019-09-04 21:08:57 -05:00
* Make sure that some required Django middleware are used:
```
MIDDLEWARE_CLASSES = [
2019-09-14 04:38:14 -05:00
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
]
```
2019-09-04 21:08:57 -05:00
* Change `AUTHENTICATION_BACKENDS` to use SQRL backend vs Django's
`ModelBackend` (default):
```
AUTHENTICATION_BACKENDS = [
2019-09-14 04:38:14 -05:00
'sqrl.backends.SQRLModelBackend',
]
```
2019-09-04 21:08:57 -05:00
* If you are using Django admin, following are required:
* Make sure that `sqrl` is listed before `admin` in the `INSTALLED_APPS`.
This allows Django to prioritize `sqrl` templates since `django-sqrl`
overwrites some of them.
```
INSTALLED_APPS = [
2019-09-14 04:38:14 -05:00
...,
'sqrl',
'django.contrib.admin',
...
]
```
2019-09-04 21:08:57 -05:00
* Make sure to add a custom template directory in settings. `django-sqrl`
extends Django admin's `base.html` which by default causes infinite recursion.
To solve that, simply add a custom template directory which allows `django-sqrl`
to explicitly extend from `django.contrib.admin` `base.html` template:
```
import os
import django
TEMPLATE_DIRS = [
2019-09-14 04:38:14 -05:00
os.path.dirname(django.__file__),
]
```
2019-09-04 21:08:57 -05:00
## URLs
All of SQRL functionality is enabled by adding its URLs to the root URL config:
```
from django.urls import path, include
2019-09-04 21:08:57 -05:00
urlpatterns = [
2019-09-14 04:38:14 -05:00
...
path('sqrl/', include('sqrl.urls', namespace="sqrl")),
...
]
```
2019-09-04 21:08:57 -05:00
If you use Django admin, the `/admin/sqrl_manage` endpoint will be available to manage
2019-09-04 21:08:57 -05:00
your site's SQRL identities.
## Templates
Now that SQRL is installed in your Django project, you can use it on any login
page with three simple template tags:
2019-09-04 21:08:57 -05:00
```
{% load sqrl %}
{% sqrl as sqrl_session %}
2019-09-14 04:38:14 -05:00
{% sqrl_login_dropin sqrl_session "named:redirect" %}
```
The `"named:redirect"` is the page that should be redirected to after logging
in. Any name that can be resolved by django's `reverse` function will work. For
example, here are the necessary lines needed to resolve `"app:main"`:
* Main project's `urls.py`:
```
urlpatterns = [
...
path("", include('app.urls', namespace="app"))
...
]
```
* Your `app`'s `urls.py`:
```
urlpatterns = [
...
path("", main_view, name="main")
...
]
```
2019-09-04 21:08:57 -05:00
2019-09-14 04:38:14 -05:00
* Your `main_view`'s template:
```
...
{% load sqrl %}
{% sqrl as sqrl_session %}
{% sqrl_login_dropin sqrl_session "app:main" %}
...
```
2019-09-04 21:08:57 -05:00
These three tags will add a simple element to your login page:
![A very basic SQRL QR code](docs/example_dropin.png "example dropin")
If that doesn't suit your fancy, you may build your own template from the
following essential tags:
```
{% load sqrl %}
{% sqrl as sqrl_session %}
<a href="{{ sqrl_session.sqrl_url }}">
2019-09-14 04:38:14 -05:00
<div id="sqrl-qr" data-sqrl="{{ sqrl_session.sqrl_url }}"></div>
</a>
<script>SQRL_NEXT="{{ your desired redirect }}"; SQRL_CHECK_URL="{% sqrl_status_url_script_tag sqrl_session %}"</script>
<script type="application/javascript" src="{% static 'sqrl/sqrl.js' %}"></script>
```
2019-09-04 21:08:57 -05:00
## Management Command
SQRL uses server state to keep track of open SQRL transactions in order to
mitigate replay attacks. Since this state will constantly grow if not cleared,
`django-sqrl` provides a helper management command to clear expired states:
```
$ python3 manage.py clearsqrlnuts
```
2019-09-04 21:08:57 -05:00
It is recommended to run this command as repeating task. Here is an example
configuration for `cron`:
```
*/5 * * * * python manage.py clearsqrlnuts >/dev/null 2>&1
```
2019-09-04 21:08:57 -05:00
## ~~Testing~~
~~To run the tests, you need to install the testing requirements first:~~
```
$ #make install
```
2019-09-04 21:08:57 -05:00
~~Then to run the tests, you can use use the Makefile command:~~
2019-08-14 11:52:59 -05:00
```
$ #make test
```