Skip to main content

Rownd bindings for Django

Project description

Rownd bindings for Django

Easily add Rownd instant accounts to your Django-backed apps.

Installation

Begin by adding the rownd_django package to your dependencies. In requirements.txt, this would look like:

rownd_django>=1.2.1

NOTE: This plugin only works with Django v3 and above. We strongly recommend upgrading if you're using something older. If you can't for some reason, please get in touch.

Next, add the Rownd app and authentication backend to your Django settings.py file.

INSTALLED_APPS = [
    ...
    'rownd_django',
]

AUTHENTICATION_BACKENDS = [
    'rownd_django.auth.backend.RowndAuthenticationBackend',
    'django.contrib.auth.backends.ModelBackend'
]

If you're using Django REST Framework, then add the Rownd authentication class to your REST_FRAMEWORK settings.

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rownd_django.auth.backend.RowndApiAuthentication',
    ]
}

Finally, add your Rownd app credentials to your Django settings.py file. You can obtain these from the Rownd dashboard. These credentials enable the Rownd authentication backend to communicate with the Rownd API.

ROWND = {
    'APP_KEY': '<your app key>',
    'APP_SECRET': '<your app secret>',
}

A note on Google Sign-in

If you plan to use Google sign-in, you'll need to add or update one last configuration item in your settings.py while developing locally without HTTPS connections enabled. Without this setting, the Google One Tap iframe will not load correctly due to a missing Referrer header.

    SECURE_REFERRER_POLICY = "no-referrer-when-downgrade"

Configure the Rownd Hub (required)

Rownd authentication requires a small code snippet to be embedded within your app, present on all HTML pages. Setup for the Hub/snippet itself is outside the scope of this document, but you can find the relevant setup guides for either single page apps or traditional web apps via vanilla js.

Use our SDKs to embed the Hub/snippet in your SPA or use the vanilla JS SDK to add the snippet in your main Django template HTML.

Now that everything is set up, you can add Rownd authentication to your APIs or views.

Usage

The Rownd Django SDK provides support for both "traditional" Django apps where you have an authentication session that follows a user across page loads, as well as "single-page" (SPA) Django apps using frameworks like React, Vue, etc.

Single-page apps (SPA) / API-based

When using an SPA framework like React, Vue, or similar, you'll likely want to leverage the specific Rownd SDK for those frameworks. You can find a list of supported frameworks in our documentation.

Typically, an SPA will use an API-driven request/response flow which makes typical sessions unnecessary (though Rownd supports them if you need them). We highly recommend the Django REST framework for this purpose. Rownd provides plug-and-play support for the REST framework's authentication API.

Here's an example of how you might configure an API to leverage Rownd's authenticator, given the installation instructions above:

from rownd_django.auth.backend import RowndApiAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

class ExampleView(APIView):
    authentication_classes = [RowndApiAuthentication]
    permission_classes = [IsAuthenticated]

    def get(self, request, format=None):
        content = {
            'user': str(request.user),  # `django.contrib.auth.User` instance.
            'auth': str(request.auth),  # None
        }
        return Response(content)

Traditional (non-SPA) apps / session-based

In this flow, once a user has been authenticated with the Rownd Hub, the Hub will make a request to your app's backend to set up a session for the user.

First, ensure your project has session middleware enabled.

MIDDLEWARE = [
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    ...
]

Next, include the Rownd session_authenticator in your urls.py file.

urlpatterns = [
    ...
    path('rownd/', include('rownd_django.auth.urls', namespace='rownd')),
    ...
]

Finally, update your Rownd Hub code snippet to fire post-authenticate and post-sign-out API requests to the session authenticator we just enabled.

<script type="text/javascript">
(function () {
    // Rownd Hub snippet
})();
</script>
<script type="text/javascript">
    function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    _rphConfig.push(['setAppKey', '<rownd app key>']);
    _rphConfig.push(['setPostAuthenticationApi', {
        method: 'post',
        url: '/rownd/session_authenticate',
        extra_headers: {
            'X-CSRFToken': getCookie('csrftoken')
        }
    }]);
    _rphConfig.push(['setPostSignOutApi', {
        method: 'post',
        url: '/rownd/session_sign_out',
        extra_headers: {
            'X-CSRFToken': getCookie('csrftoken')
        }
    }]);
</script>

The session authenticator will establish an authenticated session if one doesn't already exist and will return a response indicating that the Rownd Hub should trigger a page refresh. This is usually necessary for your app views to display the desired authenticated context. In the event that an authenticated session already exists, the Hub will not trigger further page refreshes.

CSRF Protection

By default CSRF protection is disabled on the two sign-in and sign-out routes provided by Rownd. If you would like to enable it on those endpoints, you must ensure all of your sites views contain the csrftoken cookie and update your settings to enable the CSRF protection. You can find more information on the csrftoken cookie here.

ROWND = {
    'APP_KEY': '<your app key>',
    'APP_SECRET': '<your app secret>',
    'CSRF_PROTECT_ROUTES': True
}

Custom User Model

If you are using a customized user model that changes the default USERNAME_FIELD value from username, you must tell the Rownd SDK the property name.

ROWND = {
    'USER_MODEL_USERNAME_FIELD': 'email',
}

Download files

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

Source Distribution

rownd_django-1.2.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

rownd_django-1.2.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file rownd_django-1.2.1.tar.gz.

File metadata

  • Download URL: rownd_django-1.2.1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for rownd_django-1.2.1.tar.gz
Algorithm Hash digest
SHA256 d33aa6f5a6eeee5aa007a07c02b100c02fb9be100095a2f7c959aa1c37699b5c
MD5 3cc39934cf1639bc307f5561762836dd
BLAKE2b-256 cfe68596474be654952fe7c8825300fa785036a7575516404f932e33a9a034d6

See more details on using hashes here.

File details

Details for the file rownd_django-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: rownd_django-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for rownd_django-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 deb980fdebcdcb0eeb30c2c3d1a0f8e78f48c820a9d281be63ed75bc173d02fb
MD5 60d2f41221a520b8030af44eac906504
BLAKE2b-256 b9125ece5386cd6578b6128e7894e77a2ef2351fce396c09edd6ad45085d0a36

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