Browse Source

Add proper TOTP support

bot_curator 20170706.1
KemoNine 4 years ago
parent
commit
3eeaad02b5
  1. 50
      toot_bot.py

50
toot_bot.py

@ -5,7 +5,7 @@ import argparse, sys, os, configparser, getpass, sqlite3, codecs
from mastodon import Mastodon
import feedparser
def init(api_base_url, app_name, client_cred_file, user_cred_file):
def init(api_base_url, app_name, client_cred_file, user_cred_file, totp):
# Prompt user to find out if they want to continue if the client_cred_file exists already -- shouldn't happen twice per docs
if (os.path.exists(client_cred_file)):
sys.exit('init should only ever be called once, try login instead, if that fails. delete ' + client_cred_file + ' and re-run init')
@ -14,17 +14,17 @@ def init(api_base_url, app_name, client_cred_file, user_cred_file):
Mastodon.create_app(
app_name,
api_base_url = api_base_url,
scopes=['read', 'write'],
scopes=['read', 'write', 'follow'],
to_file = client_cred_file
)
# Login to seed login credentials
login(api_base_url, client_cred_file, user_cred_file)
login(api_base_url, client_cred_file, user_cred_file, totp)
def login(api_base_url, client_cred_file, user_cred_file):
def login(api_base_url, client_cred_file, user_cred_file, totp):
# Prompt for user/password (NEVER store them on disk!)
email = input('what is the e-mail that was used to setup the bot account on ' + api_base_url + '?')
password = getpass.getpass('what is the password for the account?')
email = input('what is the e-mail that was used to setup the bot account on ' + api_base_url + '? ')
password = getpass.getpass('what is the password for the account? ')
# Setup Mastodon API
mastodon = Mastodon(
@ -32,13 +32,27 @@ def login(api_base_url, client_cred_file, user_cred_file):
api_base_url = api_base_url
)
totp_code = None
if totp:
print('TOTP enabled, open the link (below) in a browser to authorize access')
print(mastodon.auth_request_url())
print()
totp_code = input("enter the code you received from the url: ")
# Login and cache credential
mastodon.log_in(
email,
password,
scopes=['read', 'write'],
to_file = user_cred_file
)
if totp_code is None:
mastodon.log_in(
email,
password,
scopes=['read', 'write', 'follow'],
to_file = user_cred_file
)
else:
mastodon.log_in(
code = totp_code,
scopes=['read', 'write', 'follow'],
to_file = user_cred_file
)
def toot(api_base_url, client_cred_file, user_cred_file, cw_text, toot, subtoots):
# Setup Mastodon API
@ -113,7 +127,11 @@ if __name__ == '__main__':
# Actions / commands
init_parser = subparsers.add_parser('init', help='initialize credentials')
init_parser.add_argument('--totp', help='use totp login (requires user to open URL in browser and then enter token to bot during init/login)',
action='store_true')
login_parser = subparsers.add_parser('login', help='login to instance if credentials have expired')
login_parser.add_argument('--totp', help='use totp login (requires user to open URL in browser and then enter token to bot during init/login)',
action='store_true')
toot_parser = subparsers.add_parser('toot', help='send configured toot')
rss_parser = subparsers.add_parser('rss', help='cross post articles from an rss feed')
@ -138,7 +156,9 @@ if __name__ == '__main__':
config_api_base_url = config['config']['api_base_url']
config_client_cred_file = config['config']['client_cred_file']
config_user_cred_file = config['config']['user_cred_file']
config_totp = False
if 'totp' in args:
config_totp = args.totp
# Ensure client_cred_file path is valid
config_client_cred_file = os.path.abspath(config_client_cred_file)
if not os.path.exists(os.path.split(config_client_cred_file)[0]):
@ -161,10 +181,10 @@ if __name__ == '__main__':
# Deal with init command
if args.command == 'init':
init(config_api_base_url, config_app_name, config_client_cred_file, config_user_cred_file)
init(config_api_base_url, config_app_name, config_client_cred_file, config_user_cred_file, config_totp)
# Deal with login command
if args.command == 'login':
login(config_api_base_url, config_client_cred_file, config_user_cred_file)
login(config_api_base_url, config_client_cred_file, config_user_cred_file, config_totp)
# Deal with toot command
if args.command == 'toot':
# Setup main toot text (applies to RSS as well -- is prefix for RSS toots

Loading…
Cancel
Save