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

Revert crypt hash generation on login #3720

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions lego/api/tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,6 @@ def test_authenticate(self):
self.assertContains(response, text="token", status_code=status.HTTP_201_CREATED)
self.assertContains(response, text="user", status_code=status.HTTP_201_CREATED)

def test_crypt_hash_generated_on_successfull_auth(self):
user = User.objects.get(pk=12)
self.assertEqual(user.crypt_password_hash, "")
user_data = {"username": user.username, "password": "test"}
response = self.client.post(reverse("jwt:obtain_jwt_token"), user_data)
self.assertContains(response, text="token", status_code=status.HTTP_201_CREATED)
self.assertContains(response, text="user", status_code=status.HTTP_201_CREATED)
self.assertNotEqual(User.objects.get(pk=12).crypt_password_hash, "")

def test_crypt_hash_generated_on_successfull_auth_case(self):
user = User.objects.get(pk=12)
self.assertEqual(user.crypt_password_hash, "")
user_data = {"username": "tEsT12", "password": "test"}
response = self.client.post(reverse("jwt:obtain_jwt_token"), user_data)
self.assertContains(response, text="token", status_code=status.HTTP_201_CREATED)
self.assertContains(response, text="user", status_code=status.HTTP_201_CREATED)
self.assertNotEqual(User.objects.get(pk=12).crypt_password_hash, "")

def test_crypt_hash_not_generated_on_failed_auth(self):
user = User.objects.get(pk=12)
self.assertEqual(user.crypt_password_hash, "")
user_data = {"username": user.username, "password": "tes"}
response = self.client.post(reverse("jwt:obtain_jwt_token"), user_data)
self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(User.objects.get(pk=12).crypt_password_hash, "")

def test_refresh(self):
token_response = self.client.post(
reverse("jwt:obtain_jwt_token"), self.user_data
Expand Down
13 changes: 0 additions & 13 deletions lego/apps/users/fixtures/test_users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,3 @@
last_name: medlem
email: allergies@abakus.no
allergies: "Vegetar"

- model: users.User
pk: 12
fields:
username: test12
student_username: test12student
student_verification_status: true
password: pbkdf2_sha256$24000$zowF0cRkFimt$qzeoY9hZ0X3zDhlG0FP8imaGto8S2N6ed1AMp83xcn4= # test
gender: female
first_name: test
last_name: user12
email: test12@user.com
crypt_password_hash: ''
20 changes: 1 addition & 19 deletions lego/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,14 @@
from rest_framework.documentation import include_docs_urls

from rest_framework_jwt.views import (
ObtainJSONWebTokenView,
obtain_jwt_token,
refresh_jwt_token,
verify_jwt_token,
)

from lego.api.urls import urlpatterns as api
from lego.apps.users.models import User
from lego.utils.types import URLList


# START
# Temporary view to generate crypt_hashes for the users that do not have it
class TokenAuthView(ObtainJSONWebTokenView):
def post(self, request, *args, **kwargs):
result = super().post(request, *args, **kwargs)
# If the login is invalid it would have raised an exception by this point
user = User._default_manager.get_by_natural_key(request.data.get("username"))
if user.crypt_password_hash == "":
user.set_password(request.data.get("password"))
user.save()
return result


obtain_jwt_token = TokenAuthView.as_view()
# END

jwt_urlpatterns: URLList = [
re_path(r"^token-auth/$", obtain_jwt_token, name="obtain_jwt_token"),
re_path(r"^token-auth/refresh/$", refresh_jwt_token, name="refresh_jwt_token"),
Expand Down