def authenticate(driver): config = SecureConfig().decrypt(util.secure_config_file) # log in to myob using 2-factor authentication encoded_redirect_url = urlparse.quote(config['redirect_url']) login_url = f'https://secure.myob.com/oauth2/account/authorize?' \ f'client_id={config["myob_client_id"]}&' \ f'redirect_uri={encoded_redirect_url}&' \ f'response_type=code&' \ f'scope=CompanyFile' driver.get(login_url) element = driver.find_element_by_id("UserName") element.send_keys(config['myob_username']) driver.find_element_by_xpath('//button[text()="Next "]').click() element = driver.find_element_by_id("Password") element.send_keys(config['myob_password']) driver.find_element_by_xpath('//button[text()="Sign in"]').click() element = driver.find_element_by_id("Token") totp = pyotp.TOTP(config['myob_ga_secret']) element.send_keys(totp.now()) driver.find_element_by_xpath('//button[text()="Verify"]').click() # get code from redirect_url while True: if driver.current_url.startswith(config['redirect_url']): break time.sleep(0.5) parsed = urlparse.urlparse(driver.current_url) code = parse_qs(parsed.query)['code'][0] # get access token url = 'https://secure.myob.com/oauth2/v1/authorize/' payload = { 'code': code, 'client_id': config['myob_client_id'], 'client_secret': config['myob_client_secret'], 'redirect_uri': config['redirect_url'], 'scope': 'CompanyFile', 'grant_type': 'authorization_code' } headers = {'Content-Type': 'application/x-www-form-urlencoded'} r = requests.post(url, data=payload, headers=headers) auth_tokens = r.json() with open(config['token_file'], "w") as f: data = { 'access_token': auth_tokens['access_token'], 'expires_at': datetime.now() + timedelta(seconds=(int(auth_tokens['expires_in']) - 60)) } print(f"{json.dumps(data, default=datetime_converter)}", file=f) return driver
def gen_encrypted_config(key_path, conf_path): sconf = SecureConfig.from_key(read(key_path)) conf = ConfigParser.ConfigParser() conf.read(conf_path) for section in conf.sections(): sconf.add_section(section) for item_key, item_value in conf.items(section): sconf.set(section, item_key, item_value) return sconf
def get_access_token(): config = SecureConfig().decrypt(util.secure_config_file) if os.path.isfile(config['token_file']): with open(config['token_file'], "r") as f: data = json.load(f) if datetime.now() > parser.parse(data['expires_at']): authenticate() return get_access_token() else: return data['access_token'] else: authenticate() return get_access_token()
def get_access_token(driver): config = SecureConfig().decrypt(util.secure_config_file) if os.path.isfile(config['token_file']): with open(config['token_file'], "r") as f: data = json.load(f) if datetime.now() > parser.parse(data['expires_at']): authenticate(driver) return get_access_token(driver) else: return data['access_token'] else: authenticate(driver) url = 'https://essentials.myob.co.nz/LA.CO.NZ/app.htm#businesses/179995/invoices/new' driver.get(url) return get_access_token(driver)
import util import time import pyotp from secureconfig import SecureConfig from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException config = SecureConfig().decrypt(util.secure_config_file) def log_in(driver): driver.get('https://essentials.myob.co.nz/') WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "UserName"))) element = driver.find_element_by_id("UserName") element.send_keys(config['myob_username']) driver.find_element_by_xpath('//button[text()="Next "]').click() element = driver.find_element_by_id("Password") element.send_keys(config['myob_password']) driver.find_element_by_xpath('//button[text()="Sign in"]').click() element = driver.find_element_by_id("Token") totp = pyotp.TOTP(config['myob_ga_secret']) element.send_keys(totp.now()) driver.find_element_by_xpath('//button[text()="Verify"]').click() #@util.wait_for_enter_before_execution @util.wait_before_execution(wait_s=2) def go_to_invoices_page(driver): text = 'Sales'
from __future__ import print_function import os from secureconfig import SecureConfig # here we're going to open the file we encrypted in the previous demo # and make sure we can decrypt and parse it using the same keystring. CWD = os.path.dirname(os.path.realpath(__file__)) output_path = os.path.join(CWD, 'demo_secureconfig_data.enc') TEST_KEYSTRING = 'sFbO-GbipIFIpj64S2_AZBIPBvX80Yozszw7PR2dVFg=' thing = SecureConfig.from_key(TEST_KEYSTRING, filepath=output_path) print("In our last episode, we created an encrypted serialized dictionary using SecureConfig.") print("Now we're going to use the same key to decrypt and read the data back.") print() print("What's in this file? Use .sections() to see top-level dictionary data.") print(thing.sections()) print() print("So, who is in the family? Use .options('family') and .get('family', member) to see. ") for member in thing.options('family'): print( member + ": " + thing.get('family', member))
def read(self, path, keypath): f = open(keypath, 'r') key = f.read() f.close() self.cfg = SecureConfig.from_key(key, filepath=path)
from __future__ import print_function import os from secureconfig import SecureConfig # here we're going to open the file we encrypted in the previous demo # and make sure we can decrypt and parse it using the same keystring. CWD = os.path.dirname(os.path.realpath(__file__)) output_path = os.path.join(CWD, 'demo_secureconfig_data.enc') TEST_KEYSTRING = 'sFbO-GbipIFIpj64S2_AZBIPBvX80Yozszw7PR2dVFg=' thing = SecureConfig.from_key(TEST_KEYSTRING, filepath=output_path) print( "In our last episode, we created an encrypted serialized dictionary using SecureConfig." ) print("Now we're going to use the same key to decrypt and read the data back.") print() print("What's in this file? Use .sections() to see top-level dictionary data.") print(thing.sections()) print() print( "So, who is in the family? Use .options('family') and .get('family', member) to see. " ) for member in thing.options('family'): print(member + ": " + thing.get('family', member))
from __future__ import print_function import os from secureconfig import SecureConfig CWD = os.path.dirname(os.path.realpath(__file__)) output_path = os.path.join(CWD, 'demo_secureconfig_data.enc') TEST_KEYSTRING = 'sFbO-GbipIFIpj64S2_AZBIPBvX80Yozszw7PR2dVFg=' thing = SecureConfig.from_key(TEST_KEYSTRING) print("Start with an blank slate: ") print(thing.cfg) print() print("Add a section with add_section: ") thing.add_section('family') print(thing.cfg) print() print("Stick some values in it: ") thing.set('family', 'grandma', 'Kay') thing.set('family', 'granddad', 'John') print(thing.cfg) print() print("Now let's write it to disk (%s)" % output_path) fh = open(output_path, 'w') thing.write(fh) fh.close()