Skip to content

Commit 112e409

Browse files
committed
Minor modernizations.
Now that Python 3.9/Django 4.2 are the minimum supported versions, there are a few things that can be removed or updated.
1 parent 8e9bcbb commit 112e409

File tree

2 files changed

+3
-24
lines changed

2 files changed

+3
-24
lines changed

src/pwned_passwords_django/api.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,8 @@ def _prepare_password(self, password: str) -> typing.Tuple[str, str]:
7272
used by the Pwned Passwords API.
7373
7474
"""
75-
# Python's documentation states that the named constructors for particular
76-
# hashes are to be preferred due to better peformance, which here would mean
77-
# calling hashlib.sha1() instead of hashlib.new("sha1").
78-
#
79-
# However, security linters and some restricted runtime environments do not
80-
# allow access to SHA-1 unless a flag is passed to indicate it is not being used
81-
# for cryptographic purposes. This is done by passing usedforsecurity=False to
82-
# the constructor, but that argument was not added in the named constructors
83-
# until Python 3.9, while we currently support all the way back to 3.7. Luckily
84-
# hashlib.new() in Python 3.7 and 3.8 accepts arbitrary arguments without
85-
# complaint. So as a slightly hacky workaround we use new(), passing the
86-
# usedforsecurity=False argument, and rely on it being ignored for Python
87-
# 3.7/3.8, and interpreted as intended on Python 3.9+. Once support for Python
88-
# 3.7 and 3.8 ends, this can be updated to call hashlib.sha1() directly.
8975
password_hash = (
90-
hashlib.new("sha1", password.encode("utf-8"), usedforsecurity=False)
76+
hashlib.sha1(password.encode("utf-8"), usedforsecurity=False)
9177
.hexdigest()
9278
.upper() # Pwned Passwords wants all hashes to be uppercase.
9379
)

src/pwned_passwords_django/middleware.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
# SPDX-License-Identifier: BSD-3-Clause
88

9-
import asyncio
109
import logging
1110
import re
1211
import typing
12+
from inspect import iscoroutinefunction
1313

1414
from django import http
1515
from django.conf import settings
@@ -173,7 +173,7 @@ def some_view(request):
173173
# should return an async middleware that uses an async HTTP client to talk to Pwned
174174
# Passwords. We determine that by checking whether the get_response() callable is a
175175
# coroutine -- if so, we're on the async path.
176-
if asyncio.iscoroutinefunction(get_response):
176+
if iscoroutinefunction(get_response):
177177

178178
async def middleware(request: http.HttpRequest) -> http.HttpResponse:
179179
"""
@@ -183,13 +183,6 @@ async def middleware(request: http.HttpRequest) -> http.HttpResponse:
183183
"""
184184
request.pwned_passwords = []
185185
if request.method == "POST":
186-
# A bug in Django's async test client causes access to request.POST to
187-
# throw an exception unless preceded by an access to
188-
# request.body. Future versions of Django will fix this, but for now we
189-
# do a throwaway access of request.body as a workaround.
190-
#
191-
# See https://code.djangoproject.com/ticket/34063 for details.
192-
request.body # pylint: disable=pointless-statement
193186
request.pwned_passwords = await _scan_payload_async(request)
194187
response = await get_response(request)
195188
return response

0 commit comments

Comments
 (0)