vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 1 | # Copyright 2015 The Chromium Authors. All rights reserved. |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 5 | """Google OAuth2 related functions.""" |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 6 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 9 | import collections |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 10 | import datetime |
| 11 | import functools |
| 12 | import json |
| 13 | import logging |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 14 | import optparse |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 15 | import os |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 16 | import sys |
| 17 | import threading |
| 18 | import urllib |
| 19 | import urlparse |
Edward Lemur | ba5bc99 | 2019-09-23 22:59:17 +0000 | [diff] [blame] | 20 | |
| 21 | import subprocess2 |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 22 | |
Nodir Turakulov | 5abb9b7 | 2019-10-12 20:55:10 +0000 | [diff] [blame] | 23 | from third_party import httplib2 |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 24 | from third_party.oauth2client import client |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | # depot_tools/. |
| 28 | DEPOT_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 29 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 30 | # This is what most GAE apps require for authentication. |
| 31 | OAUTH_SCOPE_EMAIL = 'https://www.googleapis.com/auth/userinfo.email' |
| 32 | # Gerrit and Git on *.googlesource.com require this scope. |
| 33 | OAUTH_SCOPE_GERRIT = 'https://www.googleapis.com/auth/gerritcodereview' |
| 34 | # Deprecated. Use OAUTH_SCOPE_EMAIL instead. |
| 35 | OAUTH_SCOPES = OAUTH_SCOPE_EMAIL |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 36 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 37 | |
| 38 | # Authentication configuration extracted from command line options. |
| 39 | # See doc string for 'make_auth_config' for meaning of fields. |
| 40 | AuthConfig = collections.namedtuple('AuthConfig', [ |
| 41 | 'use_oauth2', # deprecated, will be always True |
| 42 | 'save_cookies', # deprecated, will be removed |
| 43 | 'use_local_webserver', |
| 44 | 'webserver_port', |
| 45 | ]) |
| 46 | |
| 47 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 48 | # OAuth access token with its expiration time (UTC datetime or None if unknown). |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 49 | class AccessToken(collections.namedtuple('AccessToken', [ |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 50 | 'token', |
| 51 | 'expires_at', |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 52 | ])): |
| 53 | |
| 54 | def needs_refresh(self, now=None): |
| 55 | """True if this AccessToken should be refreshed.""" |
| 56 | if self.expires_at is not None: |
| 57 | now = now or datetime.datetime.utcnow() |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 58 | # Allow 3 min of clock skew between client and backend. |
| 59 | now += datetime.timedelta(seconds=180) |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 60 | return now >= self.expires_at |
| 61 | # Token without expiration time never expires. |
| 62 | return False |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | class AuthenticationError(Exception): |
| 66 | """Raised on errors related to authentication.""" |
| 67 | |
| 68 | |
| 69 | class LoginRequiredError(AuthenticationError): |
| 70 | """Interaction with the user is required to authenticate.""" |
| 71 | |
Edward Lemur | ba5bc99 | 2019-09-23 22:59:17 +0000 | [diff] [blame] | 72 | def __init__(self, scopes=OAUTH_SCOPE_EMAIL): |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 73 | msg = ( |
| 74 | 'You are not logged in. Please login first by running:\n' |
Edward Lemur | ba5bc99 | 2019-09-23 22:59:17 +0000 | [diff] [blame] | 75 | ' luci-auth login -scopes %s' % scopes) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 76 | super(LoginRequiredError, self).__init__(msg) |
| 77 | |
| 78 | |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 79 | class LuciContextAuthError(Exception): |
| 80 | """Raised on errors related to unsuccessful attempts to load LUCI_CONTEXT""" |
| 81 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 82 | def __init__(self, msg, exc=None): |
| 83 | if exc is None: |
| 84 | logging.error(msg) |
| 85 | else: |
| 86 | logging.exception(msg) |
| 87 | msg = '%s: %s' % (msg, exc) |
| 88 | super(LuciContextAuthError, self).__init__(msg) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 89 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 90 | |
| 91 | def has_luci_context_local_auth(): |
| 92 | """Returns whether LUCI_CONTEXT should be used for ambient authentication. |
| 93 | """ |
| 94 | try: |
Andrii Shyshkalov | b3c4441 | 2018-04-19 14:27:19 -0700 | [diff] [blame] | 95 | params = _get_luci_context_local_auth_params() |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 96 | except LuciContextAuthError: |
| 97 | return False |
| 98 | if params is None: |
| 99 | return False |
| 100 | return bool(params.default_account_id) |
| 101 | |
| 102 | |
| 103 | def get_luci_context_access_token(scopes=OAUTH_SCOPE_EMAIL): |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 104 | """Returns a valid AccessToken from the local LUCI context auth server. |
| 105 | |
| 106 | Adapted from |
| 107 | https://chromium.googlesource.com/infra/luci/luci-py/+/master/client/libs/luci_context/luci_context.py |
| 108 | See the link above for more details. |
| 109 | |
| 110 | Returns: |
| 111 | AccessToken if LUCI_CONTEXT is present and attempt to load it is successful. |
| 112 | None if LUCI_CONTEXT is absent. |
| 113 | |
| 114 | Raises: |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 115 | LuciContextAuthError if LUCI_CONTEXT is present, but there was a failure |
| 116 | obtaining its access token. |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 117 | """ |
Andrii Shyshkalov | b3c4441 | 2018-04-19 14:27:19 -0700 | [diff] [blame] | 118 | params = _get_luci_context_local_auth_params() |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 119 | if params is None: |
| 120 | return None |
| 121 | return _get_luci_context_access_token( |
| 122 | params, datetime.datetime.utcnow(), scopes) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 123 | |
| 124 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 125 | _LuciContextLocalAuthParams = collections.namedtuple( |
| 126 | '_LuciContextLocalAuthParams', [ |
| 127 | 'default_account_id', |
| 128 | 'secret', |
| 129 | 'rpc_port', |
| 130 | ]) |
| 131 | |
| 132 | |
Andrii Shyshkalov | b3c4441 | 2018-04-19 14:27:19 -0700 | [diff] [blame] | 133 | def _cache_thread_safe(f): |
| 134 | """Decorator caching result of nullary function in thread-safe way.""" |
| 135 | lock = threading.Lock() |
| 136 | cache = [] |
| 137 | |
| 138 | @functools.wraps(f) |
| 139 | def caching_wrapper(): |
| 140 | if not cache: |
| 141 | with lock: |
| 142 | if not cache: |
| 143 | cache.append(f()) |
| 144 | return cache[0] |
| 145 | |
| 146 | # Allow easy way to clear cache, particularly useful in tests. |
| 147 | caching_wrapper.clear_cache = lambda: cache.pop() if cache else None |
| 148 | return caching_wrapper |
| 149 | |
| 150 | |
| 151 | @_cache_thread_safe |
| 152 | def _get_luci_context_local_auth_params(): |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 153 | """Returns local auth parameters if local auth is configured else None. |
| 154 | |
| 155 | Raises LuciContextAuthError on unexpected failures. |
| 156 | """ |
Andrii Shyshkalov | b3c4441 | 2018-04-19 14:27:19 -0700 | [diff] [blame] | 157 | ctx_path = os.environ.get('LUCI_CONTEXT') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 158 | if not ctx_path: |
| 159 | return None |
| 160 | ctx_path = ctx_path.decode(sys.getfilesystemencoding()) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 161 | try: |
| 162 | loaded = _load_luci_context(ctx_path) |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 163 | except (OSError, IOError, ValueError) as e: |
| 164 | raise LuciContextAuthError('Failed to open, read or decode LUCI_CONTEXT', e) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 165 | try: |
| 166 | local_auth = loaded.get('local_auth') |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 167 | except AttributeError as e: |
| 168 | raise LuciContextAuthError('LUCI_CONTEXT not in proper format', e) |
| 169 | if local_auth is None: |
| 170 | logging.debug('LUCI_CONTEXT configured w/o local auth') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 171 | return None |
| 172 | try: |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 173 | return _LuciContextLocalAuthParams( |
| 174 | default_account_id=local_auth.get('default_account_id'), |
| 175 | secret=local_auth.get('secret'), |
| 176 | rpc_port=int(local_auth.get('rpc_port'))) |
| 177 | except (AttributeError, ValueError) as e: |
| 178 | raise LuciContextAuthError('local_auth config malformed', e) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 179 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 180 | |
| 181 | def _load_luci_context(ctx_path): |
| 182 | # Kept separate for test mocking. |
| 183 | with open(ctx_path) as f: |
| 184 | return json.load(f) |
| 185 | |
| 186 | |
| 187 | def _get_luci_context_access_token(params, now, scopes=OAUTH_SCOPE_EMAIL): |
| 188 | # No account, local_auth shouldn't be used. |
| 189 | if not params.default_account_id: |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 190 | return None |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 191 | if not params.secret: |
| 192 | raise LuciContextAuthError('local_auth: no secret') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 193 | |
| 194 | logging.debug('local_auth: requesting an access token for account "%s"', |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 195 | params.default_account_id) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 196 | http = httplib2.Http() |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 197 | host = '127.0.0.1:%d' % params.rpc_port |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 198 | resp, content = http.request( |
| 199 | uri='http://%s/rpc/LuciLocalAuthService.GetOAuthToken' % host, |
| 200 | method='POST', |
| 201 | body=json.dumps({ |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 202 | 'account_id': params.default_account_id, |
| 203 | 'scopes': scopes.split(' '), |
| 204 | 'secret': params.secret, |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 205 | }), |
| 206 | headers={'Content-Type': 'application/json'}) |
| 207 | if resp.status != 200: |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 208 | raise LuciContextAuthError( |
| 209 | 'local_auth: Failed to grab access token from ' |
| 210 | 'LUCI context server with status %d: %r' % (resp.status, content)) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 211 | try: |
| 212 | token = json.loads(content) |
| 213 | error_code = token.get('error_code') |
| 214 | error_message = token.get('error_message') |
| 215 | access_token = token.get('access_token') |
| 216 | expiry = token.get('expiry') |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 217 | except (AttributeError, ValueError) as e: |
| 218 | raise LuciContextAuthError('Unexpected access token response format', e) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 219 | if error_code: |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 220 | raise LuciContextAuthError( |
| 221 | 'Error %d in retrieving access token: %s', error_code, error_message) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 222 | if not access_token: |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 223 | raise LuciContextAuthError( |
| 224 | 'No access token returned from LUCI context server') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 225 | expiry_dt = None |
| 226 | if expiry: |
| 227 | try: |
| 228 | expiry_dt = datetime.datetime.utcfromtimestamp(expiry) |
Mun Yong Jang | 1728f5f | 2017-11-27 13:29:08 -0800 | [diff] [blame] | 229 | logging.debug( |
| 230 | 'local_auth: got an access token for ' |
| 231 | 'account "%s" that expires in %d sec', |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 232 | params.default_account_id, (expiry_dt - now).total_seconds()) |
| 233 | except (TypeError, ValueError) as e: |
| 234 | raise LuciContextAuthError('Invalid expiry in returned token', e) |
Mun Yong Jang | 1728f5f | 2017-11-27 13:29:08 -0800 | [diff] [blame] | 235 | else: |
| 236 | logging.debug( |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 237 | 'local auth: got an access token for account "%s" that does not expire', |
| 238 | params.default_account_id) |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 239 | access_token = AccessToken(access_token, expiry_dt) |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 240 | if access_token.needs_refresh(now=now): |
| 241 | raise LuciContextAuthError('Received access token is already expired') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 242 | return access_token |
| 243 | |
| 244 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 245 | def make_auth_config( |
| 246 | use_oauth2=None, |
| 247 | save_cookies=None, |
| 248 | use_local_webserver=None, |
Edward Lemur | a056817 | 2019-10-16 15:37:58 +0000 | [diff] [blame] | 249 | webserver_port=None): |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 250 | """Returns new instance of AuthConfig. |
| 251 | |
| 252 | If some config option is None, it will be set to a reasonable default value. |
| 253 | This function also acts as an authoritative place for default values of |
| 254 | corresponding command line options. |
| 255 | """ |
| 256 | default = lambda val, d: val if val is not None else d |
| 257 | return AuthConfig( |
vadimsh@chromium.org | 19f3fe6 | 2015-04-20 17:03:10 +0000 | [diff] [blame] | 258 | default(use_oauth2, True), |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 259 | default(save_cookies, True), |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 260 | default(use_local_webserver, not _is_headless()), |
Edward Lemur | a056817 | 2019-10-16 15:37:58 +0000 | [diff] [blame] | 261 | default(webserver_port, 8090)) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 262 | |
| 263 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 264 | def add_auth_options(parser, default_config=None): |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 265 | """Appends OAuth related options to OptionParser.""" |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 266 | default_config = default_config or make_auth_config() |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 267 | parser.auth_group = optparse.OptionGroup(parser, 'Auth options') |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 268 | parser.add_option_group(parser.auth_group) |
| 269 | |
| 270 | # OAuth2 vs password switch. |
| 271 | auth_default = 'use OAuth2' if default_config.use_oauth2 else 'use password' |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 272 | parser.auth_group.add_option( |
| 273 | '--oauth2', |
| 274 | action='store_true', |
| 275 | dest='use_oauth2', |
| 276 | default=default_config.use_oauth2, |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 277 | help='Use OAuth 2.0 instead of a password. [default: %s]' % auth_default) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 278 | parser.auth_group.add_option( |
| 279 | '--no-oauth2', |
| 280 | action='store_false', |
| 281 | dest='use_oauth2', |
| 282 | default=default_config.use_oauth2, |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 283 | help='Use password instead of OAuth 2.0. [default: %s]' % auth_default) |
| 284 | |
| 285 | # Password related options, deprecated. |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 286 | parser.auth_group.add_option( |
| 287 | '--no-cookies', |
| 288 | action='store_false', |
| 289 | dest='save_cookies', |
| 290 | default=default_config.save_cookies, |
| 291 | help='Do not save authentication cookies to local disk.') |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 292 | |
| 293 | # OAuth2 related options. |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 294 | parser.auth_group.add_option( |
| 295 | '--auth-no-local-webserver', |
| 296 | action='store_false', |
| 297 | dest='use_local_webserver', |
| 298 | default=default_config.use_local_webserver, |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 299 | help='Do not run a local web server when performing OAuth2 login flow.') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 300 | parser.auth_group.add_option( |
| 301 | '--auth-host-port', |
| 302 | type=int, |
| 303 | default=default_config.webserver_port, |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 304 | help='Port a local web server should listen on. Used only if ' |
| 305 | '--auth-no-local-webserver is not set. [default: %default]') |
vadimsh@chromium.org | 24daf9e | 2015-04-17 02:42:44 +0000 | [diff] [blame] | 306 | parser.auth_group.add_option( |
| 307 | '--auth-refresh-token-json', |
Edward Lemur | a056817 | 2019-10-16 15:37:58 +0000 | [diff] [blame] | 308 | help='DEPRECATED. Do not use') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | def extract_auth_config_from_options(options): |
| 312 | """Given OptionParser parsed options, extracts AuthConfig from it. |
| 313 | |
| 314 | OptionParser should be populated with auth options by 'add_auth_options'. |
| 315 | """ |
| 316 | return make_auth_config( |
| 317 | use_oauth2=options.use_oauth2, |
| 318 | save_cookies=False if options.use_oauth2 else options.save_cookies, |
| 319 | use_local_webserver=options.use_local_webserver, |
Edward Lemur | a056817 | 2019-10-16 15:37:58 +0000 | [diff] [blame] | 320 | webserver_port=options.auth_host_port) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 321 | |
| 322 | |
| 323 | def auth_config_to_command_options(auth_config): |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 324 | """AuthConfig -> list of strings with command line options. |
| 325 | |
| 326 | Omits options that are set to default values. |
| 327 | """ |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 328 | if not auth_config: |
| 329 | return [] |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 330 | defaults = make_auth_config() |
| 331 | opts = [] |
| 332 | if auth_config.use_oauth2 != defaults.use_oauth2: |
| 333 | opts.append('--oauth2' if auth_config.use_oauth2 else '--no-oauth2') |
| 334 | if auth_config.save_cookies != auth_config.save_cookies: |
| 335 | if not auth_config.save_cookies: |
| 336 | opts.append('--no-cookies') |
| 337 | if auth_config.use_local_webserver != defaults.use_local_webserver: |
| 338 | if not auth_config.use_local_webserver: |
| 339 | opts.append('--auth-no-local-webserver') |
| 340 | if auth_config.webserver_port != defaults.webserver_port: |
| 341 | opts.extend(['--auth-host-port', str(auth_config.webserver_port)]) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 342 | return opts |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 343 | |
| 344 | |
Edward Lemur | b4a587d | 2019-10-09 23:56:38 +0000 | [diff] [blame] | 345 | def get_authenticator(config, scopes=OAUTH_SCOPE_EMAIL): |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 346 | """Returns Authenticator instance to access given host. |
| 347 | |
| 348 | Args: |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 349 | config: AuthConfig instance. |
Andrii Shyshkalov | 741afe8 | 2018-04-19 14:32:18 -0700 | [diff] [blame] | 350 | scopes: space separated oauth scopes. Defaults to OAUTH_SCOPE_EMAIL. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 351 | |
| 352 | Returns: |
| 353 | Authenticator object. |
| 354 | """ |
Edward Lemur | b4a587d | 2019-10-09 23:56:38 +0000 | [diff] [blame] | 355 | return Authenticator(config, scopes) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | class Authenticator(object): |
| 359 | """Object that knows how to refresh access tokens when needed. |
| 360 | |
| 361 | Args: |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 362 | config: AuthConfig object that holds authentication configuration. |
| 363 | """ |
| 364 | |
Edward Lemur | b4a587d | 2019-10-09 23:56:38 +0000 | [diff] [blame] | 365 | def __init__(self, config, scopes): |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 366 | assert isinstance(config, AuthConfig) |
| 367 | assert config.use_oauth2 |
| 368 | self._access_token = None |
| 369 | self._config = config |
| 370 | self._lock = threading.Lock() |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 371 | self._scopes = scopes |
vadimsh@chromium.org | cfbeecb | 2015-04-21 00:12:36 +0000 | [diff] [blame] | 372 | logging.debug('Using auth config %r', config) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 373 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 374 | def has_cached_credentials(self): |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 375 | """Returns True if long term credentials (refresh token) are in cache. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 376 | |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 377 | Doesn't make network calls. |
| 378 | |
| 379 | If returns False, get_access_token() later will ask for interactive login by |
| 380 | raising LoginRequiredError. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 381 | |
| 382 | If returns True, most probably get_access_token() won't ask for interactive |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 383 | login, though it is not guaranteed, since cached token can be already |
| 384 | revoked and there's no way to figure this out without actually trying to use |
| 385 | it. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 386 | """ |
| 387 | with self._lock: |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 388 | return bool(self._get_cached_credentials()) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 389 | |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 390 | def get_access_token(self, force_refresh=False, allow_user_interaction=False, |
| 391 | use_local_auth=True): |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 392 | """Returns AccessToken, refreshing it if necessary. |
| 393 | |
| 394 | Args: |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 395 | force_refresh: forcefully refresh access token even if it is not expired. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 396 | allow_user_interaction: True to enable blocking for user input if needed. |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 397 | use_local_auth: default to local auth if needed. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 398 | |
| 399 | Raises: |
| 400 | AuthenticationError on error or if authentication flow was interrupted. |
| 401 | LoginRequiredError if user interaction is required, but |
| 402 | allow_user_interaction is False. |
| 403 | """ |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 404 | def get_loc_auth_tkn(): |
| 405 | exi = sys.exc_info() |
| 406 | if not use_local_auth: |
| 407 | logging.error('Failed to create access token') |
| 408 | raise |
| 409 | try: |
| 410 | self._access_token = get_luci_context_access_token() |
| 411 | if not self._access_token: |
| 412 | logging.error('Failed to create access token') |
| 413 | raise |
| 414 | return self._access_token |
| 415 | except LuciContextAuthError: |
| 416 | logging.exception('Failed to use local auth') |
| 417 | raise exi[0], exi[1], exi[2] |
| 418 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 419 | with self._lock: |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 420 | if force_refresh: |
| 421 | logging.debug('Forcing access token refresh') |
Mun Yong Jang | acc8e3e | 2017-11-22 10:49:56 -0800 | [diff] [blame] | 422 | try: |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 423 | self._access_token = self._create_access_token(allow_user_interaction) |
| 424 | return self._access_token |
| 425 | except LoginRequiredError: |
| 426 | return get_loc_auth_tkn() |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 427 | |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 428 | # Load from on-disk cache on a first access. |
| 429 | if not self._access_token: |
| 430 | self._access_token = self._load_access_token() |
| 431 | |
| 432 | # Refresh if expired or missing. |
| 433 | if not self._access_token or self._access_token.needs_refresh(): |
| 434 | # Maybe some other process already updated it, reload from the cache. |
| 435 | self._access_token = self._load_access_token() |
| 436 | # Nope, still expired, need to run the refresh flow. |
| 437 | if not self._access_token or self._access_token.needs_refresh(): |
| 438 | try: |
| 439 | self._access_token = self._create_access_token( |
| 440 | allow_user_interaction) |
| 441 | except LoginRequiredError: |
| 442 | get_loc_auth_tkn() |
| 443 | |
| 444 | return self._access_token |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 445 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 446 | def authorize(self, http): |
| 447 | """Monkey patches authentication logic of httplib2.Http instance. |
| 448 | |
| 449 | The modified http.request method will add authentication headers to each |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 450 | request and will refresh access_tokens when a 401 is received on a |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 451 | request. |
| 452 | |
| 453 | Args: |
| 454 | http: An instance of httplib2.Http. |
| 455 | |
| 456 | Returns: |
| 457 | A modified instance of http that was passed in. |
| 458 | """ |
| 459 | # Adapted from oauth2client.OAuth2Credentials.authorize. |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 460 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 461 | request_orig = http.request |
| 462 | |
| 463 | @functools.wraps(request_orig) |
| 464 | def new_request( |
| 465 | uri, method='GET', body=None, headers=None, |
| 466 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 467 | connection_type=None): |
| 468 | headers = (headers or {}).copy() |
vadimsh@chromium.org | afbb019 | 2015-04-13 23:26:31 +0000 | [diff] [blame] | 469 | headers['Authorization'] = 'Bearer %s' % self.get_access_token().token |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 470 | resp, content = request_orig( |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 471 | uri, method, body, headers, redirections, connection_type) |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 472 | if resp.status in client.REFRESH_STATUS_CODES: |
| 473 | logging.info('Refreshing due to a %s', resp.status) |
| 474 | access_token = self.get_access_token(force_refresh=True) |
| 475 | headers['Authorization'] = 'Bearer %s' % access_token.token |
| 476 | return request_orig( |
| 477 | uri, method, body, headers, redirections, connection_type) |
| 478 | else: |
| 479 | return (resp, content) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 480 | |
| 481 | http.request = new_request |
| 482 | return http |
| 483 | |
| 484 | ## Private methods. |
| 485 | |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 486 | def _get_cached_credentials(self): |
| 487 | """Returns oauth2client.Credentials loaded from luci-auth.""" |
| 488 | credentials = _get_luci_auth_credentials(self._scopes) |
| 489 | |
| 490 | if not credentials: |
| 491 | logging.debug('No cached token') |
| 492 | else: |
| 493 | _log_credentials_info('cached token', credentials) |
| 494 | |
| 495 | return credentials if (credentials and not credentials.invalid) else None |
| 496 | |
| 497 | def _load_access_token(self): |
| 498 | """Returns cached AccessToken if it is not expired yet.""" |
| 499 | logging.debug('Reloading access token from cache') |
| 500 | creds = self._get_cached_credentials() |
| 501 | if not creds or not creds.access_token or creds.access_token_expired: |
| 502 | logging.debug('Access token is missing or expired') |
| 503 | return None |
| 504 | return AccessToken(str(creds.access_token), creds.token_expiry) |
| 505 | |
| 506 | def _create_access_token(self, allow_user_interaction=False): |
| 507 | """Mints and caches a new access token, launching OAuth2 dance if necessary. |
| 508 | |
| 509 | Uses cached refresh token, if present. In that case user interaction is not |
| 510 | required and function will finish quietly. Otherwise it will launch 3-legged |
| 511 | OAuth2 flow, that needs user interaction. |
| 512 | |
| 513 | Args: |
| 514 | allow_user_interaction: if True, allow interaction with the user (e.g. |
| 515 | reading standard input, or launching a browser). |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 516 | |
| 517 | Returns: |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 518 | AccessToken. |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 519 | |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 520 | Raises: |
| 521 | AuthenticationError on error or if authentication flow was interrupted. |
| 522 | LoginRequiredError if user interaction is required, but |
| 523 | allow_user_interaction is False. |
| 524 | """ |
| 525 | logging.debug( |
| 526 | 'Making new access token (allow_user_interaction=%r)', |
| 527 | allow_user_interaction) |
| 528 | credentials = self._get_cached_credentials() |
| 529 | |
| 530 | # 3-legged flow with (perhaps cached) refresh token. |
| 531 | refreshed = False |
| 532 | if credentials and not credentials.invalid: |
| 533 | try: |
| 534 | logging.debug('Attempting to refresh access_token') |
| 535 | credentials.refresh(httplib2.Http()) |
| 536 | _log_credentials_info('refreshed token', credentials) |
| 537 | refreshed = True |
| 538 | except client.Error as err: |
| 539 | logging.warning( |
| 540 | 'OAuth error during access token refresh (%s). ' |
| 541 | 'Attempting a full authentication flow.', err) |
| 542 | |
| 543 | # Refresh token is missing or invalid, go through the full flow. |
| 544 | if not refreshed: |
| 545 | if not allow_user_interaction: |
| 546 | logging.debug('Requesting user to login') |
| 547 | raise LoginRequiredError(self._scopes) |
| 548 | logging.debug('Launching OAuth browser flow') |
| 549 | credentials = _run_oauth_dance(self._scopes) |
| 550 | _log_credentials_info('new token', credentials) |
| 551 | |
| 552 | logging.info( |
| 553 | 'OAuth access_token refreshed. Expires in %s.', |
| 554 | credentials.token_expiry - datetime.datetime.utcnow()) |
| 555 | return AccessToken(str(credentials.access_token), credentials.token_expiry) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 556 | |
| 557 | |
| 558 | ## Private functions. |
| 559 | |
| 560 | |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 561 | def _is_headless(): |
| 562 | """True if machine doesn't seem to have a display.""" |
| 563 | return sys.platform == 'linux2' and not os.environ.get('DISPLAY') |
Edward Lesmes | 989bc35 | 2019-10-17 05:45:35 +0000 | [diff] [blame] | 564 | |
| 565 | |
| 566 | def _log_credentials_info(title, credentials): |
| 567 | """Dumps (non sensitive) part of client.Credentials object to debug log.""" |
| 568 | if credentials: |
| 569 | logging.debug('%s info: %r', title, { |
| 570 | 'access_token_expired': credentials.access_token_expired, |
| 571 | 'has_access_token': bool(credentials.access_token), |
| 572 | 'invalid': credentials.invalid, |
| 573 | 'utcnow': datetime.datetime.utcnow(), |
| 574 | 'token_expiry': credentials.token_expiry, |
| 575 | }) |
| 576 | |
| 577 | |
| 578 | def _get_luci_auth_credentials(scopes): |
| 579 | try: |
| 580 | token_info = json.loads(subprocess2.check_output( |
| 581 | ['luci-auth', 'token', '-scopes', scopes, '-json-output', '-'], |
| 582 | stderr=subprocess2.VOID)) |
| 583 | except subprocess2.CalledProcessError: |
| 584 | return None |
| 585 | |
| 586 | return client.OAuth2Credentials( |
| 587 | access_token=token_info['token'], |
| 588 | client_id=None, |
| 589 | client_secret=None, |
| 590 | refresh_token=None, |
| 591 | token_expiry=datetime.datetime.utcfromtimestamp(token_info['expiry']), |
| 592 | token_uri=None, |
| 593 | user_agent=None, |
| 594 | revoke_uri=None) |
| 595 | |
| 596 | |
| 597 | def _run_oauth_dance(scopes): |
| 598 | """Perform full 3-legged OAuth2 flow with the browser. |
| 599 | |
| 600 | Returns: |
| 601 | oauth2client.Credentials. |
| 602 | """ |
| 603 | subprocess2.check_call(['luci-auth', 'login', '-scopes', scopes]) |
| 604 | return _get_luci_auth_credentials(scopes) |