Merge pull request #3336 from alphagov/non-consecutive-security-code

Don’t repeat digits in security codes
This commit is contained in:
Chris Hill-Scott
2021-09-30 10:34:59 +01:00
committed by GitHub
2 changed files with 21 additions and 1 deletions

View File

@@ -20,7 +20,15 @@ def _remove_values_for_keys_if_present(dict, keys):
def create_secret_code():
return ''.join(map(str, [SystemRandom().randrange(10) for i in range(5)]))
return ''.join(get_non_repeating_random_digits(5))
def get_non_repeating_random_digits(length):
output = [None] * length
for index in range(length):
while output[index] in {None, output[index - 1]}:
output[index] = str(SystemRandom().randrange(10))
return output
def save_user_attribute(usr, update_dict=None):

View File

@@ -188,6 +188,18 @@ def test_create_secret_code_returns_5_digits():
assert len(str(code)) == 5
def test_create_secret_code_never_repeats_consecutive_digits(mocker):
mocker.patch('app.dao.users_dao.SystemRandom.randrange', side_effect=[
1, 1, 1,
2,
3,
4, 4,
1, # Repeated allowed if not consecutive
9, 9, # Not called because we have 5 digits now
])
assert create_secret_code() == '12341'
@freeze_time('2018-07-07 12:00:00')
def test_dao_archive_user(sample_user, sample_organisation, fake_uuid):
sample_user.current_session_id = fake_uuid