Skip to main content

Reusable Django app for Penn Labs accounts

Project description

Django Labs Accounts

CircleCI Coverage Status PyPi Package

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',
    ...
)

(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 object
  • created a boolean delineating if the user was just created
  • dictionary 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()

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.

Changelog

See CHANGELOG.md

License

See LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-labs-accounts-0.8.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_labs_accounts-0.8.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file django-labs-accounts-0.8.0.tar.gz.

File metadata

  • Download URL: django-labs-accounts-0.8.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django-labs-accounts-0.8.0.tar.gz
Algorithm Hash digest
SHA256 fa537531f019c7668251455b1e586bcd6aa1dfb748198a6c1a5ef96972b6eae4
MD5 6349d0425be818e6cfe90bc130cba94a
BLAKE2b-256 26b62293307d21c2ccff5081100d15b9f8affa98f8c8738800745accce49fbbf

See more details on using hashes here.

File details

Details for the file django_labs_accounts-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: django_labs_accounts-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django_labs_accounts-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32cf0f705c53fb4624eea7326c77e37706f496462bb4125fb488547a3af187b2
MD5 9fb9aa4f5b41369ce540b0f88aeb9796
BLAKE2b-256 8513ac395158717771ea1b668b443e46f27364d728f4cec2c42464ca0b5a4113

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page