8000 Add _legacy_get_session_auth_hash to fix Django 3.1 support · codingjoe/django-mail-auth@78c2e2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 78c2e2a

Browse files
committed
Add _legacy_get_session_auth_hash to fix Django 3.1 support
1 parent becd3b7 commit 78c2e2a

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jobs:
44
docs:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/setup-python@v1.1.1
8-
- uses: actions/checkout@v2.0.0
7+
- uses: actions/setup-python@v2
8+
- uses: actions/checkout@v2
99
- run: python -m pip install -r requirements.txt
1010
- run: python setup.py develop
1111
- run: python setup.py build_sphinx -W
@@ -18,16 +18,37 @@ jobs:
1818
python-version: [3.6, 3.7, 3.8]
1919
steps:
2020
- name: Set up Python ${{ matrix.python-version }}
21-
uses: actions/setup-python@v1.1.1
21+
uses: actions/setup-python@v2
2222
with:
2323
python-version: ${{ matrix.python-version }}
24-
- uses: actions/checkout@v2.0.0
24+
- uses: actions/checkout@v2
2525
- run: python setup.py test
2626
- name: Codecov
2727
run: |
2828
python -m pip install codecov
2929
codecov -t ${{secrets.CODECOV_TOKEN}}
3030
31+
extras:
32+
needs: [docs]
33+
runs-on: ubuntu-latest
34+
strategy:
35+
matrix:
36+
extras:
37+
- wagtail
38+ 10000
python-version: [3.8]
39+
steps:
40+
- name: Set up Python ${{ matrix.python-version }}
41+
uses: actions/setup-python@v2
42+
with:
43+
python-version: ${{ matrix.python-version }}
44+
- uses: actions/checkout@v2
45+
- run: python -m pip install -e ".[${{ matrix.extras }}]"
46+
- run: python setup.py test
47+
- name: Codecov
48+
run: |
49+
python -m pip install codecov
50+
codecov -t ${{ secrets.CODECOV_TOKEN }}
51+
3152
3253
PostgreSQL:
3354
needs: [docs]
@@ -50,10 +71,10 @@ jobs:
5071
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
5172
steps:
5273
- name: Set up Python ${{ matrix.python-version }}
53-
uses: actions/setup-python@v1.1.1
74+
uses: actions/setup-python@v2
5475
with:
5576
python-version: ${{ matrix.python-version }}
56-
- uses: actions/checkout@v2.0.0
77+
- uses: actions/checkout@v2
5778
- run: python -m pip install psycopg2-binary Django==${{ matrix.django-version }}
5879
- run: python setup.py test
5980
env:

mailauth/contrib/user/models.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.conf import settings
12
from django.contrib.auth.base_user import BaseUserManager
23
from django.contrib.auth.models import AbstractUser
34
from django.db import models
@@ -62,12 +63,26 @@ def has_usable_password(self):
6263
class Meta(AbstractUser.Meta):
6364
abstract = True
6465

66+
def _legacy_get_session_auth_hash(self):
67+
# RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
68+
key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
69+
if not self.session_salt:
70+
raise ValueError("'session_salt' must be set")
71+
return salted_hmac(key_salt, self.session_salt, algorithm='sha1').hexdigest()
72+
6573
def get_session_auth_hash(self):
6674
"""Return an HMAC of the :attr:`.session_salt` field."""
6775
key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
6876
if not self.session_salt:
6977
raise ValueError("'session_salt' must be set")
70-
return salted_hmac(key_salt, self.session_salt).hexdigest()
78+
return salted_hmac(
79+
key_salt,
80+
self.session_salt,
81+
# RemovedInDjango40Warning: when the deprecation ends, replace
82+
# with:
83+
# algorithm='sha256',
84+
algorithm=settings.DEFAULT_HASHING_ALGORITHM,
85+
).hexdigest()
7186

7287

7388
delattr(AbstractEmailUser, 'password')

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ tests_require =
3838
pytest
3939
pytest-django
4040
pytest-cov
41-
wagtail
4241

4342
[options.package_data]
4443
* = *.txt, *.rst, *.html, *.po

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def admin_user(db):
3636
@pytest.fixture()
3737
def signature():
3838
"""Return a signature matching the user fixture."""
39-
return 'LZ.173QUS.1Hjptg.lf2hFgOXQtjQsFypS2ItRG2hkpA'
39+
return 'LZ.173QUS.1Hjptg.UtFdkTPoyrSA0IB6AUEhtz_hMyFZY0kcREE1HnWdFq4'
4040

4141

4242
@pytest.fixture()

tests/contrib/auth/test_models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ def test_get_session_auth_hash__unique(self, db):
2828

2929
assert spiderman.get_session_auth_hash() != ironman.get_session_auth_hash()
3030

31+
def test_legacy_get_session_auth_hash__default(self, db):
32+
user = EmailUser(email='spiderman@avengers.com')
33+
34+
assert user.session_salt
35+
assert user._legacy_get_session_auth_hash()
36+
37+
def test_legacy_get_session_auth_hash__value_error(self, db):
38+
user = EmailUser(email='spiderman@avengers.com', session_salt=None)
39+
40+
with pytest.raises(ValueError) as e:
41+
user._legacy_get_session_auth_hash()
42+
43+
assert "'session_salt' must be set" in str(e.value)
44+
45+
def test_legacy_get_session_auth_hash__unique(self, db):
46+
spiderman = EmailUser(email='spiderman@avengers.com')
47+
ironman = EmailUser(email='ironman@avengers.com')
48+
49+
assert spiderman._legacy_get_session_auth_hash() != ironman._legacy_get_session_auth_hash()
50+
3151
def test_password_field(self):
3252
user = EmailUser(email='spiderman@avengers.com')
3353
with pytest.raises(FieldDoesNotExist):

0 commit comments

Comments
 (0)
0