Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013 The Swarming Authors. All rights reserved. |
| 3 | # Use of this source code is governed under the Apache License, Version 2.0 that |
| 4 | # can be found in the LICENSE file. |
| 5 | |
| 6 | """Client tool to perform various authentication related tasks.""" |
| 7 | |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 8 | __version__ = '0.3.1' |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 9 | |
| 10 | import optparse |
| 11 | import sys |
| 12 | |
| 13 | from third_party import colorama |
| 14 | from third_party.depot_tools import fix_encoding |
| 15 | from third_party.depot_tools import subcommand |
| 16 | |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 17 | from utils import on_error |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 18 | from utils import net |
| 19 | from utils import oauth |
| 20 | from utils import tools |
| 21 | |
| 22 | |
| 23 | def add_auth_options(parser): |
| 24 | """Adds command line options related to authentication.""" |
| 25 | parser.auth_group = optparse.OptionGroup(parser, 'Authentication') |
| 26 | parser.auth_group.add_option( |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 27 | '--auth-method', |
| 28 | metavar='METHOD', |
Vadim Shtayura | 33414bf | 2014-02-27 11:19:34 -0800 | [diff] [blame] | 29 | default=net.get_default_auth_config()[0], |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 30 | help='Authentication method to use: %s. [default: %%default]' % |
Vadim Shtayura | 33414bf | 2014-02-27 11:19:34 -0800 | [diff] [blame] | 31 | ', '.join(name for name, _ in net.AUTH_METHODS)) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 32 | parser.add_option_group(parser.auth_group) |
| 33 | oauth.add_oauth_options(parser) |
| 34 | |
| 35 | |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 36 | def process_auth_options(parser, options): |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 37 | """Configures process-wide authentication parameters based on |options|.""" |
Vadim Shtayura | 33414bf | 2014-02-27 11:19:34 -0800 | [diff] [blame] | 38 | # Validate that authentication method is known. |
| 39 | if options.auth_method not in dict(net.AUTH_METHODS): |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 40 | parser.error('Invalid --auth-method value: %s' % options.auth_method) |
Vadim Shtayura | 33414bf | 2014-02-27 11:19:34 -0800 | [diff] [blame] | 41 | |
| 42 | # Process the rest of the flags based on actual method used. |
| 43 | # Only oauth is configurable now. |
| 44 | config = None |
| 45 | if options.auth_method == 'oauth': |
| 46 | config = oauth.extract_oauth_config_from_options(options) |
| 47 | |
| 48 | # Now configure 'net' globally to use this for every request. |
| 49 | net.configure_auth(options.auth_method, config) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class AuthServiceError(Exception): |
| 53 | """Unexpected response from authentication service.""" |
| 54 | |
| 55 | |
| 56 | class AuthService(object): |
| 57 | """Represents remote Authentication service.""" |
| 58 | |
| 59 | def __init__(self, url): |
| 60 | self._service = net.get_http_service(url) |
| 61 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 62 | def login(self, allow_user_interaction): |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 63 | """Refreshes cached access token or creates a new one.""" |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 64 | return self._service.login(allow_user_interaction) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 65 | |
| 66 | def logout(self): |
| 67 | """Purges cached access token.""" |
| 68 | return self._service.logout() |
| 69 | |
| 70 | def get_current_identity(self): |
| 71 | """Returns identity associated with currently used credentials. |
| 72 | |
| 73 | Identity is a string: |
| 74 | user:<email> - if using OAuth or cookie based authentication. |
| 75 | bot:<id> - if using HMAC based authentication. |
| 76 | anonymous:anonymous - if not authenticated. |
| 77 | """ |
| 78 | identity = self._service.json_request('GET', '/auth/api/v1/accounts/self') |
| 79 | if not identity: |
| 80 | raise AuthServiceError('Failed to fetch identity') |
| 81 | return identity['identity'] |
| 82 | |
| 83 | |
| 84 | @subcommand.usage('[options]') |
| 85 | def CMDlogin(parser, args): |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 86 | """Runs interactive login flow and stores auth token/cookie on disk.""" |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 87 | (options, args) = parser.parse_args(args) |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 88 | process_auth_options(parser, options) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 89 | service = AuthService(options.service) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 90 | if service.login(True): |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 91 | print 'Logged in as \'%s\'.' % service.get_current_identity() |
| 92 | return 0 |
| 93 | else: |
| 94 | print 'Login failed or canceled.' |
| 95 | return 1 |
| 96 | |
| 97 | |
| 98 | @subcommand.usage('[options]') |
| 99 | def CMDlogout(parser, args): |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 100 | """Purges cached auth token/cookie.""" |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 101 | (options, args) = parser.parse_args(args) |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 102 | process_auth_options(parser, options) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 103 | service = AuthService(options.service) |
| 104 | service.logout() |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 105 | return 0 |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 106 | |
| 107 | |
| 108 | @subcommand.usage('[options]') |
| 109 | def CMDcheck(parser, args): |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 110 | """Shows identity associated with currently cached auth token/cookie.""" |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 111 | (options, args) = parser.parse_args(args) |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 112 | process_auth_options(parser, options) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 113 | service = AuthService(options.service) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 114 | service.login(False) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 115 | print service.get_current_identity() |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 116 | return 0 |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class OptionParserAuth(tools.OptionParserWithLogging): |
| 120 | def __init__(self, **kwargs): |
| 121 | tools.OptionParserWithLogging.__init__(self, prog='auth.py', **kwargs) |
| 122 | self.server_group = tools.optparse.OptionGroup(self, 'Server') |
| 123 | self.server_group.add_option( |
| 124 | '-S', '--service', |
| 125 | metavar='URL', default='', |
| 126 | help='Service to use') |
| 127 | self.add_option_group(self.server_group) |
| 128 | add_auth_options(self) |
| 129 | |
| 130 | def parse_args(self, *args, **kwargs): |
| 131 | options, args = tools.OptionParserWithLogging.parse_args( |
| 132 | self, *args, **kwargs) |
| 133 | options.service = options.service.rstrip('/') |
| 134 | if not options.service: |
| 135 | self.error('--service is required.') |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 136 | on_error.report_on_exception_exit(options.service) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 137 | return options, args |
| 138 | |
| 139 | |
| 140 | def main(args): |
| 141 | dispatcher = subcommand.CommandDispatcher(__name__) |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 142 | return dispatcher.execute(OptionParserAuth(version=__version__), args) |
Vadim Shtayura | c4c76b6 | 2014-01-13 15:05:41 -0800 | [diff] [blame] | 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | fix_encoding.fix_encoding() |
| 147 | tools.disable_buffering() |
| 148 | colorama.init() |
| 149 | sys.exit(main(sys.argv[1:])) |