Skip to content

Conversation

@lunkwill42
Copy link
Member

@lunkwill42 lunkwill42 commented Oct 22, 2025

Scope and purpose

Fixes #3498.

This adds a new Django authentication backend implementation that reframes the legacy NAV LDAP authentication into Django's way of doing authentication.

The back-end is only implemented, not activated. Se subsequent PRs for this.

Contributor Checklist

Every pull request should have this checklist filled out, no matter how small it is.
More information about contributing to NAV can be found in the
Hacker's guide to NAV.

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation
  • Linted/formatted the code with ruff, easiest by using pre-commit
  • Wrote the commit message so that the first line 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 https://cbea.ms/git-commit/
  • Based this pull request on the correct upstream branch: For a patch/bugfix affecting the latest stable version, it should be based on that version's branch (<major>.<minor>.x). For a new feature or other additions, it should be based on master.
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done
  • If it's not obvious from a linked issue, described how to interact with NAV in order for a reviewer to observe the effects of this change first-hand (commands, URLs, UI interactions)
  • If this results in changes in the UI: Added screenshots of the before and after
  • If this adds a new Python source code file: Added the boilerplate header to that file

@lunkwill42 lunkwill42 added nonews No news fragment is necessary for this PR (e.g. refactoring, cleanups, workflow/development changes) ldap authentication labels Oct 22, 2025
@hmpf hmpf force-pushed the towards-django-auth branch from 12fddee to 720763d Compare October 22, 2025 14:18
@github-actions
Copy link

github-actions bot commented Oct 22, 2025

Test results

    27 files      27 suites   45m 43s ⏱️
 2 636 tests  2 636 ✅ 0 💤 0 ❌
19 478 runs  19 478 ✅ 0 💤 0 ❌

Results for commit 8619aef.

♻️ This comment has been updated with latest results.

@codecov
Copy link

codecov bot commented Oct 22, 2025

Codecov Report

❌ Patch coverage is 96.72131% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.54%. Comparing base (ac1e688) to head (8619aef).

Files with missing lines Patch % Lines
python/nav/web/auth/__init__.py 75.00% 1 Missing ⚠️
python/nav/web/auth/ldap_auth_backend.py 98.24% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3624      +/-   ##
==========================================
+ Coverage   62.49%   62.54%   +0.04%     
==========================================
  Files         611      612       +1     
  Lines       45103    45153      +50     
  Branches       43       43              
==========================================
+ Hits        28188    28241      +53     
+ Misses      16905    16902       -3     
  Partials       10       10              

☔ 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.

@lunkwill42 lunkwill42 force-pushed the feature/nav-django-ldap-backend branch from 129f823 to c45f372 Compare October 22, 2025 14:30
@hmpf hmpf force-pushed the towards-django-auth branch from 720763d to 55c9d73 Compare October 23, 2025 07:51
@lunkwill42 lunkwill42 force-pushed the feature/nav-django-ldap-backend branch from 79e2568 to 1946a3c Compare October 23, 2025 08:34
Base automatically changed from towards-django-auth to master October 23, 2025 08:38
@lunkwill42 lunkwill42 removed the nonews No news fragment is necessary for this PR (e.g. refactoring, cleanups, workflow/development changes) label Oct 23, 2025
Preparing a new LDAP authentication backend module for integrating NAV's
LDAP authentication with Django. When finished, there should be no
LDAP-specific stuff in the generic auth module.
@lunkwill42 lunkwill42 force-pushed the feature/nav-django-ldap-backend branch from a0b4085 to 78c2967 Compare October 23, 2025 08:41
This `LdapBackend` can replace the existing legacy LDAP login flow in
NAV, by re-using the `nav.web.auth.ldap` module, and without
unnecessarily leaking LDAP abstractions and errors to the rest
of the login system.
Grokking what this function did from its name wasn't all too easy.
Also, given all the other methods of the LdapBackend class, it fit
better as an extra method of that class.

Additionally, added a more explicit docstring.
This ensures basic OpenLDAP library configuration is installed in the
devcontainer. These configuration files are necessary in order to point
the OpenLDAP library to the correct set of CA certificates to consider
when verifying LDAP server connections. Without this, setting up NAV
to authenticate against an SSL-protected LDAP server will just fail.
@sonarqubecloud
Copy link

@johannaengland johannaengland requested review from a team and hmpf October 23, 2025 14:57
@johannaengland johannaengland marked this pull request as ready for review October 23, 2025 15:22
@hmpf hmpf requested a review from johannaengland October 30, 2025 13:19
Copy link
Contributor

@hmpf hmpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this can safely be merged to master.

Then poc/allauth needs to move to the new master, and then rebase the dependents. (poc/django-auth-2, poc/allauth-dataporten, feature/enable-django-auth.. I think that's all of them.)

def test_given_invalid_ldap_user_info_raise_permission_denied_exception(
self, mock_authenticate, db, ldap_synced_account
):
mock_authenticate.side_effect = PermissionDenied
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes no sense. The low-level LDAP authenticate() function would never raise a Django specific exception.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and I can't request changes when reviewing, since I originated this PR 😆 )

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make a Django auth backend for logging into NAV with LDAP, NAV-style

4 participants