Skip to content

Support login_required in AuthorizationMiddleware#3676

Merged
hmpf merged 5 commits intomasterfrom
compatibility-with-login-required-middleware
Mar 20, 2026
Merged

Support login_required in AuthorizationMiddleware#3676
hmpf merged 5 commits intomasterfrom
compatibility-with-login-required-middleware

Conversation

@hmpf
Copy link
Copy Markdown
Contributor

@hmpf hmpf commented Dec 10, 2025

Make AuthorizationMiddleware compatible with Django's LoginRequiredMiddleware. With the latter, if a view has the attribute "login_required" set to False, the view is public and everyone is authorized to access it.

This is necessary for supporting django-allauth's "social" login methods (oidc, saml).

This change should not be visible to end users or affect existing views, it will only take effect if we include 3rd party views that are adapted to LoginRequiredMiddleware.

Since ModPython cannot use middlewares that process views, we refactor out what modpython needs into functions used in both middlewares. (Easier to test, too.)

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

@hmpf hmpf added the nonews No news fragment is necessary for this PR (e.g. refactoring, cleanups, workflow/development changes) label Dec 10, 2025
@sonarqubecloud
Copy link
Copy Markdown

@hmpf hmpf self-assigned this Dec 10, 2025
@hmpf hmpf requested review from a team, johannaengland and lunkwill42 December 10, 2025 13:19
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 10, 2025

Test results

    13 files      13 suites   24m 56s ⏱️
 2 899 tests  2 899 ✅ 0 💤 0 ❌
16 950 runs  16 950 ✅ 0 💤 0 ❌

Results for commit ae07bfa.

♻️ This comment has been updated with latest results.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 10, 2025

Codecov Report

❌ Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 63.82%. Comparing base (446fde8) to head (ae07bfa).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
python/nav/web/modpython.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3676      +/-   ##
==========================================
+ Coverage   63.74%   63.82%   +0.07%     
==========================================
  Files         624      624              
  Lines       46154    46175      +21     
  Branches       43       43              
==========================================
+ Hits        29421    29470      +49     
+ Misses      16723    16695      -28     
  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.

@hmpf hmpf force-pushed the compatibility-with-login-required-middleware branch from fd4c392 to 6fd3bd4 Compare March 11, 2026 12:43
@hmpf hmpf requested a review from Simrayz March 11, 2026 12:56
@hmpf hmpf force-pushed the compatibility-with-login-required-middleware branch 2 times, most recently from ff6fcd3 to 3977a3f Compare March 13, 2026 07:48
@hmpf hmpf added auth authentication allauth and removed nonews No news fragment is necessary for this PR (e.g. refactoring, cleanups, workflow/development changes) labels Mar 13, 2026
@hmpf hmpf force-pushed the compatibility-with-login-required-middleware branch from 3977a3f to f3aa882 Compare March 13, 2026 09:08
Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

The change in flow of logic seems reasonable here, but I am still not sure how this will actually affect mod-python...

@hmpf
Copy link
Copy Markdown
Contributor Author

hmpf commented Mar 18, 2026

/../ I am still not sure how this will actually affect mod-python...

We should clean up the modpython stuff anyway (out of scope). The code is as it is to make it work as before, without any deep thinking. Maybe we should have a "modpython" privilege in addition to "web_access"?

@hmpf hmpf requested a review from lunkwill42 March 18, 2026 09:28
Copy link
Copy Markdown
Contributor

@Simrayz Simrayz left a comment

Choose a reason for hiding this comment

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

I think this is good to go after re-organizing the utils 🚀

@@ -0,0 +1,4 @@
Made NAV's authorization system compatible with the LoginRequiredMiddleware
that was added in Django 5.2. A view can now be explicitly marked as not
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: The middleware was added in 5.1 right? Does the changelog refer to the correct version?

Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

Alright, as long as we follow up on this by refactoring to the mentioned utils module (which is a nice idea), I'm good with this as-is :-)

@hmpf hmpf force-pushed the compatibility-with-login-required-middleware branch from 6f7a35c to 1356714 Compare March 19, 2026 14:33
hmpf and others added 3 commits March 19, 2026 15:34
Make AuthorizationMiddleware compatible with Django's
LoginRequiredMiddlware. With the latter, if a view has the attribute
"login_required" set to False, the view is public and everyone is
authorized to access it.

The way we reuse Django's middlewares for ModPython is not compatible
with middlewares that process the view, so we need a ModPython-only
middleware for authorization.
Co-authored-by: Morten Brekkevold <morten.brekkevold@sikt.no>
Remove when we are on Django 5.2!
@hmpf hmpf force-pushed the compatibility-with-login-required-middleware branch from 1356714 to ae07bfa Compare March 19, 2026 14:34
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@Simrayz Simrayz left a comment

Choose a reason for hiding this comment

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

Nice! That looks a lot better organization wise.

@hmpf hmpf merged commit d8578a8 into master Mar 20, 2026
19 checks passed
@hmpf hmpf deleted the compatibility-with-login-required-middleware branch March 20, 2026 13:06
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.

3 participants