Reusable Django app for Penn Labs accounts
Project description
Django Labs Accounts
Requirements
- Python 3.6+
- Django 2.1+
Installation
Install with pip pip install django-labs-accounts
Add accounts
to INSTALLED_APPS
INSTALLED_APPS = (
...
'accounts.apps.AccountsConfig',
...
)
Add the new accounts backend to AUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
...
'accounts.backends.LabsUserBackend',
'django.contrib.auth.backends.ModelBackend',
...
)
Add the new accounts middleware to MIDDLEWARE
. Note the middleware does not need to be at the top of the list, but should be placed above the default Django middleware.
MIDDLEWARE = [
...
'accounts.middleware.OAuth2TokenMiddleware',
...
]
Add the following to urls.py
urlpatterns = [
...
path('accounts/', include('accounts.urls', namespace='accounts')),
...
]
Documentation
All settings are handled within a PLATFORM_ACCOUNTS
dictionary.
Example:
PLATFORM_ACCOUNTS = {
'CLIENT_ID': 'id',
'CLIENT_SECRET': 'secret',
'REDIRECT_URI': 'example',
'ADMIN_PERMISSION': 'example_admin'
'CUSTOM_ADMIN': True
}
The available settings are:
CLIENT_ID
the client ID to connect to platform with. Defaults to LABS_CLIENT_ID
environment variable.
CLIENT_SECRET
the client secret to connect to platform with. Defaults to LABS_CLIENT_SECRET
environment variable.
REDIRECT_URI
the redirect uri to send to platform. Defaults to first the LABS_REDIRECT_URI
environment variable and then generating the value from the request object.
SCOPE
the scope for this applications tokens. Must include introspection
. Defaults to ['read', 'introspection']
.
PLATFORM_URL
URL of platform server to connect to. Should be https://platform(-dev).pennlabs.org
(no trailing slash)
ADMIN_PERMISSION
The name of the permission on platform to grant admin access. Defaults to example_admin
CUSTOM_ADMIN
enable the custom admin login page to log in users through platform. Defaults to True
When developing locally with an http (not https) callback URL, it may be helpful to set the OAUTHLIB_INSECURE_TRANSPORT
environment variable.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = "1"
Custom post authentication
If you want to customize how DLA saves user information from platform into User objects, you can subclass accounts.backends.LabsUserBackend
and redefine the post_authenticate method. This method will be run after the user is logged in. The parameters are:
user
the user objectcreated
a boolean delineating if the user was just createddictionary
a dictionary of user information from platform.
Then just set the AUTHENTICATION_BACKENDS
setting to be the subclassed backend.
Here is an example of a custom backend that sets every user's first name to "Modified"
.
from accounts.backends import LabsUserBackend
class CustomBackend(LabsUserBackend):
def post_authenticate(self, user, created, dictionary):
user.first_name = 'Modified'
user.save()
Changelog
See CHANGELOG.md
License
See LICENSE
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
Hashes for django-labs-accounts-0.5.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49a35aa30941dae7b6f50c8dab52b29cef4fc24a3abf4301b8aa55571ec180cc |
|
MD5 | 7ad4ea04b4be55c0a214f4c4904db0d1 |
|
BLAKE2b-256 | bd2500f443d4e5ddffa2c7ce7245cc8b599b720d079e34464faad94b0c294f5f |
Hashes for django_labs_accounts-0.5.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b692b9eb097dc619890b639de9e962d6df7b137a9ded39f3e021860e7e9a5f2b |
|
MD5 | 1a05acaa877f6648bb16c6e9911d3383 |
|
BLAKE2b-256 | 6594b463b848903eb7045eb8b7ce3f8c43f7a19457d056b24e48539ca3434b69 |