Files
galaxy/test/unit/test_security_passwords.py
T
John Chilton 55f7280d79 More password encoding handling fixes.
Fix handling comparison of older hashes in Python 2 and fix the hanlding of both for Python 3.

The previous iteration gave me some errors in Python 3, I brought in and adapted @mvdbeek's Python 3 fixes to fix these:

```
======================================================================
ERROR: test_security_passwords.test_hash_and_check
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/john/workspace/galaxy-lib/.tox/py34/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/john/workspace/galaxy-lib/tests/test_security_passwords.py", line 12, in test_hash_and_check
    simple_pass_hash = passwords.hash_password(simple_pass)
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 21, in hash_password
    return hash_password_PBKDF2(password)
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 44, in hash_password_PBKDF2
    hashed = pbkdf2_bin(smart_str(password), salt, COST_FACTOR, KEY_LENGTH, getattr(hashlib, HASH_FUNCTION))
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 78, in pbkdf2_bin
    rv = u = _pseudorandom(salt + _pack_int(block))
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 75, in _pseudorandom
    return [ord(_) for _ in h.digest()]
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 75, in <listcomp>
    return [ord(_) for _ in h.digest()]
TypeError: ord() expected string of length 1, but int found

======================================================================
ERROR: test_security_passwords.test_hash_consistent
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/john/workspace/galaxy-lib/.tox/py34/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/john/workspace/galaxy-lib/tests/test_security_passwords.py", line 38, in test_hash_consistent
    assert passwords.check_password(simple_pass, simple_pass_hash)
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 30, in check_password
    if check_password_PBKDF2(guess, hashed):
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 53, in check_password_PBKDF2
    hashed_guess = pbkdf2_bin(smart_str(guess), salt, int(cost_factor), KEY_LENGTH, getattr(hashlib, hash_function))
  File "/Users/john/workspace/galaxy-lib/galaxy/security/passwords.py", line 78, in pbkdf2_bin
    rv = u = _pseudorandom(salt + _pack_int(block))
TypeError: Can't convert 'bytes' object to str implicitly
```

Added tests that all now pass in Python 3 - including generating some passwords in Python 3 and verifying they pass when checked in Python 2 and vice versa.
2018-05-23 13:01:05 -04:00

55 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
from galaxy.security import passwords
def test_hash_and_check():
simple_pass = "my simple pass"
complex_pass = u"à strange ünicode ڃtring"
not_pass_simple = "not simple pass"
not_pass_complex = u"à strange ün ڃtring"
simple_pass_hash = passwords.hash_password(simple_pass)
complex_pass_hash = passwords.hash_password(complex_pass)
assert passwords.check_password(simple_pass, simple_pass_hash)
assert passwords.check_password(complex_pass, complex_pass_hash)
assert not passwords.check_password(complex_pass, simple_pass_hash)
assert not passwords.check_password(not_pass_complex, simple_pass_hash)
assert not passwords.check_password(not_pass_simple, simple_pass_hash)
assert not passwords.check_password(simple_pass, complex_pass_hash)
assert not passwords.check_password(not_pass_complex, complex_pass_hash)
assert not passwords.check_password(not_pass_simple, complex_pass_hash)
def test_hash_consistent():
# Make sure hashes created for earlier Galaxies continue to work in the future.
simple_pass = "my simple pass"
complex_pass = u"à strange ünicode ڃtring"
# Check hashes built in Python 2 for 18.05.
simple_pass_hash = 'PBKDF2$sha256$10000$LlkhoImT15LZAcFB$/afKJrFX9GXt6VEpB+omae19A2S4kBEY'
complex_pass_hash = 'PBKDF2$sha256$10000$rg2gPThkJycziB0s$/BJ7AfuaYjpo5hhUvCMhf4TTrhsrOIH1'
# Check hashes built in Python 3 for 18.05.
simple_pass_hash_2 = 'PBKDF2$sha256$10000$vOIv7wGXS2/rGhDu$wS/5NwGSZm1ECv/vSEZb7jKjYSzXWZDL'
complex_pass_hash_2 = 'PBKDF2$sha256$10000$q1av/Clyujm5Fdc6$Dh7o1KTMn/YQYc5NurN+aVaF3uaI8iOv'
# Check older sha1 passwords that may still be the database as well.
simple_pass_hash_old = '58f17339ffdb71050734d2fbdd41b870423fe433'
complex_pass_hash_old = 'b567602039213afc0db026eedd72bd3ee2e57092'
assert passwords.check_password(simple_pass, simple_pass_hash)
assert passwords.check_password(complex_pass, complex_pass_hash)
assert passwords.check_password(simple_pass, simple_pass_hash_2)
assert passwords.check_password(complex_pass, complex_pass_hash_2)
assert passwords.check_password(simple_pass, simple_pass_hash_old)
assert passwords.check_password(complex_pass, complex_pass_hash_old)
assert not passwords.check_password(complex_pass, simple_pass_hash_old)
assert not passwords.check_password(simple_pass, complex_pass_hash_old)