Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Bare minimum password obfuscation #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions siricontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import email
import os
import pkgutil
import sys
import base64
from os.path import expanduser, dirname, exists, join, abspath

##########################################

# Add your gmail username and password here
##########################################

username = ""
password = ""
# Add your gmail username and password to
# a file called .siricontrolsecret

##########################################

SECFILE = ".siricontrolsecret"

class ControlException(Exception):
pass
Expand Down Expand Up @@ -128,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(username, password)
Control(*get_credentials())