Add SSO to Your Django App Using WorkOS

July 29, 2021

If your SaaS product is built with Python’s Django web framework and you want to add SSO functionality for your customers, you can do a dry-run of our WorkOS SSO integration on our example Django app. It makes use of the WorkOS Python SDK to authenticate users via SSO.

If you get stuck while following the steps below and aren't able to resolve the issue by reading our API reference or SSO Setup Guide, reach out to us at [email protected] so we can help.

Prerequisites

  • Python 3.6+

Django Project Setup

1. In your CLI, navigate to the directory into which you want to clone this git repo.

2. Clone the example Django app’s git repo using your preferred secure method (HTTPS or SSH).

or

Note: When you clone the repo, the DEBUG setting is False by default in workos_django/settings.py. You can set DEBUG=True if you need to troubleshoot something during the tutorial, but you must use DEBUG=False in order to successfully connect to the WorkOS API.

3. Navigate to the cloned repo.

4. Create and source a Python virtual environment. You should then see (env) at the beginning of your command-line prompt.

5. Install the cloned app's dependencies.

6. Obtain and make note of the following values. In the next step, these will be set as environment variables.

7. Ensure you're in the root directory for the example app, python-django-sso-example/. Create a .env file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's .gitignore file, so your sensitive information will not be checked into version control.)

8. Once the Nano text editor opens, you can directly edit the .env file by listing the environment variables:

To exit the Nano text editor, type CTRL + x. When prompted to "Save modified buffer", type Y, then press the Enter or Return key.

For the sake of simplicity, we include a plain text Django SECRET_KEY in python-django-sso-example/workos_django/settings.py. For security, please ensure that you store your production application’s Django SECRET_KEY as a configuration or environment variable and not in the application code.

9. Source the environment variables so they are accessible to the operating system.

You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.

10. Run the Django migrations. Again, ensure you're in the python-django-sso-example/ directory where the manage.pyfile is.

You should see output like:

11. In python-django-sso-example/sso/views.py change the CUSTOMER_EMAIL_DOMAIN string value to an email domain that makes sense for your testing purposes if the default gmail.com isn't relevant.

12. The final setup step is to start the server.

You'll know the server is running when you see no warnings or errors in the CLI, and output similar to the following is displayed:

Navigate to localhost:8000 in your web browser. You should see a "Login" link. If you click this link, you'll be redirected to an HTTP 404 page because we haven't set up SSO yet!

You can stop the local Django server for now by entering CTRL + c on the command line.

SSO Setup with WorkOS

13. Follow the SSO authentication flow instructions to set up an SSO connection. When you get to the step where you provide the REDIRECT_URI value, use http://localhost:8000/auth/callback.

If you get stuck, reach out to us at [email protected] so we can help.

Testing the Integration

14. Navigate to the python-django-sso-example directory, which contains the manage.py file. Source the virtual environment we created earlier, if it isn't still activated from the steps above. Start the Django server locally.

Once running, navigate to http://localhost:8000 to test out the SSO workflow.

Hooray!

Need help?

If you get stuck and aren't able to resolve the issue by reading our API reference or SSO Setup Guide, reach out to us at [email protected] so we can help.

Start Integrating Today
Create an account to begin adding enterprise-ready features to your application today.
Get Started

This site uses cookies to improve your experience. Please accept the use of cookies on this site. You can review our cookie policy here and our privacy policy here. If you choose to refuse, functionality of this site will be limited.