Skip to content

Conversation

hmpf
Copy link
Contributor

@hmpf hmpf commented May 5, 2025

Scope and purpose

django-allauth (hereafter: allauth) is python-social-auths (hereafter: PSA) oldest competitor. It is possible to replace PSA with allauth, and the latter has support for MFA built in.

It is possible to run the two in parallel, there are no clashing module names or table names.

django-allauth does not support LDAP logins.

This pull request (eventually)

  • adds/changes/removes a dependency
  • changes the database
  • changes the API

How to test

Install the dependency: pip install argus-server[allauth-mfa] or pip install django-allauth[mfa]

The easiest way to configure is to use the extra-apps-machinery.

First make sure that argus-htmx overrides settings:

from argus.htmx.appconfig import APP_SETTINGS

update_settings(globals(), APP_SETTINGS, override=True)

Note override=True! If this is not the case, the allauth templates will be completely unstyled and white.

Then set EXTRA_APPS as an environment-variable like so (shell-dependent):

export ARGUS_EXTRA_APPS=`cat allauth.json`

then finally allauth.json needs to look like this:

[
  {
    "app_name": "allauth",
    "settings": { "USE_PYTHON_SOCIAL_AUTH": false }
  },
  {
    "app_name": "allauth.account",
    "middleware": {
      "allauth.account.middleware.AccountMiddleware": "end"
    },
    "settings": {
      "ACCOUNT_ADAPTER": "argus.auth.allauth.ArgusAccountAdapter",
      "ACCOUNT_ALLOW_SIGNUPS": false,
      "LOGIN_URL": "/accounts/login/",
      "PUBLIC_URLS": [
        "/accounts/login/",
        "/accounts/signup/",
        "/api/"
      ]
    }
  },
  {
    "app_name": "allauth.mfa",
    "settings": {
      "MFA_TOTP_ISSUER": "Argus",
      "MFA_TOTP_TOLERANCE": 0
    }
  }
]

MFA_TOTP_TOLERANCE is to account for clock drift. 0 is default. 1 is for network lag, 2 works for TOTP smartcards. Lower is better.

Run migrations. allauth.account adds the tables account_emailaddress and account_emailconfirmation. allauth.mfa adds the tables mfa_authenticator.

You must be logged in and you must have an email address verified the allauth way. Then you can visit '/accounts/2fa' to get the QR code/magic string to set up a totp provider.

OAuth2/SAML/OIDC

Install the dependency: pip install argus-server[allauth-social] or pip install django-allauth[socialaccount]

Add to allauth.json:

  {
    "app_name": "allauth.socialaccount",
    "settings": {                                                               
      "SOCIALACCOUNT_AUTO_SIGNUP": true,                                        
      "SOCIALACCOUNT_ADAPTER": "argus.auth.allauth.ArgusSocialAccountAdapter"         
    }
  }

This is JSON so validate with jq or something to ensure that right commas are in place, JSON doesn't like comma before }.

Run migrations. The tables socialaccount_socialaccount, socialaccount_socialapp and socialaccount_socialtoken are added.

Finally one app per provider is needed, here showing dataporten:

  {
    "app_name": "allauth.socialaccount.providers.dataporten"
  }

The settings for the provider needs to be set per site so directly in a settings-file. Here's copying the values used for PSA:

SOCIALACCOUNT_PROVIDERS = {
    "dataporten": {
        "EMAIL_AUTHENTICATION": True,
        "VERIFIED_EMAIL": True,
        "APP": {
            "client_id": get_str_env("ARGUS_DATAPORTEN_KEY", required=True),
         #   "key": get_str_env("ARGUS_DATAPORTEN_KEY", required=True),
            "secret": get_str_env("ARGUS_DATAPORTEN_SECRET", required=True),
        }
    }
}

At this point there's a new page on /accounts/3rdparty/.

Contributor Checklist

Every pull request should have this checklist filled out, no matter how small it is.
More information about contributing to Argus can be found in the
Development docs.

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation
  • Linted/formatted the code with ruff and djLint, easiest by using pre-commit
  • The first line of the commit message continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See our how-to
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done
  • If this results in changes in the UI: Added screenshots of the before and after
  • If this results in changes to the database model: Updated the ER diagram

Known missing

Delete as they are fixed.

  • Pretty templates
  • Docs
  • ...

@hmpf hmpf added documentation Improvements or additions to documentation enhancement New feature or request frontend Affects frontend data model Affects the data model and/or SQL schema dependencies Run `tox -r` before testing locally, dependencies have changed auth Touches the authentication/authorization subsystem research Research needed user administration HTMx Views, urls, templates... new setting Needs a new setting in settings.py, possibly with validator change setting Name, location or default value of a setting was changed data migration Batch change to data in the database labels May 5, 2025
@hmpf hmpf moved this from 📋 Backlog to 🏗 In progress in Argus development, public May 5, 2025
@hmpf hmpf self-assigned this May 5, 2025
Copy link

github-actions bot commented May 5, 2025

Test results

    4 files    468 suites   17m 37s ⏱️
  565 tests   564 ✅ 1 💤 0 ❌
2 260 runs  2 256 ✅ 4 💤 0 ❌

Results for commit d499b14.

♻️ This comment has been updated with latest results.

@codecov-commenter
Copy link

codecov-commenter commented May 5, 2025

Codecov Report

❌ Patch coverage is 23.44498% with 160 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.33%. Comparing base (7703e72) to head (d499b14).

Files with missing lines Patch % Lines
src/argus/auth/allauth/utils/psa.py 0.00% 46 Missing ⚠️
...th/allauth/management/commands/convert_from_psa.py 0.00% 28 Missing ⚠️
...allauth/management/commands/delete_allauth_data.py 0.00% 23 Missing ⚠️
src/argus/auth/allauth/views.py 0.00% 17 Missing ⚠️
src/argus/auth/allauth/adapter.py 0.00% 14 Missing ⚠️
src/argus/auth/psa/utils.py 44.44% 10 Missing ⚠️
src/argus/auth/allauth/urls.py 0.00% 5 Missing ⚠️
src/argus/auth/allauth/utils/__init__.py 0.00% 5 Missing ⚠️
src/argus/auth/psa/htmx/views.py 50.00% 4 Missing ⚠️
src/argus/htmx/auth/urls.py 0.00% 4 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1406      +/-   ##
==========================================
- Coverage   78.25%   76.33%   -1.93%     
==========================================
  Files         125      138      +13     
  Lines        5519     5712     +193     
==========================================
+ Hits         4319     4360      +41     
- Misses       1200     1352     +152     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hmpf
Copy link
Contributor Author

hmpf commented May 5, 2025

In order to use MFA with allauth it is necessary to have a verified email address.

The template to verify an address is the same used to add and remove addresses, and to set one of several addresses as "primary". It is not possible to hide the "Add new address"-form since it is also used to change an address, by first adding an address then removing the old.

We add addresses via "destinations" and by getting a "synced" address from OAuth2 so we need to adapt somehow. Either copy addresses in destinations to allauth's system or generate destinations from allauth's system or sync both ways. Either way, a "synced" address (copied from social account) cannot be deleted so we cannot leave this form and view untouched.

Copy link

@hmpf hmpf changed the title Use django-allauth for MFA Use django-allauth for MFA and replace python sopcial auth Oct 7, 2025
@hmpf hmpf changed the title Use django-allauth for MFA and replace python sopcial auth Use django-allauth for MFA and replace python social auth Oct 7, 2025
@hmpf hmpf force-pushed the allauth branch 5 times, most recently from a5e0285 to 3a5087a Compare October 10, 2025 09:36
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Touches the authentication/authorization subsystem change setting Name, location or default value of a setting was changed data migration Batch change to data in the database data model Affects the data model and/or SQL schema dependencies Run `tox -r` before testing locally, dependencies have changed documentation Improvements or additions to documentation enhancement New feature or request frontend Affects frontend HTMx Views, urls, templates... new setting Needs a new setting in settings.py, possibly with validator research Research needed user administration

Projects

Status: 🏗 In progress

Development

Successfully merging this pull request may close these issues.

2 participants