def update_signature(account_id, client_signature): # import dependencies import hashlib from labpack.records.settings import save_settings # add signature to records signature_hash = hashlib.sha256(str(client_signature).encode('utf-8')).hexdigest() signature_key = '../data/signatures/%s.%s.yaml' % (account_id, signature_hash) save_settings(signature_key, client_signature, overwrite=True) return True
def update_flightplan(access_token, flightplan_details): from os import listdir, path from hashlib import md5 from labpack.records.settings import save_settings account_dir = listdir('../data/') hash_string = md5(access_token.encode('utf-8')).hexdigest() if hash_string in account_dir: flightplan_path = '../data/%s/flightplan.json' % hash_string save_settings(flightplan_path, flightplan_details) return True return False
def create_state(account_id, oauth2_config): from labpack.records.id import labID from labpack.records.settings import save_settings record_id = labID() state_details = { 'account_id': account_id, 'state_value': record_id.id36, 'dt': record_id.epoch, 'service_scope': oauth2_config['oauth2_service_scope'], 'proxy_provider': '', 'subdomain': '' } state_path = '../data/oauth2/state/%s.yaml' % state_details['state_value'] save_settings(state_path, state_details, overwrite=True) return state_details
def create_account(account_email, account_password): # import dependencies import hashlib from labpack.records.settings import save_settings from labpack.records.id import labID # create account details record_id = labID() account_details = { 'email': account_email, 'salt': record_id.id24, 'id': record_id.id36, 'created': record_id.epoch, 'confirmed': False, 'confirm_token': record_id.id48 } # create password hash salted_password = '******' % (account_details['salt'], str(account_password)) password_hash = hashlib.sha256(salted_password.encode('utf-8')).hexdigest() account_details['password'] = password_hash # save account in records email_hash = hashlib.sha256(account_email.encode('utf-8')).hexdigest() email_key = '../data/accounts/email/%s.yaml' % email_hash save_settings(email_key, account_details, overwrite=True) id_key = '../data/accounts/id/%s.yaml' % account_details['id'] save_settings(id_key, account_details, overwrite=True) token_key = '../data/accounts/token/%s.yaml' % account_details['confirm_token'] save_settings(token_key, account_details, overwrite=True) return account_details
def create_token(token_details, account_id, service_scope, service_name, service_id=''): from labpack.records.settings import save_settings token_fields = { 'access_token': '', 'token_type': '', 'expires_at': 0, 'refresh_token': '', 'account_id': account_id, 'service_name': service_name, 'service_id': service_id, 'service_scope': [] } # update token fields with access token input if 'expires_in' in token_details.keys(): if isinstance(token_details['expires_in'], int): from time import time token_details['expires_at'] = time() + token_details['expires_in'] del token_details['expires_in'] if 'user_id' in token_details.keys(): if isinstance(token_details['user_id'], int) or isinstance(token_details['user_id'], str): token_details['service_id'] = token_details['user_id'] del token_details['user_id'] token_fields.update(**token_details) # add service scope to token if isinstance(service_scope, str): token_fields['service_scope'].extend(service_scope.split(' ')) elif isinstance(service_scope, list): token_fields['service_scope'].extend(service_scope) else: raise ValueError('%s must be a list or space separate string' % str(service_scope)) # create path token_path = '../data/oauth2/token/%s/%s/%s.yaml' % (token_fields['service_name'], token_fields['account_id'], token_fields['expires_at']) save_settings(token_path, token_fields, overwrite=True) return token_fields
def update_account(account_details): # import dependencies import hashlib from os import path from labpack.records.settings import save_settings, remove_settings # validate id in account_details try: account_id = account_details['id'] account_email = account_details['email'] account_password = account_details['password'] except: raise ValueError('update_account input must contain id, email and password fields.') # find current account details by account id current_details = read_account(account_id) if not current_details: raise FileNotFoundError # find files associated with account create_paths = [] remove_paths = [] current_email_hash = '' for key, value in current_details.items(): if key.find('token') > -1: token_path = '../data/accounts/token/%s.yaml' % value if not key in account_details.keys(): remove_paths.append(token_path) else: create_paths.append(token_path) elif key == 'email': current_email_hash = hashlib.sha256(value.encode('utf-8')).hexdigest() # save new id file id_path = '../data/accounts/id/%s.yaml' % account_id save_settings(id_path, account_details, overwrite=True) # save new email hash (and remove old ones) email_hash = hashlib.sha256(account_email.encode('utf-8')).hexdigest() email_path = '../data/accounts/email/%s.yaml' % email_hash save_settings(email_path, account_details, overwrite=True) if email_hash != current_email_hash: current_email_path = '../data/accounts/email/%s.yaml' % current_email_hash remove_paths.append(current_email_path) # determine token files to create for key, value in account_details.items(): if key.find('token') > -1: if not key in current_details.keys(): token_path = '../data/accounts/token/%s.yaml' % value create_paths.append(token_path) # save token files to update and create for file_path in create_paths: save_settings(file_path, account_details, overwrite=True) # remove token files for file_path in remove_paths: if path.exists(file_path): remove_settings(file_path) return account_details
photo_id = 'AgADAQADsKcxG4RH3Q85DF_-VgGr___A5y8ABVzwsrRBb8xF-wEAAQI' photo_path = '../../data/test_photo.png' file_path = '../../data/test_voice.ogg' update_path = '../../data/telegram-update.json' update_id = load_settings(update_path)['last_update'] bot_id = telegram_config['telegram_bot_id'] access_token = telegram_config['telegram_access_token'] user_id = telegram_config['telegram_admin_id'] telegram_bot = telegramBotClient(bot_id, access_token, requests_handler=handle_requests) details = telegram_bot.get_me() assert details['json']['result']['id'] == bot_id updates_details = telegram_bot.get_updates() if updates_details['json']['result']: update_list = sorted(updates_details['json']['result'], key=lambda k: k['update_id']) offset_details = { 'last_update': update_list[-1]['update_id']} save_settings(offset_details, update_path, overwrite=True) # details = telegram_bot.send_message(user_id, 'text me again') # details = telegram_bot.send_photo(user_id, photo_url=photo_url, caption_text='Lab Logo') # details = telegram_bot.send_photo(user_id, photo_id=photo_id) # details = telegram_bot.send_photo(user_id, photo_path=photo_path) # details = telegram_bot.send_message(user_id, '*Select a Number:*\n\t_1_\n\t\t`2`\n\t\t\t[3](http://collectiveacuity.com)', message_style='markdown') # details = telegram_bot.send_message(user_id, 'Select a Number:', button_list=['1','2','3']) # details = telegram_bot.send_message(user_id, 'Select a Letter:', button_list=['ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF'], small_buttons=False, persist_buttons=True) file_id = 'AwADAQADAwADXGbcCxP7_eEhVMEeAg' details = telegram_bot.get_route(file_id) file_route = details['json']['result']['file_path'] file_buffer = telegram_bot.get_file(file_route, file_name='test_voice') file_data = file_buffer.getvalue() file_name = file_buffer.name from labpack.parsing.magic import labMagic lab_magic = labMagic('../../data/magic.mgc')