Skip to content

Commit

Permalink
Merge pull request #2 from dgravitate/master
Browse files Browse the repository at this point in the history
expand history support for 24 passwords
  • Loading branch information
dgravitate authored Sep 1, 2022
2 parents 70eaea5 + 549c8e9 commit 1b49740
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 12 deletions.
9 changes: 8 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ History
++++++++++++++++++

* Set django admin user fields to autocomplete, and change password fields to read only.
* Third release on PyPI.
* Third release on PyPI.


1.0.5 (2022-08-31)
++++++++++++++++++

* Extend support of password history up to 24 passwords.
* Fourth release on PyPI.
2 changes: 1 addition & 1 deletion django_password_history/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.4'
__version__ = '1.0.5'
108 changes: 108 additions & 0 deletions django_password_history/migrations/0002_auto_20220831_2019.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Generated by Django 3.2.13 on 2022-08-31 20:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_password_history', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='userpasswordhistory',
name='password_10',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_11',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_12',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_13',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_14',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_15',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_16',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_17',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_18',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_19',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_20',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_21',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_22',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_23',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_24',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_6',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_7',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_8',
field=models.CharField(blank=True, max_length=128, null=True),
),
migrations.AddField(
model_name='userpasswordhistory',
name='password_9',
field=models.CharField(blank=True, max_length=128, null=True),
),
]
43 changes: 33 additions & 10 deletions django_password_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from django.apps import apps
from django.contrib.auth.hashers import make_password, check_password

DEFAULT_PASSWORD_COUNT = 5 # set to 5 to maintain backward compatibility


# Note: A future version of this will likely change to use an ArrayField or JSONField

class UserPasswordHistory(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Expand All @@ -17,26 +21,45 @@ class UserPasswordHistory(models.Model):
password_3 = models.CharField(blank=True, null=True, max_length=128)
password_4 = models.CharField(blank=True, null=True, max_length=128)
password_5 = models.CharField(blank=True, null=True, max_length=128)
password_6 = models.CharField(blank=True, null=True, max_length=128)
password_7 = models.CharField(blank=True, null=True, max_length=128)
password_8 = models.CharField(blank=True, null=True, max_length=128)
password_9 = models.CharField(blank=True, null=True, max_length=128)
password_10 = models.CharField(blank=True, null=True, max_length=128)
password_11 = models.CharField(blank=True, null=True, max_length=128)
password_12 = models.CharField(blank=True, null=True, max_length=128)
password_13 = models.CharField(blank=True, null=True, max_length=128)
password_14 = models.CharField(blank=True, null=True, max_length=128)
password_15 = models.CharField(blank=True, null=True, max_length=128)
password_16 = models.CharField(blank=True, null=True, max_length=128)
password_17 = models.CharField(blank=True, null=True, max_length=128)
password_18 = models.CharField(blank=True, null=True, max_length=128)
password_19 = models.CharField(blank=True, null=True, max_length=128)
password_20 = models.CharField(blank=True, null=True, max_length=128)
password_21 = models.CharField(blank=True, null=True, max_length=128)
password_22 = models.CharField(blank=True, null=True, max_length=128)
password_23 = models.CharField(blank=True, null=True, max_length=128)
password_24 = models.CharField(blank=True, null=True, max_length=128)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.user.username + "_password_history"
return self.user.username + '_password_history'

def password_is_used(self, password, site_id=1):
previous_passwords_count = 5
previous_passwords_count = DEFAULT_PASSWORD_COUNT
SiteSettings = None

use_site_setting_password_history = getattr(settings,"USE_SITE_SETTINGS_PASSWORD_HISTORY",False)
use_site_setting_password_history = getattr(settings, 'USE_SITE_SETTINGS_PASSWORD_HISTORY', False)

try:
SiteSettings = apps.get_model("setup","SiteSettings")
SiteSettings = apps.get_model('setup', 'SiteSettings')
except:
SiteSettings = None

if use_site_setting_password_history and SiteSettings:
previous_passwords_count = SiteSettings.objects.get(id=site_id).previous_password_count
else:
previous_passwords_count = getattr(settings,"PREVIOUS_PASSWORD_COUNT", 5)
previous_passwords_count = getattr(settings, 'PREVIOUS_PASSWORD_COUNT', DEFAULT_PASSWORD_COUNT)

if previous_passwords_count:
for x in range(1, min(previous_passwords_count, 5) + 1):
Expand All @@ -47,9 +70,9 @@ def password_is_used(self, password, site_id=1):
return False

def store_password(self):
self.password_5 = self.password_4
self.password_4 = self.password_3
self.password_3 = self.password_2
self.password_2 = self.password_1
for x in range(2, 25)[::-1]:
""" Shift all the existing passwords """
setattr(self, f'password_{x}', getattr(self, f'password_{x-1}'))

self.password_1 = self.user.password
self.save()
self.save()
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ def get_version(*file_paths):
keywords='django-password-history',
classifiers=[
'Framework :: Django :: 2.2',
'Framework :: Django :: 3.0',
'Framework :: Django :: 3.1',
'Framework :: Django :: 3.2',
'Framework :: Django :: 4.0',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
)

0 comments on commit 1b49740

Please sign in to comment.