From 432de9fc3c1878ce4c2f6c6a9d2f3c9873a796d1 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Tue, 13 Feb 2018 00:22:13 -0500 Subject: [PATCH 1/5] Add the ability to use the browser auth flow --- fediplay.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/fediplay.py b/fediplay.py index 24bbac1..7359235 100644 --- a/fediplay.py +++ b/fediplay.py @@ -79,6 +79,12 @@ def login(api_base_url, email, password): client.log_in(email, password, to_file='usercred.secret') umask(old_umask) +def login_with_code(api_base_url, grant_code): + client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) + old_umask = umask(0o77) + client.log_in(code=grant_code, to_file='usercred.secret') + umask(old_umask) + def stream(api_base_url): client = Mastodon(client_id='clientcred.secret', access_token='usercred.secret', api_base_url=api_base_url) listener = StreamListener() @@ -111,7 +117,7 @@ def build_play_command(filename): def main(): from getpass import getpass from os import path - from sys import exit + from sys import exit, argv api_base_url = environ.get('FEDIPLAY_API_BASE_URL') if not api_base_url: @@ -124,9 +130,16 @@ def main(): if not path.exists('usercred.secret'): print('==> No usercred.secret; logging in') - email = input('Email: ') - password = getpass('Password: ') - login(api_base_url, email, password) + if '-b' in argv or '--browser' in argv: + client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) + print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") + print(client.auth_request_url()) + grant_code = getpass('Code? ') + login_with_code(api_base_url, grant_code) + else: + email = input('Email: ') + password = getpass('Password: ') + login(api_base_url, email, password) stream(api_base_url) From a2a11584d4a3a35cc985ad15720290b5b3b4207b Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Fri, 16 Feb 2018 23:31:29 -0500 Subject: [PATCH 2/5] Make the browser auth flow the only flow, per @zigg --- fediplay.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/fediplay.py b/fediplay.py index 7359235..400264c 100644 --- a/fediplay.py +++ b/fediplay.py @@ -73,13 +73,7 @@ def register(api_base_url): Mastodon.create_app('fediplay', api_base_url=api_base_url, to_file='clientcred.secret') umask(old_umask) -def login(api_base_url, email, password): - client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) - old_umask = umask(0o77) - client.log_in(email, password, to_file='usercred.secret') - umask(old_umask) - -def login_with_code(api_base_url, grant_code): +def login(api_base_url, grant_code): client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) old_umask = umask(0o77) client.log_in(code=grant_code, to_file='usercred.secret') @@ -130,16 +124,11 @@ def main(): if not path.exists('usercred.secret'): print('==> No usercred.secret; logging in') - if '-b' in argv or '--browser' in argv: - client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) - print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") - print(client.auth_request_url()) - grant_code = getpass('Code? ') - login_with_code(api_base_url, grant_code) - else: - email = input('Email: ') - password = getpass('Password: ') - login(api_base_url, email, password) + client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) + print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") + print(client.auth_request_url()) + grant_code = getpass('Code? ') + login(api_base_url, grant_code) stream(api_base_url) From 7f88d23d0a4742f383a25cf1213b1aa8450398a3 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Fri, 16 Feb 2018 23:32:25 -0500 Subject: [PATCH 3/5] Switch from `getpass` -> `input` for grant token --- fediplay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fediplay.py b/fediplay.py index 400264c..ccab9fd 100644 --- a/fediplay.py +++ b/fediplay.py @@ -127,7 +127,7 @@ def main(): client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") print(client.auth_request_url()) - grant_code = getpass('Code? ') + grant_code = input('Code? ') login(api_base_url, grant_code) stream(api_base_url) From 96096d7b003af74d9c0fcd641ec5c228db2202dd Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Fri, 16 Feb 2018 23:34:21 -0500 Subject: [PATCH 4/5] no need for `sys.argv` anymore --- fediplay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fediplay.py b/fediplay.py index ccab9fd..c9eefa4 100644 --- a/fediplay.py +++ b/fediplay.py @@ -111,7 +111,7 @@ def build_play_command(filename): def main(): from getpass import getpass from os import path - from sys import exit, argv + from sys import exit api_base_url = environ.get('FEDIPLAY_API_BASE_URL') if not api_base_url: From 22031dbcb54ae0ba814ad575b39c5baac3509e75 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Fri, 16 Feb 2018 23:36:54 -0500 Subject: [PATCH 5/5] No need to construct the `client` twice when logging in --- fediplay.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fediplay.py b/fediplay.py index c9eefa4..a2c8df8 100644 --- a/fediplay.py +++ b/fediplay.py @@ -73,8 +73,7 @@ def register(api_base_url): Mastodon.create_app('fediplay', api_base_url=api_base_url, to_file='clientcred.secret') umask(old_umask) -def login(api_base_url, grant_code): - client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url) +def login(client, grant_code): old_umask = umask(0o77) client.log_in(code=grant_code, to_file='usercred.secret') umask(old_umask) @@ -128,7 +127,7 @@ def main(): print("Open this page in your browser and follow the instructions to get the code you need, then paste it here") print(client.auth_request_url()) grant_code = input('Code? ') - login(api_base_url, grant_code) + login(client, grant_code) stream(api_base_url)