SceneID authentication for Django
Project description
django-sceneid
SceneID authentication for Django
How it works
It's assumed that your website has an existing login system, based on Django's authentication framework. SceneID does not replace it - the ability to log in via SceneID will be offered as an alternative alongside the standard login form. The existing User model will continue to be used, regardless of whether a user logs in through SceneID or a regular username / password.
The 'Sign in with SceneID' link will authenticate the user against SceneID. If the SceneID account is already associated with a user account on the website (as stored in the database under the sceneid.SceneID model), that user is immediately logged in and your website can refer to request.user
as normal. Otherwise, the user is redirected to a 'connect' view, prompting them to either log in with an existing username / password, or register a new account (in which case they'll be prompted for a username and other required fields, but not password). In both cases, the resulting user account will be linked to the SceneID account so that they can log in immediately next time.
Demo site (including much of the configuration and customisation described below) - https://github.com/demozoo/django-sceneid-demosite
Installation
django-sceneid is compatible with Django 3.x and 4.x. To install:
pip install django-sceneid
Add 'sceneid'
to your project's INSTALLED_APPS
, then run ./manage.py migrate
.
In your project's top-level URL config, include the URLconf sceneid.urls
:
from django.urls import include, path
urlpatterns = [
# ...
path('account/sceneid/', include('sceneid.urls')),
# ...
]
At this point you'll need to obtain an API key from scene.org. They'll want to know your redirect URI, which is the path you used above with login/
appended; for example https://demosite.example.com/account/sceneid/login/
. Once you have this, add the following settings to your project's settings file:
# The client ID you received from scene.org
SCENEID_CLIENT_ID = 'demosite'
# The client secret you received from scene.org
SCENEID_CLIENT_SECRET = 'supersecretclientsecret'
# The root URL of your website, without a trailing slash
BASE_URL = 'https://demosite.example.com'
# Where to redirect to after login (defaults to /accounts/profile/)
LOGIN_REDIRECT_URL = '/'
Integrating into your site
The simplest way to add a 'sign in with SceneID' button to your site is to add the sceneid_login_button_small
or sceneid_login_button_large
template tag in a suitable place on your template:
{% load sceneid_tags %} {# must be at the top of your template #}
{% if not request.user.is_authenticated %}
{% sceneid_login_button_large %}
{% endif %}
This will output a button linking to the login view, with a 'next' parameter that will redirect back to the current page after login (unless the current page is a POST request, in which case this will be omitted). To override this, pass a next_url
parameter to the tag - this can be the desired redirect URL, or False to disable it entirely.
{% sceneid_login_button_large next_url='/some/other/path/' %}
{% sceneid_login_button_large next_url=False %}
If you don't want to use the button images, the sceneid_auth_url
tag outputs just the URL, to be inserted into a link of your choice. This tag also accepts a next_url
parameter, as well as supporting the as var
syntax to write the result to a variable:
{% sceneid_auth_url next_url='/some/other/path/' as auth_url %}
<a href="{{ auth_url }}">Sign in with SceneID</a>
Customisation
At minimum, you'll probably want to customise the template for the 'connect' view to match your site styling. The quick-and-dirty way to do this is to create a template with the path sceneid/connect.html
within one of your apps, and ensuring that template takes precedence over the default one by placing that app's entry above 'sceneid'
in INSTALLED_APPS
. This template receives the following context variables:
user_data
- the dictionary of user data returned from SceneID, consisting ofid
,first_name
,last_name
anddisplay_name
login_form
- a Django form object for the login form, to be POSTed to{% url 'sceneid:connect_old' %}
register_form
- a Django form object for the registration form, to be POSTed to{% url 'sceneid:connect_new' %}
For more control over customisations, you can override the AppConfig for the sceneid
app. Within your project-level configuration folder (i.e. the one containing the settings
and urls
submodules), add the following to apps.py
:
from sceneid.apps import SceneIDConfig
class DemositeSceneIDConfig(SceneIDConfig):
connect_template_name = 'accounts/connect.html'
Then replace the 'sceneid'
entry in INSTALLED_APPS
with the dotted path to the AppConfig class:
INSTALLED_APPS = [
# ...
'demosite.apps.DemositeSceneIDConfig',
# ...
]
The template path specified in connect_template_name
will then be used for the 'connect' view.
Custom forms
The form classes used for the login and registration forms are also configurable from the AppConfig class:
connect_login_form_class
(default:'django.contrib.auth.forms.AuthenticationForm'
)connect_register_form_class
(default:'sceneid.forms.UserCreationForm'
)
In particular, the provided UserCreationForm
can be subclassed to add extra fields to the data collected on account creation. For example, to include fields for first name and last name (which will then populate the corresponding fields on the standard Django user model):
# accounts/forms.py
from sceneid.forms import UserCreationForm as BaseUserCreationForm
class SceneIDUserCreationForm(BaseUserCreationForm):
class Meta(BaseUserCreationForm.Meta):
fields = ('username', 'first_name', 'last_name')
# demosite/apps.py
from sceneid.apps import SceneIDConfig
class DemositeSceneIDConfig(SceneIDConfig):
connect_template_name = 'accounts/connect.html'
connect_register_form_class = 'accounts.forms.SceneIDUserCreationForm'
The base UserCreationForm
class recognises the field names first_name
and last_name
where present and pre-populates these from the SceneID data, in addition to pre-populating the username
field with a cleaned version of the SceneID display name.
Using django-sceneid with a custom User model is currently untested. The base UserCreationForm
assumes the presence of a 'username'
field and will need to be customised accordingly if this is not the case (e.g. if your site uses email as a user's identifier instead), but all other functionality (including the login form) should work unchanged.
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 Distribution
File details
Details for the file django-sceneid-0.1.1.tar.gz
.
File metadata
- Download URL: django-sceneid-0.1.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d333dd077a1ec9050d210f4c76c568925a70323da5b044ac9de48cd6223e3a2b |
|
MD5 | f154ce165e6412d8b52f91e56bc9b0b8 |
|
BLAKE2b-256 | d98e3ec7bd7458f076556d524abaccce5290c5ee7c15f2af6a0d404a38184188 |
File details
Details for the file django_sceneid-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_sceneid-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff8ef4b3e17c217fe62fc3e5bd6f355d034bf11962bb500c0080c23acf8bd566 |
|
MD5 | 107fe6721dbcde28e53724361bfdbff9 |
|
BLAKE2b-256 | cb0836291f486e2ab322018233a818d04bd216945b6662e495951a712edda0dd |