-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'force-change-pw' into 'main'
Add user option to force password change on next login See merge request reportcreator/reportcreator!782
- Loading branch information
Showing
14 changed files
with
261 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ def assert_login(self, user, password=None, success=True, status='success'): | |
self.assert_api_access(False) | ||
return res | ||
|
||
def assert_mfa_login(self, mfa_method, data=None, user=None, success=True): | ||
def assert_mfa_login(self, mfa_method, data=None, user=None, success=True, status='success'): | ||
self.assert_login(user=user or self.user_mfa, status='mfa-required') | ||
if mfa_method.method_type == MFAMethodType.BACKUP: | ||
res = self.client.post(reverse('auth-login-code'), data={ | ||
|
@@ -62,12 +62,23 @@ def assert_mfa_login(self, mfa_method, data=None, user=None, success=True): | |
|
||
if success: | ||
assert res.status_code == 200 | ||
self.assert_api_access(True) | ||
assert res.data['status'] == status | ||
if status == 'success': | ||
self.assert_api_access(True) | ||
else: | ||
assert res.status_code in [400, 403] | ||
self.assert_api_access(False) | ||
return res | ||
|
||
def assert_change_password(self, password=None, success=True): | ||
res = self.client.post(reverse('auth-change-password'), data={'password': password or get_random_string(32)}) | ||
if success: | ||
assert res.status_code == 200 | ||
assert res.data['status'] == 'success' | ||
else: | ||
assert res.status_code in [400, 403] | ||
return res | ||
|
||
def test_login(self): | ||
self.assert_login(user=self.user) | ||
self.assert_api_access(True) | ||
|
@@ -116,6 +127,32 @@ def test_login_mfa_method_of_other_user(self): | |
other_mfa = MFAMethod.objects.create_totp(user=other_user) | ||
self.assert_mfa_login(user=self.user_mfa, mfa_method=other_mfa, success=False) | ||
|
||
def test_must_change_password(self): | ||
self.user.must_change_password = True | ||
self.user.save() | ||
|
||
self.assert_login(self.user, status='password-change-required') | ||
self.assert_api_access(False) | ||
self.assert_change_password(password=get_random_string(3), success=False) | ||
self.assert_api_access(False) | ||
self.assert_change_password() | ||
self.assert_api_access(True) | ||
|
||
self.user.refresh_from_db() | ||
assert not self.user.must_change_password | ||
|
||
def test_must_change_password_mfa(self): | ||
self.user_mfa.must_change_password = True | ||
self.user_mfa.save() | ||
|
||
self.assert_mfa_login(mfa_method=self.mfa_totp, user=self.user_mfa, status='password-change-required') | ||
self.assert_api_access(False) | ||
self.assert_change_password() | ||
self.assert_api_access(True) | ||
|
||
self.user.refresh_from_db() | ||
assert not self.user.must_change_password | ||
|
||
@override_settings(REMOTE_USER_AUTH_ENABLED=True, REMOTE_USER_AUTH_HEADER='Remote-User') | ||
def test_login_remoteuser(self): | ||
AuthIdentity.objects.create(user=self.user_mfa, provider=AuthIdentity.PROVIDER_REMOTE_USER, identifier='[email protected]') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
api/src/reportcreator_api/users/migrations/0014_pentestuser_must_change_password.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.3 on 2024-11-27 09:11 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0013_pentestuser_is_project_admin'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='pentestuser', | ||
name='must_change_password', | ||
field=models.BooleanField(default=False, verbose_name='Must change password at next login'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.