def _find_account(app, user, account_name): """For a given account name, returns the Account object. Raises an exception if not found. """ if not account_name: raise ConsoleError("Empty account name given") accounts = api.search_accounts(app, user, account_name) if account_name[0] == "@": account_name = account_name[1:] for account in accounts: if account['acct'] == account_name: return account raise ConsoleError("Account not found")
def login_interactive(app): print("\nLog in to " + green(app.instance)) email = input('Email: ') password = getpass('Password: '******'access_token']) path = config.save_user(user) print("Access token saved to: " + green(path)) return user
def _find_account(app, user, account_name): """For a given account name, returns the Account object or raises an exception if not found.""" response = api.search(app, user, account_name, False) for account in response['accounts']: if account['acct'] == account_name or "@" + account[ 'acct'] == account_name: return account raise ConsoleError("Account not found")
def two_factor_login_interactive(app): """Hacky implementation of two factor authentication""" print_out("Log in to {}".format(app.instance)) email = input('Email: ') password = getpass('Password: '******'/auth/sign_in' session = requests.Session() # Fetch sign in form response = session.get(sign_in_url) response.raise_for_status() soup = BeautifulSoup(response.content, "html.parser") form = soup.find('form') inputs = form.find_all('input') data = {i.attrs.get('name'): i.attrs.get('value') for i in inputs} data['user[email]'] = email data['user[password]'] = password # Submit form, get 2FA entry form response = session.post(sign_in_url, data) response.raise_for_status() soup = BeautifulSoup(response.content, "html.parser") form = soup.find('form') inputs = form.find_all('input') data = {i.attrs.get('name'): i.attrs.get('value') for i in inputs} data['user[otp_attempt]'] = input("2FA Token: ") # Submit token response = session.post(sign_in_url, data) response.raise_for_status() # Extract access token from response soup = BeautifulSoup(response.content, "html.parser") initial_state = soup.find('script', id='initial-state') if not initial_state: raise ConsoleError("Login failed: Invalid 2FA token?") data = json.loads(initial_state.get_text()) access_token = data['meta']['access_token'] user = User(app.instance, email, access_token) path = config.save_user(user) print_out("Access token saved to: <green>{}</green>".format(path))
def register_app(instance): print("Registering application with %s" % green(instance)) try: response = api.create_app(instance) except: raise ConsoleError( "Registration failed. Did you enter a valid instance?") base_url = 'https://' + instance app = App(instance, base_url, response['client_id'], response['client_secret']) path = config.save_app(app) print("Application tokens saved to: {}\n".format(green(path))) return app
def login_interactive(app, email=None): print_out("Log in to <green>{}</green>".format(app.instance)) if email: print_out("Email: <green>{}</green>".format(email)) while not email: email = input('Email: ') password = getpass('Password: '******'access_token'])
# -*- coding: utf-8 -*- from __future__ import unicode_literals from __future__ import print_function import webbrowser from textwrap import wrap from toot import ConsoleError from toot.utils import format_content # Attempt to load curses, which is not available on windows try: import curses except ImportError as e: raise ConsoleError("Curses is not available on this platform") class Color: @staticmethod def setup_palette(): curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) @staticmethod def blue(): return curses.color_pair(1) @staticmethod def green():