chiark / gitweb /
Handle Mastodon 2FA (sort of)
authorColin Watson <cjwatson@debian.org>
Tue, 1 Oct 2024 13:20:28 +0000 (14:20 +0100)
committerColin Watson <cjwatson@debian.org>
Tue, 1 Oct 2024 13:20:28 +0000 (14:20 +0100)
fediverse/fediverse.py

index 7c28f6dceacc730837a68ed17143c9a3553739a5..768e96ea63bd113aa57a14def2bc2ad809f77771 100644 (file)
@@ -40,9 +40,11 @@ def post_on_mastodon(settings, new_posts):
    mt_username = settings.get('MASTODON_USERNAME', '')
    global mt_password
    mt_password = settings.get('MASTODON_PASSWORD', '')
+   global mt_code
+   mt_code = settings.get('MASTODON_CODE', '')
 
    # check if config file has been duly filled or print an error message and exit
-   if mt_base_url == '' or mt_username == '' or mt_password == '':
+   if mt_base_url == '' or ((mt_username == '' or mt_password == '') and mt_code == ''):
       logger.warning('Pelican_fediverse: Mastodon access credentials not configured...')
       sys.exit(9)
 
@@ -78,15 +80,22 @@ def post_updates(generator, writer):
    # we only write the newly found sites to disk if posting them worked. that way we can retry later
    if new_posts:
       if post_on_mastodon(generator.settings, new_posts):
-         mastodon = Mastodon(
-            client_id = 'pelicanfediverse_clientcred.secret',
-            api_base_url = mt_base_url
-         )
-         mastodon.log_in(
-            mt_username,
-            mt_password,
-            to_file = 'pelicanfediverse_usercred.secret'
-         )
+         if not os.path.exists('pelicanfediverse_usercred.secret'):
+            mastodon = Mastodon(
+               client_id = 'pelicanfediverse_clientcred.secret',
+               api_base_url = mt_base_url
+            )
+            if mt_code:
+               mastodon.log_in(
+                  code=mt_code,
+                  to_file = 'pelicanfediverse_usercred.secret'
+               )
+            else:
+               mastodon.log_in(
+                  mt_username,
+                  mt_password,
+                  to_file = 'pelicanfediverse_usercred.secret'
+               )
          mastodon = Mastodon(
             access_token = 'pelicanfediverse_usercred.secret',
             api_base_url = mt_base_url