OLD | NEW |
(Empty) | |
| 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 |
| 14 import logging |
| 15 import optparse |
| 16 import sys |
| 17 |
| 18 from third_party import colorama |
| 19 |
| 20 import auth |
| 21 import subcommand |
| 22 |
| 23 __version__ = '1.0' |
| 24 |
| 25 |
| 26 @subcommand.usage('<hostname>') |
| 27 def CMDlogin(parser, args): |
| 28 """Performs interactive login and caches authentication token.""" |
| 29 # Forcefully relogin, revoking previous token. |
| 30 hostname, authenticator = parser.parse_args(args) |
| 31 authenticator.logout() |
| 32 authenticator.login() |
| 33 print_token_info(hostname, authenticator) |
| 34 return 0 |
| 35 |
| 36 |
| 37 @subcommand.usage('<hostname>') |
| 38 def CMDlogout(parser, args): |
| 39 """Revokes cached authentication token and removes it from disk.""" |
| 40 _, authenticator = parser.parse_args(args) |
| 41 done = authenticator.logout() |
| 42 print 'Done.' if done else 'Already logged out.' |
| 43 return 0 |
| 44 |
| 45 |
| 46 @subcommand.usage('<hostname>') |
| 47 def CMDinfo(parser, args): |
| 48 """Shows email associated with a cached authentication token.""" |
| 49 # If no token is cached, AuthenticationError will be caught in 'main'. |
| 50 hostname, authenticator = parser.parse_args(args) |
| 51 print_token_info(hostname, authenticator) |
| 52 return 0 |
| 53 |
| 54 |
| 55 def print_token_info(hostname, authenticator): |
| 56 token_info = authenticator.get_token_info() |
| 57 print 'Logged in to %s as %s.' % (hostname, token_info['email']) |
| 58 print '' |
| 59 print 'To login with a different email run:' |
| 60 print ' depot-tools-auth login %s' % hostname |
| 61 print 'To logout and purge the authentication token run:' |
| 62 print ' depot-tools-auth logout %s' % hostname |
| 63 |
| 64 |
| 65 class OptionParser(optparse.OptionParser): |
| 66 def __init__(self, *args, **kwargs): |
| 67 optparse.OptionParser.__init__( |
| 68 self, *args, prog='depot-tools-auth', version=__version__, **kwargs) |
| 69 self.add_option( |
| 70 '-v', '--verbose', action='count', default=0, |
| 71 help='Use 2 times for more debugging info') |
| 72 auth.add_auth_options(self, auth.make_auth_config(use_oauth2=True)) |
| 73 |
| 74 def parse_args(self, args=None, values=None): |
| 75 """Parses options and returns (hostname, auth.Authenticator object).""" |
| 76 options, args = optparse.OptionParser.parse_args(self, args, values) |
| 77 levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 78 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 79 auth_config = auth.extract_auth_config_from_options(options) |
| 80 if len(args) != 1: |
| 81 self.error('Expecting single argument (hostname).') |
| 82 if not auth_config.use_oauth2: |
| 83 self.error('This command is only usable with OAuth2 authentication') |
| 84 return args[0], auth.get_authenticator_for_host(args[0], auth_config) |
| 85 |
| 86 |
| 87 def main(argv): |
| 88 dispatcher = subcommand.CommandDispatcher(__name__) |
| 89 try: |
| 90 return dispatcher.execute(OptionParser(), argv) |
| 91 except auth.AuthenticationError as e: |
| 92 print >> sys.stderr, e |
| 93 return 1 |
| 94 |
| 95 |
| 96 if __name__ == '__main__': |
| 97 colorama.init() |
| 98 try: |
| 99 sys.exit(main(sys.argv[1:])) |
| 100 except KeyboardInterrupt: |
| 101 sys.stderr.write('interrupted\n') |
| 102 sys.exit(1) |
OLD | NEW |