-
Notifications
You must be signed in to change notification settings - Fork 45
POC: Use Django authentication system with NAV's Account
model
#3493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Test results 9 files 9 suites 7m 59s ⏱️ For more details on these failures, see this check. Results for commit 6d59681. ♻️ This comment has been updated with latest results. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3493 +/- ##
==========================================
+ Coverage 61.35% 61.76% +0.41%
==========================================
Files 611 611
Lines 44782 44860 +78
Branches 43 43
==========================================
+ Hits 27476 27709 +233
+ Misses 17296 17141 -155
Partials 10 10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
And as evidenced by the test suite, the tests will fail because of this: We test that every model defined in the system seems to work on the SQL level, and naturally those tests will fail for the new models introduced by the new apps in |
Missing: django expects "request.user" to be set, we have hardcoded on "request.account". With this, both are supported. |
'django.contrib.auth', | ||
'django.contrib.contenttypes', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably switch places
It is also highly likely that |
And, as discussed, I think this was already fixed in #3332. But the broader scope here would be to stop using |
cf1eb61
to
1e79972
Compare
There are two failing tests now: tests/integration/sql_test.pyThe tables and other things that Django expects to be there (auth.(Group|Permission), ContentType) are put in the public namespace (what postgres calls a schema) and this test checks that the public namespace is empty. tests/integration/web/webfront_test.py::test_shows_password_issue_banner_to_admins_on_other_users_password_issuesThe string "There are 1 accounts that have insecure or old passwords." does not show up in the generated page. |
2488d54
to
05cb1d1
Compare
Manually putting the Django tables into the "profiles" schema fixed this test. If we add more non-NAV tables we should probably reconsider what schemas to use, I would prefer one per app. |
The string is set by the context processor Liberally spreading "assert False" inside |
The context processor is used but does not get an admin user. The context from |
Might add a historic note here: As you know, NAV web tools were never divided into "apps" in the first place, because much of the web code predates Django. The reason there are multiple namespaces in the PostgreSQL database at all is that there were originally at least four separate PostgreSQL databases involved in NAV - which, of course, made it impossible to utilize proper referential integrity between parts of the data. The namespaces we use today are remnants of this: They are named after the originally separate databases. |
The context processor gets the anonymous user actually (default account). |
The |
There are two different ways to check if an account has admin powers:
Both ways could be |
05cb1d1
to
8234053
Compare
The Django auth system uses the `is_active` property to decide whether a user is allowed to log in. NAV's Account class already has the attribute `locked` for this purpose. In order to edge towards compatibility with Django Auth, this adds `is_active` as the negation of `locked`.
Django's auth system uses the generic concept of "natural keys" to fetch user objects by their names (rather than their primary keys), as entered in a login form. In order for the Django auth system to be able to look up NAV user accounts, the Account model and its manager need to provide the necessary calls.
These are necessary for Django's auth system to work. However, since we use our custom migration system, a custom migration script for these apps needs to be created later.
This replaces the authentication mechanism used in the `do_login` view to instead employ Django's authentication system. The default list of authentication backends include only the `ModelBackend`, which will use NAV's `Account` model for authentication. This temporarily disconnects LDAP authentication, as part of a proof-of- concept. LDAP authentication will have to be added as a separate back-end. Additionally, it turns out the `AUTH_USER_MODULE` was set incorrectly, since the app label we use for NAV models is simply `models`.
8234053
to
b74282b
Compare
If you log in with github, an account and it's social auth table is correctly created. But: the user lacks a name and password so it is locked, and the user is sent straight to the login page.
Setting password and setting other user data, if done in two different steps, could be configurable. So: either ask for a password or set a random password. I kinda want to set up a test-NAV with this now... |
How data from the external auth is mapped to the local account can be controlled via |
So I have a partial pipeline that shows a form that asks for a new password, but if the pipeline handles the save, the form data will be sent as GET-parameters.. |
b74282b
to
6d59681
Compare
|
Scope and purpose
This builds a proof-of-concept for #2688, by replacing the internal NAV mechanism used by the
do_login
view for authenticating users with Django's standard mechanism (theModelBackend
by default).The POC temporarily disables LDAP authentication to show how this can be achieved. LDAP authentication should probably be re-implemented as a new authentication backend to be added to NAV's list of Django authentication backends.
The
django.contrib.auth
anddjango.contrib.contenttypes
apps are added to theINSTALLED_APPS
setting to enable this. ATM, the POC works, but since NAV uses a custom migration system, the Django development web server will complain that there are unapplied migrations for these apps. The database models provided by these apps aren't touched by NAV, which is why it still works without them - but, as I believe has happened before, the migrations should probably be replicated as pure SQL and squashed into a single NAV migration script (switching to the Django migration system would likely be a huge endeavor, and is out of scope here).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.
<major>.<minor>.x
). For a new feature or other additions, it should be based onmaster
.