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',
'identity.apps.IdentityConfig', # If you want to enable B2B IPC
...
)
Add the new accounts backend to AUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = (
...
'accounts.backends.LabsUserBackend',
'django.contrib.auth.backends.ModelBackend',
...
)
(Optional) Add the new Platform DRF authentication class to rest framework's DEFAULT_AUTHENTICATION_CLASSES
. This authentication class should go at the end of the list of authentication classes in most cases.
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'accounts.authentication.PlatformAuthentication',
]
...
}
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()
B2B IPC
DLA also provides an interface for backend to backend IPC requests. With B2B IPC implemented, the backend of a product will—at startup time—request platform for a JWT to verify its identity. Each product will have an allow-list, and this will enable products to make requests to each other.
In order to limit a view to only be available to a B2B IPC request, you can use the included DRF permission:
from identity.permissions import B2BPermission
class TestView(APIView):
permission_classes = [B2BPermission("urn:pennlabs:example")]
Make sure to define an URN to limit access. Valid URNs are either a specific product (ex. urn:pennlabs:platform
) or a wildcard (ex. urn:pennlabs:*
)
In order to make an IPC request, use the included helper function:
from identity.identity import authenticated_b2b_request
result = authenticated_b2b_request('GET', 'http://url/path')
Use in Production
DLA and Penn Labs' templates are set up so that no configuration is needed to run in development. However, in production a client ID and client secret need to be set. These values should be set in vault. Contact platform for both credentials and any questions you have.
B2B IPC
DLA also provides an interface for backend to backend IPC requests. In order to limit a view to only be available to a B2B IPC request, you can use the included DRF permission:
from identity.permissions import B2BPermission
class TestView(APIView):
permission_classes = [B2BPermission("urn:pennlabs:example")]
Make sure to define an URN to limit access. Valid URNs are either a specific product (ex. urn:pennlabs:platform
) or a wildcard (ex. urn:pennlabs:*
)
In order to make an IPC request, use the included helper function:
from identity.identity import authenticated_b2b_request
result = authenticated_b2b_request('GET', 'http://url/path')
Development Setup
Install poetry:
pipx install poetry
Install Dependencies:
poetry install
Testing:
export DJANGO_SETTINGS_MODULE=tests.settings && poetry run pytest
Linting:
poetry run black . && poetry run isort . && poetry run flake8
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
File details
Details for the file django_labs_accounts-1.0.1.tar.gz
.
File metadata
- Download URL: django_labs_accounts-1.0.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe519874cf5a50df47a8117fc993c9a4a68f5e1a915f04a863eca6ee92d09025 |
|
MD5 | f82d0c9cd8ba25e6c4e57ccfe5c258bd |
|
BLAKE2b-256 | a2ce091b8f042ea04a1182bb6421f10bb4207b243f7bf7bcca22f36a5f95aa82 |
File details
Details for the file django_labs_accounts-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_labs_accounts-1.0.1-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28197a10137e65c8ec81f0c9769d005822c8d110591fb27f7d7d677c7c394f63 |
|
MD5 | 96b133f6fbe7b4e52de1fe20f4e8df8a |
|
BLAKE2b-256 | 450d4b3ec96b09b9f91647aff03e2a58c1690a01f3f87f39f6f693403d9c21b6 |