Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bpaulin committed Jun 9, 2020
1 parent 81d436f commit 2fcec21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
12 changes: 8 additions & 4 deletions brownpaperbag/authent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ def _hex_to_digit(toconvert):
return "".join([str(int(i, 16)).zfill(2) for i in toconvert])


def generate_authent(nonce, pwd):
def _generate_random_rb():
return "".join(random.choice(string.ascii_letters) for x in range(20))


def generate_authent(nonce, pwd, rb=None):
"""Return authentification string."""
ra = nonce[2:-2]
ra = _digit_to_hex(ra)
rb = hashlib.sha256(
"".join(random.choice(string.ascii_letters) for x in range(20)).encode()
).hexdigest()
if not rb:
rb = _generate_random_rb()
rb = hashlib.sha256(rb.encode()).hexdigest()
message = ra + rb + SERVER_ID + CLIENT_ID + hashlib.sha256(pwd.encode()).hexdigest()
message = hashlib.sha256(message.encode()).hexdigest()
return {
Expand Down
34 changes: 34 additions & 0 deletions tests/test_authent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,42 @@
from brownpaperbag import authent
from datetime import datetime, timedelta

VALID_PARAMS = {
"nonce": "*#02000314150304060013131006110815150615091405090400021413081501010501090312041402151400131409111112130303100602050504030715110105##",
"password": "azerty123",
"rb": "01234567890123456789",
"client_response": "*#04140706101308030504040601040307120004141509110911020402050400110604000613070802151502120315110208101513101105110402031508081514*05050001021215001503051000021215111412151002000810010600010301150800120007061400090312001104091502130311040403130812110511001215##",
"server_response": "*#09050908071313011009060510000100090607041205031002140513151007131113041101100105001514141503080210020105151101100400140900081007##",
}


def test_convert_digit_hex():
s = "1401120915100305"
h = authent._digit_to_hex(s)
assert s == authent._hex_to_digit(h)


def test_generate_authent():
nonce = VALID_PARAMS["nonce"]
password = VALID_PARAMS["password"]
rb = VALID_PARAMS["rb"]
client = authent.generate_authent(nonce, password, rb)
assert client["client_response"] == VALID_PARAMS["client_response"]

server = authent.check_server_authent(client, VALID_PARAMS["server_response"],)
assert True == server


def test_generate_authent_without_rb():
nonce = VALID_PARAMS["nonce"]
password = VALID_PARAMS["password"]
client = authent.generate_authent(nonce, password)
assert client["rb"] != None


def test_generate_random_rb():
rb1 = authent._generate_random_rb()
assert 20 == len(rb1)
rb2 = authent._generate_random_rb()
assert 20 == len(rb2)
assert rb1 != rb2

0 comments on commit 2fcec21

Please sign in to comment.