|
| 1 | +# python-django-sso-example |
| 2 | +An example Django application demonstrating how to use the [WorkOS MFA API](https://workos.com/docs/mfa/guide) using the [Python SDK](https://github.com/workos-inc/workos-python) to authenticate users. |
| 3 | + |
| 4 | +## Prerequisites |
| 5 | +- Python 3.6+ |
| 6 | + |
| 7 | + |
| 8 | +## Django Project Setup |
| 9 | + |
| 10 | +1. Clone the main git repo for these Python example apps using your preferred secure method (HTTPS or SSH). |
| 11 | + ```bash |
| 12 | + # HTTPS |
| 13 | + $ git clone https://github.com/workos/python-django-example-applications.git |
| 14 | + ``` |
| 15 | + |
| 16 | + or |
| 17 | + |
| 18 | + ```bash |
| 19 | + # SSH |
| 20 | + $ git clone [email protected]:workos/python-django-example-applications.git |
| 21 | + ``` |
| 22 | + |
| 23 | +2. Navigate to the MFA example app within the cloned repo. |
| 24 | + ```bash |
| 25 | + $ cd python-django-example-applications/python-django-mfa-example |
| 26 | + ```` |
| 27 | + |
| 28 | + |
| 29 | +3. Create and source a Python virtual environment. You should then see `(env)` at the beginning of your command-line prompt. |
| 30 | + ```bash |
| 31 | + $ python3 -m venv env |
| 32 | + $ source env/bin/activate |
| 33 | + (env) $ |
| 34 | + ``` |
| 35 | + |
| 36 | +4. Install the cloned app's dependencies. If the `pip` command doesn't work, try `pip3` instead. |
| 37 | + ```bash |
| 38 | + (env) $ pip install -r requirements.txt |
| 39 | + ``` |
| 40 | + |
| 41 | +5. Obtain and make note of the following values. In the next step, these will be set as environment variables. |
| 42 | + - Your [WorkOS API key](https://dashboard.workos.com/api-keys) |
| 43 | + - Your [SSO-specific, WorkOS Client ID](https://dashboard.workos.com/sso/configuration) |
| 44 | + - The redirect URI. For this example, we'll use http://localhost:8000/auth/callback |
| 45 | +
|
| 46 | +6. Ensure you're in the root directory for the example app, `python-django-mfa-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.) |
| 47 | + ```bash |
| 48 | + (env) $ touch .env |
| 49 | + (env) $ nano .env |
| 50 | + ``` |
| 51 | +
|
| 52 | +7. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables: |
| 53 | + ```bash |
| 54 | + export WORKOS_API_KEY=<value found in step 6> |
| 55 | + export WORKOS_CLIENT_ID=<value found in step 6> |
| 56 | + ``` |
| 57 | +
|
| 58 | + To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key. |
| 59 | +
|
| 60 | +8. Source the environment variables so they are accessible to the operating system. |
| 61 | + ```bash |
| 62 | + (env) $ source .env |
| 63 | + ``` |
| 64 | +
|
| 65 | + You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values. |
| 66 | + ```bash |
| 67 | + (env) $ echo $WORKOS_API_KEY |
| 68 | + (env) $ echo $WORKOS_CLIENT_ID |
| 69 | + ``` |
| 70 | +
|
| 71 | +9. Run the Django migrations. Again, ensure you're in the `python-django-mfa-example/` directory where the `manange.py` file is. |
| 72 | + ```bash |
| 73 | + (env) $ python3 manage.py migrate |
| 74 | + ``` |
| 75 | + |
| 76 | + You should see output like: |
| 77 | + ```bash |
| 78 | + Operations to perform: |
| 79 | + Apply all migrations: admin, auth, contenttypes, sessions |
| 80 | + Running migrations: |
| 81 | + Applying contenttypes.0001_initial... OK |
| 82 | + Applying auth.0001_initial... OK |
| 83 | + . . . |
| 84 | + ``` |
| 85 | + |
| 86 | +10. In `python-django-sso-example/mfa/views.py` change the `CONNECTION_ID` string value to the connection ID that you are targeting. This can be found in the WorkOS Dashboard under the Connection Settings. |
| 87 | + |
| 88 | +11. The final setup step is to start the server. |
| 89 | + ```bash |
| 90 | + (env) $ python3 manage.py runserver --insecure |
| 91 | + ``` |
| 92 | + |
| 93 | + 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: |
| 94 | +
|
| 95 | + ```bash |
| 96 | + Watching for file changes with StatReloader |
| 97 | + Performing system checks... |
| 98 | +
|
| 99 | + System check identified no issues (0 silenced). |
| 100 | + March 18, 2021 - 04:54:50 |
| 101 | + Django version 3.1.7, using settings 'workos_django.settings' |
| 102 | + Starting development server at http://127.0.0.1:8000/ |
| 103 | + Quit the server with CONTROL-C. |
| 104 | + ``` |
| 105 | +
|
| 106 | + 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! |
| 107 | +
|
| 108 | + You can stop the local Django server for now by entering `CTRL + c` on the command line. |
| 109 | +
|
| 110 | +
|
| 111 | +## Using the MFA application |
| 112 | +
|
| 113 | +11. This application is meant to showcase the MFA API and how to interact with it using the WorkOS Python SDK. It is not meant to show a real-life example of how MFA should be implemented. |
| 114 | +
|
| 115 | + The app supports two types of MFA flows, SMS and Time-based One Time Password (TOTP). |
| 116 | +
|
| 117 | + SMS: The SMS flow requires you to send a code via text message. You can customize this message, but the message must include the string "{{code}}". This string of characters tells the WorkOS API to generate a random code that will be populated automatically. If "{{code}}" is not included in the message, the authentication cannot be completed. |
| 118 | +
|
| 119 | + TOTP: This type of authentication requires the use of a 3rd party authentication app (1Password, Authy, Google Authenticator, Microsoft Authenticator, Duo, etc). Scan the QR code from the Factor Details page to create the corresponding factor in the 3rd party app, then enter the time-based password when prompted in this MFA application. |
| 120 | +
|
| 121 | + TOTP NOTE - Since all storage is being done via browser cookies, only 1 TOTP type connection can be added at a time to this app due to limitations on the size of the cookies that browsers can store. This is due to the size of the QR code. |
| 122 | +
|
| 123 | +## Need help? |
| 124 | +
|
| 125 | +When you clone this 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. |
| 126 | +
|
| 127 | +If you get stuck, make sure to reference the MFA docs at https://workos.com/docs/mfa/guide. |
| 128 | +
|
| 129 | +If you're still having trouble and aren 't able to resolve the issue by reading our API reference or tutorials, you can reach out to us at [email protected] and we'll lend a hand. |
0 commit comments