diff --git a/bin/pwnagotchi b/bin/pwnagotchi index 2dd34b3a..bc0cc1f0 100755 --- a/bin/pwnagotchi +++ b/bin/pwnagotchi @@ -10,6 +10,7 @@ import os import pwnagotchi from pwnagotchi import utils +from pwnagotchi.google import cmd as google_cmd from pwnagotchi.plugins import cmd as plugins_cmd from pwnagotchi import log from pwnagotchi import restart @@ -98,7 +99,7 @@ def do_auto_mode(agent): if __name__ == '__main__': parser = argparse.ArgumentParser() parser = plugins_cmd.add_parsers(parser) - parser = google_cmd.add_parsers(parser) + parser2 = google_cmd.add_parsers(parser) parser.add_argument('-C', '--config', action='store', dest='config', default='/etc/pwnagotchi/default.toml', help='Main configuration file.') @@ -134,6 +135,9 @@ if __name__ == '__main__': log.setup_logging(args, config) rc = plugins_cmd.handle_cmd(args, config) sys.exit(rc) + elif google_cmd.used_google_cmd(args): + rc = google_cmd.handle_cmd(args) + sys.exit(rc) if args.version: print(pwnagotchi.__version__) diff --git a/builder/data/root/settings.yaml b/builder/data/root/settings.yaml new file mode 100644 index 00000000..e69de29b diff --git a/pwnagotchi/google/cmd.py b/pwnagotchi/google/cmd.py index 0ddb6137..040c7ad0 100644 --- a/pwnagotchi/google/cmd.py +++ b/pwnagotchi/google/cmd.py @@ -1,13 +1,8 @@ # Handles the commandline stuff -import os +import pydrive2 +from pydrive2.auth import GoogleAuth import logging -import glob -import re -import shutil -from fnmatch import fnmatch -from pwnagotchi.utils import download_file, unzip, save_config, parse_version, md5 -from pwnagotchi.plugins import default_path def add_parsers(parser): @@ -16,45 +11,83 @@ def add_parsers(parser): """ subparsers = parser.add_subparsers() # pwnagotchi google - parser_plugins = subparsers.add_parser('google') - plugin_subparsers = parser_plugins.add_subparsers(dest='googlecmd') + parser_google = subparsers.add_parser('google') + google_subparsers = parser_google.add_subparsers(dest='googlecmd') # pwnagotchi plugins search - parser_plugins_search = plugin_subparsers.add_parser('search', help='Search for pwnagotchi plugins') - parser_plugins_search.add_argument('pattern', type=str, help="Search expression (wildcards allowed)") + parser_google_auth = google_subparsers.add_parser('auth', help='Google Authentication') + parser_google_auth.add_argument('true', type=str, help="This will start the authentication process") return parser -def used_plugin_cmd(args): +def used_google_cmd(args): """ Checks if the plugins subcommand was used """ - return hasattr(args, 'plugincmd') + return hasattr(args, 'googlecmd') -def handle_cmd(args, config): +def handle_cmd(args): """ Parses the arguments and does the thing the user wants """ - if args.plugincmd == 'update': - return update(config) - elif args.plugincmd == 'search': - args.installed = True # also search in installed plugins - return list_plugins(args, config, args.pattern) - elif args.plugincmd == 'install': - return install(args, config) - elif args.plugincmd == 'uninstall': - return uninstall(args, config) - elif args.plugincmd == 'list': - return list_plugins(args, config) - elif args.plugincmd == 'enable': - return enable(args, config) - elif args.plugincmd == 'disable': - return disable(args, config) - elif args.plugincmd == 'upgrade': - return upgrade(args, config, args.pattern) - elif args.plugincmd == 'edit': - return edit(args, config) + if args.plugincmd == 'auth': + return auth(args) + elif args.plugincmd == 'refresh': + return refresh(args) + raise NotImplementedError() - raise NotImplementedError() \ No newline at end of file + +def auth(args): + if args == "true": + # start authentication process + user_input = input("By completing these steps you give pwnagotchi access to your personal Google Drive!\n" + "Personal credentials will be stored only locally for automated verification in the future.\n" + "No one else but you have access to these.\n" + "Do you agree? \n\n[y(es)/n(o)]") + if user_input.lower() in ('y', 'yes'): + try: + gauth = GoogleAuth(settings_file="settings.yaml") + print(gauth.GetAuthUrl()) + user_input = input("Please copy this URL into a browser, " + "complete the verification and then copy/paste the code from addressbar.") + gauth.Auth(user_input) + gauth.SaveCredentialsFile("credentials.json") + except Exception as e: + logging.error(f"Error: {e}") + return + + +def refresh(args): + if int(args): + # refresh token for x amount of time (seconds) + gauth = GoogleAuth(settings_file="settings.yaml") + try: + # Try to load saved client credentials + gauth.LoadCredentialsFile("credentials.json") + except pydrive2.auth.InvalidCredentialsError: + print(gauth.GetAuthUrl()) + user_input = input("Please copy this URL into a browser, " + "complete the verification and then copy/paste the code from addressbar.") + gauth.Auth(user_input) + + if gauth.access_token_expired: + if gauth.credentials is not None: + try: + # Refresh the token + gauth.Refresh() + except pydrive2.auth.RefreshError: + print(gauth.GetAuthUrl()) + user_input = input("Please copy this URL into a browser, " + "complete the verification and then copy/paste the code from addressbar.") + gauth.Auth(user_input) + else: + print(gauth.GetAuthUrl()) + user_input = input("Please copy this URL into a browser, " + "complete the verification and then copy/paste the code from addressbar.") + gauth.Auth(user_input) + gauth.Authorize() + gauth.SaveCredentialsFile("credentials.json") + print("No refresh is required.") + return diff --git a/pwnagotchi/plugins/cmd.py b/pwnagotchi/plugins/cmd.py index b0b9bf9e..ee752010 100644 --- a/pwnagotchi/plugins/cmd.py +++ b/pwnagotchi/plugins/cmd.py @@ -75,7 +75,7 @@ def handle_cmd(args, config): if args.plugincmd == 'update': return update(config) elif args.plugincmd == 'search': - args.installed = True # also search in installed plugins + args.installed = True # also search in installed plugins return list_plugins(args, config, args.pattern) elif args.plugincmd == 'install': return install(args, config) diff --git a/pwnagotchi/plugins/default/gdrivesync.py b/pwnagotchi/plugins/default/gdrivesync.py index 5b376fb6..493c972d 100644 --- a/pwnagotchi/plugins/default/gdrivesync.py +++ b/pwnagotchi/plugins/default/gdrivesync.py @@ -51,8 +51,7 @@ class GdriveSync(plugins.Plugin): self.backup = False try: - gauth = GoogleAuth() - + gauth = GoogleAuth(settings_file="settings.yaml") gauth.Authorize() # Create GoogleDrive instance diff --git a/requirements.txt b/requirements.txt index 32e9a958..e30135e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ torch==2.0.1 torchvision==0.15.2 stable_baselines3 RPi.GPIO +pydrive2 \ No newline at end of file