Skip to content

Commit

Permalink
Make auth token generation a testable function
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Aug 17, 2023
1 parent 5980ad6 commit f742a3c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/zinolib/ritz.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@

import logging
import socket
import hashlib
import enum
import ipaddress
from datetime import datetime, timedelta
Expand All @@ -89,7 +88,7 @@
import codecs
import select

from .utils import windows_codepage_cp1252
from .utils import windows_codepage_cp1252, generate_authtoken


codecs.register_error("windows_codepage_cp1252", windows_codepage_cp1252)
Expand Down Expand Up @@ -489,13 +488,12 @@ def authenticate(self, user, password):
ritz_session.connect()
ritz_session.authenticate("username","password")
"""
# Authenticate user
if not self.connStatus:
raise NotConnectedError("Not connected to device")

# Combine Password and authChallenge from Ritz to make authToken
genToken = "%s %s" % (self.authChallenge, password)
authToken = hashlib.sha1(genToken.encode("UTF-8")).hexdigest()
authToken = generate_authtoken(self.authChallenge, password)

# Authenticate user
cmd = "user %s %s -" % (user, authToken)
# try:
try:
Expand Down Expand Up @@ -647,40 +645,36 @@ def clean_attributes(self, caseinfo):

return cleaninfo

def get_raw_history(self, caseid):
# gethist Get Logs from CaseID
# Parameters: caseID
# Returns a list of historylines (timestamp, message)??
self.check_connection()
self.check_id(caseid, "CaseID")

return self._request(b"gethist %d" % caseid)

def get_history(self, caseid):
"""Return all history elements of a CaseID
Usage:
case_history = ritz_session.get_history(123)
"""
response = self.get_raw_history(caseid)
return _decode_history(response.data)

def get_raw_log(self, caseid):
# getlog Get Logs from CaseID
# gethist Get Logs from CaseID
# Parameters: caseID
# Returns a list of loglines (timestamp, message)
# Returns a list of historylines (timestamp, message)??
self.check_connection()
self.check_id(caseid, "CaseID")

return self._request(b"getlog %d" % caseid)
response = self._request(b"gethist %d" % caseid)

return _decode_history(response.data)

def get_log(self, caseid):
"""Return all log elements of a CaseID
Usage:
case_logs = ritz_session.get_log(123)
"""
response = self.get_raw_log(caseid)
# getlog Get Logs from CaseID
# Parameters: caseID
# Returns a list of loglines (timestamp, message)
self.check_connection()
self.check_id(caseid, "CaseID")

response = self._request(b"getlog %d" % caseid)

return _decode_history(response.data)

def add_history(self, caseid, message):
Expand Down
17 changes: 17 additions & 0 deletions src/zinolib/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import hashlib


__all__ = [
"windows_codepage_cp1252",
"generate_authtoken",
]


def windows_codepage_cp1252(error):
"""Windows Codepage 1252 decoder fallback
Expand Down Expand Up @@ -39,3 +48,11 @@ def windows_codepage_cp1252(error):
result.append(chr(0x00 + byte))

return "".join(result), error.end


def generate_authtoken(challenge, password):
"Combine Password and authChallenge from Ritz to make authToken"

raw_token = "%s %s" % (challenge, password)
token = hashlib.sha1(raw_token.encode("UTF-8")).hexdigest()
return token
14 changes: 14 additions & 0 deletions tests/test_zinolib_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import hashlib
import unittest

from zinolib.utils import generate_authtoken


class GenerateAuthtokenTest(unittest.TestCase):

def test_generate_authtoken(self):
challenge = "ababp"
password = "fillifjonka"
expected = "84f9c302c392488f3f04f69f4c87994e10511892"
result = generate_authtoken(challenge, password)
self.assertEqual(expected, result)

0 comments on commit f742a3c

Please sign in to comment.