Skip to content

Commit

Permalink
Fixed dupicite key error
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Apr 8, 2021
1 parent 4e1b6be commit a6a994f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions auth_token/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class KeyGeneratorError(Exception):
pass
14 changes: 11 additions & 3 deletions auth_token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from enumfields import NumEnumField

from chamber.exceptions import PersistenceException
from chamber.models import SmartModel, SmartQuerySet, SmartManager

from generic_m2m_field.models import GenericManyToManyField
Expand All @@ -21,6 +22,7 @@
from auth_token.config import settings

from .enums import AuthorizationRequestState, AuthorizationRequestResult
from .exceptions import KeyGeneratorError


KEY_SALT = 'django-auth-token'
Expand All @@ -40,16 +42,22 @@ def _hash_key(self, key, **kwargs):
return hash_key(key)

def create(self, key_generator, **kwargs):
for attempt in range(settings.MAX_RANDOM_KEY_ITERATIONS + 1):
for attempt in range(1, settings.MAX_RANDOM_KEY_ITERATIONS + 1):
try:
key = key_generator()
hashed_key = self._hash_key(key, **kwargs)
obj = super().create(key=hashed_key, **kwargs)
obj.secret_key = key
return obj
except IntegrityError:
if attempt > settings.MAX_RANDOM_KEY_ITERATIONS:
raise IntegrityError('Could not produce unique key')
if attempt >= settings.MAX_RANDOM_KEY_ITERATIONS:
raise KeyGeneratorError('Could not produce unique key')
except PersistenceException as ex:
if ex.error_dict and 'key' in ex.error_dict:
if attempt >= settings.MAX_RANDOM_KEY_ITERATIONS:
raise KeyGeneratorError('Could not produce unique key')
else:
raise ex


class AuthorizationTokenManager(BaseHashKeyManager):
Expand Down
4 changes: 1 addition & 3 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ pip:
$(PYTHON_BIN)/pip install -r requirements.txt

initvirtualenv:
virtualenv -p $(PYTHON) --no-site-packages $(VIRTUAL_ENV)
$(PYTHON_BIN)/pip install --upgrade pip==1.5
$(PYTHON_BIN)/pip install setuptools --no-use-wheel --upgrade
virtualenv -p $(PYTHON) $(VIRTUAL_ENV)

bootstrap: initvirtualenv pip

Expand Down
21 changes: 21 additions & 0 deletions example/dj/apps/app/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from auth_token.enums import AuthorizationRequestState
from auth_token.exceptions import KeyGeneratorError
from auth_token.models import AnonymousUser, AnonymousAuthorizationToken, MobileDevice
from auth_token.utils import (
compute_expires_at, hash_key, header_name_to_django, generate_key,
Expand Down Expand Up @@ -698,3 +699,23 @@ def test_cancel_authorization_request_should_cancel_authorization_and_not_call_r
cancel_authorization_request(authorization_request)
mocked_receiver.assert_not_called()
assert_equal(authorization_request.refresh_from_db().state, AuthorizationRequestState.CANCELLED)

@freeze_time(now())
def test_duplicite_key_generator_should_raise_exception(self):
create_otp('test', key_generator=lambda: '1234')
with assert_raises(KeyGeneratorError):
create_otp('test', key_generator=lambda: '1234')

@freeze_time(now())
def test_key_genrator_should_be_called_more_times(self):
def key_genertor():
key_genertor.iteration += 1
if key_genertor.iteration < 100:
return '1234'
else:
return '1235'
key_genertor.iteration = 0

create_otp('test', key_generator=lambda: '1234')
assert_equal(create_otp('test', key_generator=key_genertor).secret_key, '1235')
assert_equal(key_genertor.iteration, 100)
2 changes: 1 addition & 1 deletion example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Django==3.1
django-germanium==2.2.5
coverage==5.3.1
django-pyston==2.14.2
django-is-core==2.22.3
django-is-core==2.22.9
djangorestframework==3.12.2
freezegun==1.0.0
-e ../
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'django>=2.2.14',
'django-ipware>=3.0.2',
'import_string==0.1.0',
'django-chamber>=0.5.26',
'django-chamber>=0.6.5',
'django-generic-m2m-field>=0.0.4',
'django-choice-enumfields>=1.0.3',
],
Expand Down

0 comments on commit a6a994f

Please sign in to comment.