Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Better handle incorrect password. Bump to 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ksya committed Jan 19, 2020
1 parent e3ec9a3 commit 647b1ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
57 changes: 33 additions & 24 deletions aionefit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,45 @@ def connect(self):
def disconnect(self):
self.xmppclient.disconnect()

def raw_message_callback(self, msg):
def raw_message_callback(self, msg, source):
decodeSuccess = False
if msg['type'] in ('chat', 'normal'):
headers = msg['body'].split("\n")[:-1]
body = msg['body'].split("\n")[-1:][0]
_LOGGER.debug('headers: %s', headers)
_LOGGER.debug('body: %s', body)
response = self.encryption.decrypt(body)
_LOGGER.debug('response: %s', response)

statusline = headers[0]
statuscode = statusline.split(" ")[1]
if statuscode == "204":
_LOGGER.debug('Empty message (204 No Content) received')

elif statuscode == "200":
try:
data = json.loads(response)
if self.message_callback:
self.message_callback(data)
except json.decoder.JSONDecodeError as e:
_LOGGER.error('Error parsing message %s', msg)
_LOGGER.error(e)
return
else:
_LOGGER.error("Errormessage received: %s", statusline)
if response:
_LOGGER.error(response)

try:
# Try to decode the message. Will fail if password is incorrect
response = self.encryption.decrypt(body)
_LOGGER.debug('response: %s', response)

statusline = headers[0]
statuscode = statusline.split(" ")[1]
if statuscode == "204":
_LOGGER.debug('Empty message (204 No Content) received')

elif statuscode == "200":
try:
data = json.loads(response)
if self.message_callback:
self.message_callback(data)
except json.decoder.JSONDecodeError as e:
_LOGGER.error('Error parsing message %s', msg)
_LOGGER.error(e)
return
else:
_LOGGER.error("response is null")
raise SystemError(statusline)
_LOGGER.error("Errormessage received: %s", statusline)
if response:
_LOGGER.error(response)
else:
_LOGGER.error("response is null")
raise SystemError(statusline)
except UnicodeDecodeError:
if source == 'message':
_LOGGER.error('Error decrypting message. Password incorrect?')
if self.failed_auth_handler:
self.failed_auth_handler('auth_error_password')

def get(self, path):
"""Construct a "GET command"
Expand Down
3 changes: 2 additions & 1 deletion aionefit/provider/pyaes_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def decrypt(self, enc):
try:
r = decrypted.decode("utf8").rstrip(chr(0))
except UnicodeDecodeError as e:
raise SystemError("Decryption error (%s). Wrong password?", e)
raise

return r

def _pad(self, s):
Expand Down
7 changes: 3 additions & 4 deletions aionefit/provider/slixmpp_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def on_failed_auth(self, event):
else:
_LOGGER.error('failed_auth event: %s', event)
raise SystemError('Invalid login. Check credentials ' +
'(serial_number, access_key, password).')
'(serial_number, access_key).')

def on_auth_success(self, event):
"""Callback handler for a successfull authentication.
Expand All @@ -67,14 +67,13 @@ def session_end(self, event):
def message_callback(self, msg):
"""Callback handler for a received message.
"""
self.nefit_client.raw_message_callback(msg)
self.nefit_client.raw_message_callback(msg, 'message')
self.message_event.set()

def carbonmsg_recv_callblack(self, msg):
"""Callback handler for a received carbon message.
"""
self.nefit_client.raw_message_callback(msg['carbon_received'])
self.message_event.set()
self.nefit_client.raw_message_callback(msg['carbon_received'], 'carbon')

def carbonmsg_sent_callblack(self, msg):
"""Callback handler for a sent carbon message.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EMAIL = 'marconfus@gmail.com'
AUTHOR = 'Marco Reichwald'
REQUIRES_PYTHON = '>=3.5.0'
VERSION = "0.5"
VERSION = "0.6"

# What packages are required for this module to be executed?
REQUIRED = [
Expand Down

0 comments on commit 647b1ea

Please sign in to comment.