OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 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 """Tool that manages OAuth2 token cache used by other depot_tools scripts. | |
M-A Ruel
2015/04/09 20:00:40
"""Manages ...
Vadim Sh.
2015/04/10 01:18:42
Done.
| |
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 """Perform interactive login and caches authentication token.""" | |
M-A Ruel
2015/04/09 20:00:40
Performs
Vadim Sh.
2015/04/10 01:18:42
Done.
| |
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 | |
M-A Ruel
2015/04/09 20:00:40
I'd prefer if you return 0 explicitly everywhere.
Vadim Sh.
2015/04/10 01:18:43
Done.
| |
35 | |
36 @subcommand.usage('<hostname>') | |
37 def CMDlogout(parser, args): | |
38 """Revokes cached authentication token and removes it from disk.""" | |
39 _, authenticator = parser.parse_args(args) | |
40 done = authenticator.logout() | |
41 print 'Done.' if done else 'Already logged out.' | |
42 | |
43 | |
44 @subcommand.usage('<hostname>') | |
45 def CMDinfo(parser, args): | |
46 """Shows email associated with a cached authentication token.""" | |
47 # If no token is cached, AuthenticationError will be caught in 'main'. | |
48 hostname, authenticator = parser.parse_args(args) | |
49 print_token_info(hostname, authenticator) | |
50 | |
51 | |
52 def print_token_info(hostname, authenticator): | |
53 token_info = authenticator.get_token_info() | |
54 print 'Logged in to %s as %s.' % (hostname, token_info['email']) | |
55 print '' | |
56 print 'To login with a different email run:' | |
57 print ' depot-tools-auth login %s' % hostname | |
58 print 'To logout and purge the authentication token run:' | |
59 print ' depot-tools-auth logout %s' % hostname | |
60 | |
61 | |
62 class OptionParser(optparse.OptionParser): | |
63 def __init__(self, *args, **kwargs): | |
64 optparse.OptionParser.__init__( | |
65 self, *args, prog='depot-tools-auth', version=__version__, **kwargs) | |
66 self.add_option( | |
67 '-v', '--verbose', action='count', default=0, | |
68 help='Use 2 times for more debugging info') | |
69 auth.add_auth_options(self, auth.make_auth_config(use_oauth2=True)) | |
70 | |
71 def parse_args(self, args=None, values=None): | |
72 """Parses options and returns (hostname, auth.Authenticator object).""" | |
73 options, args = optparse.OptionParser.parse_args(self, args, values) | |
74 levels = [logging.WARNING, logging.INFO, logging.DEBUG] | |
75 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) | |
76 auth_config = auth.extract_auth_config_from_options(options) | |
77 if len(args) != 1: | |
78 self.error('Expecting single argument (hostname).') | |
79 if not auth_config.use_oauth2: | |
80 self.error('This command is only usable with OAuth2 authentication') | |
81 return args[0], auth.Authenticator( | |
82 token_cache_key=auth.hostname_to_token_cache_key(args[0]), | |
83 config=auth_config) | |
84 | |
85 | |
86 def main(argv): | |
87 dispatcher = subcommand.CommandDispatcher(__name__) | |
88 try: | |
89 return dispatcher.execute(OptionParser(), argv) | |
90 except auth.AuthenticationError as e: | |
91 print >> sys.stderr, e | |
92 return 1 | |
93 | |
94 | |
95 if __name__ == '__main__': | |
96 colorama.init() | |
97 try: | |
98 sys.exit(main(sys.argv[1:])) | |
99 except KeyboardInterrupt: | |
100 sys.stderr.write('interrupted\n') | |
101 sys.exit(1) | |
OLD | NEW |