Skip to content
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

Update to work with Django 4.0 #11

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
53 changes: 53 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Test

on:
push:
branches:
- main
pull_request:

jobs:
tests:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
python -m pip install --upgrade tox
- name: Run tox targets for ${{ matrix.python-version }}
run: tox run -f py$(echo ${{ matrix.python-version }} | tr -d .)

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install dependencies
run: |
python -m pip install --upgrade pip tox
- name: Run lint
run: tox -e lint
5 changes: 5 additions & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3.12
3.11
3.10
3.9
3.8
43 changes: 0 additions & 43 deletions .travis.yml

This file was deleted.

15 changes: 4 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
Django PWNED Passwords
======================

.. image:: https://badge.fury.io/py/django-pwned-passwords.svg
:target: https://badge.fury.io/py/django-pwned-passwords

.. image:: https://travis-ci.org/jamiecounsell/django-pwned-passwords.svg?branch=master
:target: https://travis-ci.org/jamiecounsell/django-pwned-passwords

.. image:: https://codecov.io/gh/jamiecounsell/django-pwned-passwords/branch/master/graph/badge.svg
:target: https://codecov.io/gh/jamiecounsell/django-pwned-passwords
This fork currently does not have a pypi package, install it is currently installed directly from github.

django-pwned-passwords is a Django password validator that checks Troy Hunt's PWNED Passwords API to see if a password has been involved in a major security breach before.

Expand All @@ -23,15 +16,15 @@ The full documentation is at https://django-pwned-passwords.readthedocs.io.
Requirements
------------

* Django [1.9, 2.1]
* Python 2.7, [3.5, 3.6, 3.7]
* Django [4.2, 5.0, 5.1]
* Python 3.8, 3.9, 3.10, 3.11, 3.12

Quickstart
----------

Install django-pwned-passwords::

pip install django-pwned-passwords
pip install git+https://github.com/slinkymanbyday/django-pwned-passwords.git

Add it to your `INSTALLED_APPS`:

Expand Down
2 changes: 1 addition & 1 deletion django_pwned_passwords/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.1.0'
__version__ = "5.2.0"
2 changes: 1 addition & 1 deletion django_pwned_passwords/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@


class DjangoPwnedPasswordsConfig(AppConfig):
name = 'django_pwned_passwords'
name = "django_pwned_passwords"
53 changes: 31 additions & 22 deletions django_pwned_passwords/password_validation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.core.exceptions import ValidationError
from django.conf import settings
from django.utils.translation import ugettext as _

import hashlib

import requests
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _


class PWNEDPasswordValidator(object):
Expand All @@ -19,17 +19,30 @@ class PWNEDPasswordValidator(object):

def __init__(self, min_length=8):
self.min_length = min_length
self.timeout = getattr(settings, 'PWNED_VALIDATOR_TIMEOUT', 2)
self.fail_safe = getattr(settings, 'PWNED_VALIDATOR_FAIL_SAFE', True)
self.min_breaches = getattr(settings, 'PWNED_VALIDATOR_MINIMUM_BREACHES', 1)
self.url = getattr(settings, 'PWNED_VALIDATOR_URL',
'https://api.pwnedpasswords.com/range/{short_hash}')
self.error_msg = getattr(settings, 'PWNED_VALIDATOR_ERROR',
"Your password was determined to have been involved in a major security breach.")
self.error_fail_msg = getattr(settings, 'PWNED_VALIDATOR_ERROR_FAIL',
"We could not validate the safety of this password. This does not mean the password is invalid. Please try again later.")
self.help_text = getattr(settings, 'PWNED_VALIDATOR_HELP_TEXT',
"Your password must not have been detected in a major security breach.")
self.timeout = getattr(settings, "PWNED_VALIDATOR_TIMEOUT", 2)
self.fail_safe = getattr(settings, "PWNED_VALIDATOR_FAIL_SAFE", True)
self.min_breaches = getattr(settings, "PWNED_VALIDATOR_MINIMUM_BREACHES", 1)
self.url = getattr(
settings,
"PWNED_VALIDATOR_URL",
"https://api.pwnedpasswords.com/range/{short_hash}",
)
self.error_msg = getattr(
settings,
"PWNED_VALIDATOR_ERROR",
"Your password was determined to have been involved in a major security breach.", # noqa: E501
)
self.error_fail_msg = getattr(
settings,
"PWNED_VALIDATOR_ERROR_FAIL",
"We could not validate the safety of this password. "
"This does not mean the password is invalid. Please try again later.",
)
self.help_text = getattr(
settings,
"PWNED_VALIDATOR_HELP_TEXT",
"Your password must not have been detected in a major security breach.",
)

def validate(self, password, user=None):
if not self.check_valid(password):
Expand All @@ -51,7 +64,7 @@ def check_valid(self, password):
INVALID = False

try:
p_hash = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
p_hash = hashlib.sha1(password.encode("utf-8")).hexdigest().upper()
response = requests.get(self.get_url(p_hash[0:5]), timeout=self.timeout)

if self.get_breach_count(p_hash, response.text) >= self.min_breaches:
Expand All @@ -72,14 +85,10 @@ def check_valid(self, password):
raise ValidationError(self.error_fail_msg)

def get_url(self, short_hash):
return self.url.format(
short_hash = short_hash
)
return self.url.format(short_hash=short_hash)

def get_help_text(self):
return _(
self.help_text
)
return _(self.help_text)

@staticmethod
def get_breach_count(p_hash, response_text):
Expand Down
Loading