From bcd8044fe64dbf152021af629de82b84f21875f7 Mon Sep 17 00:00:00 2001 From: toyg Date: Sat, 5 Aug 2017 01:24:36 +0100 Subject: [PATCH 1/3] added bare minimum credential obfuscation. --- siricontrol.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/siricontrol.py b/siricontrol.py index b5ef526..b82e336 100644 --- a/siricontrol.py +++ b/siricontrol.py @@ -3,16 +3,52 @@ import email import os import pkgutil +from os.path import expanduser, dirname, exists, join, abspath +from base64 import b64encode, b64decode ########################################## -# Add your gmail username and password here - -username = "" -password = "" +# Add your gmail username and password to +# a file called .siricontrolsecret ########################################## +SECFILE = ".siricontrolsecret" +def get_credentials(): + possible_locations = set([ + expanduser('~'), + abspath(dirname(__file__)), + os.getcwd() + ]) + user = "" + pwd = "" + for dir_path in possible_locations: + sec_path = join(dir_path, SECFILE) + if exists(sec_path): + try: + sf = open(sec_path, "r") + lines = sf.readlines() + user = lines[0].strip() + pwd = lines[1].strip() + sf.close() + if not pwd.startswith("{enc}"): + # pwd not encoded + pwd = "{enc}" + b64encode(pwd) + sf = open(sec_path, "w") + sf.write("\n".join([user , pwd])) + sf.close() + return user, b64decode(pwd[5:]) + except IOError as e: + print("Found " + sec_path + " but I couldn't read it: " + e) + except IndexError: + print("File " + sec_path + " must contain two lines, " + + " username and password, to be valid.") + + #still here? I couldn't find anything + raise Exception(SECFILE + + " not found, please create it" + + " in one of these folders:\n" + + "\n".join(possible_locations)) class ControlException(Exception): pass @@ -130,4 +166,4 @@ def handle(self): if __name__ == '__main__': - Control(username, password) + Control(get_credentials()) From 59705e6ea06208f0c9452ace9d00a89b5f02f233 Mon Sep 17 00:00:00 2001 From: toyg Date: Sat, 5 Aug 2017 01:43:46 +0100 Subject: [PATCH 2/3] fixed py3 compatibility for password encoding --- siricontrol.py | 86 +++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/siricontrol.py b/siricontrol.py index b82e336..71a7df2 100644 --- a/siricontrol.py +++ b/siricontrol.py @@ -3,8 +3,10 @@ import email import os import pkgutil +import sys +import base64 from os.path import expanduser, dirname, exists, join, abspath -from base64 import b64encode, b64decode + ########################################## @@ -14,41 +16,6 @@ ########################################## SECFILE = ".siricontrolsecret" -def get_credentials(): - possible_locations = set([ - expanduser('~'), - abspath(dirname(__file__)), - os.getcwd() - ]) - user = "" - pwd = "" - for dir_path in possible_locations: - sec_path = join(dir_path, SECFILE) - if exists(sec_path): - try: - sf = open(sec_path, "r") - lines = sf.readlines() - user = lines[0].strip() - pwd = lines[1].strip() - sf.close() - if not pwd.startswith("{enc}"): - # pwd not encoded - pwd = "{enc}" + b64encode(pwd) - sf = open(sec_path, "w") - sf.write("\n".join([user , pwd])) - sf.close() - return user, b64decode(pwd[5:]) - except IOError as e: - print("Found " + sec_path + " but I couldn't read it: " + e) - except IndexError: - print("File " + sec_path + " must contain two lines, " + - " username and password, to be valid.") - - #still here? I couldn't find anything - raise Exception(SECFILE + - " not found, please create it" + - " in one of these folders:\n" + - "\n".join(possible_locations)) class ControlException(Exception): pass @@ -164,6 +131,53 @@ def handle(self): print("Restarting...") time.sleep(1) +def _encode_password(mypwd): + if sys.version_info[0] == 2: + return base64.encodestring(mypwd).strip("\n") + else: + return base64.b64encode(mypwd.encode('utf-8')).decode('utf-8') + +def _decode_password(mypwd): + if sys.version_info[0] == 2: + return base64.decodestring(mypwd) + else: + return base64.b64decode(mypwd).decode('utf-8') + +def get_credentials(): + # SECFILE can be placed in one of these directories + possible_locations = set([ + expanduser('~'), # home + abspath(dirname(__file__)), # same dir as script + os.getcwd() # working directory + ]) + user = "" + pwd = "" + for dir_path in possible_locations: + sec_path = join(dir_path, SECFILE) + if exists(sec_path): + try: + sf = open(sec_path, "r") + lines = sf.readlines() + user = lines[0].strip() + pwd = lines[1].strip() + sf.close() + if not pwd.startswith("{enc}"): + # pwd not encoded + pwd = "{enc}" + _encode_password(pwd) + sf = open(sec_path, "w") + sf.write("\n".join([user , pwd])) + sf.close() + return user, _decode_password(pwd[5:]) + except IOError as e: + print("Found " + sec_path + " but I couldn't read it: " + e) + except IndexError: + print("File " + sec_path + " must contain two lines, " + + " username and password, to be valid.") + #still here? I couldn't find anything + raise Exception(SECFILE + + " not found, please create it" + + " in one of these folders:\n" + + "\n".join(possible_locations)) if __name__ == '__main__': Control(get_credentials()) From 7e127f81b51c44992158edd48f78b18a180135d1 Mon Sep 17 00:00:00 2001 From: toyg Date: Sat, 5 Aug 2017 01:46:38 +0100 Subject: [PATCH 3/3] fixed typo --- siricontrol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/siricontrol.py b/siricontrol.py index 71a7df2..cc9c932 100644 --- a/siricontrol.py +++ b/siricontrol.py @@ -180,4 +180,4 @@ def get_credentials(): "\n".join(possible_locations)) if __name__ == '__main__': - Control(get_credentials()) + Control(*get_credentials())