vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Manages cached OAuth2 tokens used by other depot_tools scripts. |
| 7 | |
| 8 | Usage: |
| 9 | depot-tools-auth login codereview.chromium.org |
| 10 | depot-tools-auth info codereview.chromium.org |
| 11 | depot-tools-auth logout codereview.chromium.org |
| 12 | """ |
| 13 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 16 | import logging |
| 17 | import optparse |
| 18 | import sys |
iannucci@chromium.org | 0703ea2 | 2016-04-01 01:02:42 +0000 | [diff] [blame] | 19 | import os |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 20 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 21 | import auth |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 22 | import setup_color |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 23 | import subcommand |
| 24 | |
| 25 | __version__ = '1.0' |
| 26 | |
| 27 | |
| 28 | @subcommand.usage('<hostname>') |
| 29 | def CMDlogin(parser, args): |
| 30 | """Performs interactive login and caches authentication token.""" |
| 31 | # Forcefully relogin, revoking previous token. |
| 32 | hostname, authenticator = parser.parse_args(args) |
| 33 | authenticator.logout() |
| 34 | authenticator.login() |
| 35 | print_token_info(hostname, authenticator) |
| 36 | return 0 |
| 37 | |
| 38 | |
| 39 | @subcommand.usage('<hostname>') |
| 40 | def CMDlogout(parser, args): |
| 41 | """Revokes cached authentication token and removes it from disk.""" |
| 42 | _, authenticator = parser.parse_args(args) |
| 43 | done = authenticator.logout() |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 44 | print('Done.' if done else 'Already logged out.') |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 45 | return 0 |
| 46 | |
| 47 | |
| 48 | @subcommand.usage('<hostname>') |
| 49 | def CMDinfo(parser, args): |
| 50 | """Shows email associated with a cached authentication token.""" |
| 51 | # If no token is cached, AuthenticationError will be caught in 'main'. |
| 52 | hostname, authenticator = parser.parse_args(args) |
| 53 | print_token_info(hostname, authenticator) |
| 54 | return 0 |
| 55 | |
| 56 | |
| 57 | def print_token_info(hostname, authenticator): |
| 58 | token_info = authenticator.get_token_info() |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 59 | print('Logged in to %s as %s.' % (hostname, token_info['email'])) |
| 60 | print('') |
| 61 | print('To login with a different email run:') |
| 62 | print(' depot-tools-auth login %s' % hostname) |
| 63 | print('To logout and purge the authentication token run:') |
| 64 | print(' depot-tools-auth logout %s' % hostname) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | class OptionParser(optparse.OptionParser): |
| 68 | def __init__(self, *args, **kwargs): |
| 69 | optparse.OptionParser.__init__( |
| 70 | self, *args, prog='depot-tools-auth', version=__version__, **kwargs) |
| 71 | self.add_option( |
| 72 | '-v', '--verbose', action='count', default=0, |
| 73 | help='Use 2 times for more debugging info') |
| 74 | auth.add_auth_options(self, auth.make_auth_config(use_oauth2=True)) |
| 75 | |
| 76 | def parse_args(self, args=None, values=None): |
| 77 | """Parses options and returns (hostname, auth.Authenticator object).""" |
| 78 | options, args = optparse.OptionParser.parse_args(self, args, values) |
| 79 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 80 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 81 | auth_config = auth.extract_auth_config_from_options(options) |
| 82 | if len(args) != 1: |
| 83 | self.error('Expecting single argument (hostname).') |
| 84 | if not auth_config.use_oauth2: |
| 85 | self.error('This command is only usable with OAuth2 authentication') |
| 86 | return args[0], auth.get_authenticator_for_host(args[0], auth_config) |
| 87 | |
| 88 | |
| 89 | def main(argv): |
| 90 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 91 | try: |
| 92 | return dispatcher.execute(OptionParser(), argv) |
| 93 | except auth.AuthenticationError as e: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 94 | print(e, file=sys.stderr) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 95 | return 1 |
| 96 | |
| 97 | |
| 98 | if __name__ == '__main__': |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 99 | setup_color.init() |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 100 | try: |
| 101 | sys.exit(main(sys.argv[1:])) |
| 102 | except KeyboardInterrupt: |
| 103 | sys.stderr.write('interrupted\n') |
| 104 | sys.exit(1) |