auth.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright 2015 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Google OAuth2 related functions."""
  5. import collections
  6. import datetime
  7. import functools
  8. import httplib2
  9. import json
  10. import logging
  11. import os
  12. import subprocess2
  13. # TODO: Should fix these warnings.
  14. # pylint: disable=line-too-long
  15. # This is what most GAE apps require for authentication.
  16. OAUTH_SCOPE_EMAIL = 'https://www.googleapis.com/auth/userinfo.email'
  17. # Gerrit and Git on *.googlesource.com require this scope.
  18. OAUTH_SCOPE_GERRIT = 'https://www.googleapis.com/auth/gerritcodereview'
  19. # Deprecated. Use OAUTH_SCOPE_EMAIL instead.
  20. OAUTH_SCOPES = OAUTH_SCOPE_EMAIL
  21. # Mockable datetime.datetime.utcnow for testing.
  22. def datetime_now():
  23. return datetime.datetime.utcnow()
  24. # OAuth access token or ID token with its expiration time (UTC datetime or None
  25. # if unknown).
  26. class Token(collections.namedtuple('Token', [
  27. 'token',
  28. 'expires_at',
  29. ])):
  30. def needs_refresh(self):
  31. """True if this token should be refreshed."""
  32. if self.expires_at is not None:
  33. # Allow 30s of clock skew between client and backend.
  34. return datetime_now() + datetime.timedelta(
  35. seconds=30) >= self.expires_at
  36. # Token without expiration time never expires.
  37. return False
  38. class LoginRequiredError(Exception):
  39. """Interaction with the user is required to authenticate."""
  40. def __init__(self, scopes=OAUTH_SCOPE_EMAIL):
  41. msg = ('You are not logged in. Please login first by running:\n'
  42. ' luci-auth login -scopes %s' % scopes)
  43. super(LoginRequiredError, self).__init__(msg)
  44. def has_luci_context_local_auth():
  45. """Returns whether LUCI_CONTEXT should be used for ambient authentication."""
  46. ctx_path = os.environ.get('LUCI_CONTEXT')
  47. if not ctx_path:
  48. return False
  49. try:
  50. with open(ctx_path) as f:
  51. loaded = json.load(f)
  52. except (OSError, IOError, ValueError):
  53. return False
  54. return loaded.get('local_auth', {}).get('default_account_id') is not None
  55. class Authenticator(object):
  56. """Object that knows how to refresh access tokens or id tokens when needed.
  57. Args:
  58. scopes: space separated oauth scopes. It's used to generate access tokens.
  59. Defaults to OAUTH_SCOPE_EMAIL.
  60. audience: An audience in ID tokens to claim which clients should accept it.
  61. """
  62. def __init__(self, scopes=OAUTH_SCOPE_EMAIL, audience=None):
  63. self._access_token = None
  64. self._scopes = scopes
  65. self._id_token = None
  66. self._audience = audience
  67. def has_cached_credentials(self):
  68. """Returns True if credentials can be obtained.
  69. If returns False, get_access_token() or get_id_token() later will probably
  70. ask for interactive login by raising LoginRequiredError.
  71. If returns True, get_access_token() or get_id_token() won't ask for
  72. interactive login.
  73. """
  74. return bool(self._get_luci_auth_token())
  75. def get_access_token(self):
  76. """Returns AccessToken, refreshing it if necessary.
  77. Raises:
  78. LoginRequiredError if user interaction is required.
  79. """
  80. if self._access_token and not self._access_token.needs_refresh():
  81. return self._access_token
  82. # Token expired or missing. Maybe some other process already updated it,
  83. # reload from the cache.
  84. self._access_token = self._get_luci_auth_token()
  85. if self._access_token and not self._access_token.needs_refresh():
  86. return self._access_token
  87. # Nope, still expired. Needs user interaction.
  88. logging.error('Failed to create access token')
  89. raise LoginRequiredError(self._scopes)
  90. def get_id_token(self):
  91. """Returns id token, refreshing it if necessary.
  92. Returns:
  93. A Token object.
  94. Raises:
  95. LoginRequiredError if user interaction is required.
  96. """
  97. if self._id_token and not self._id_token.needs_refresh():
  98. return self._id_token
  99. self._id_token = self._get_luci_auth_token(use_id_token=True)
  100. if self._id_token and not self._id_token.needs_refresh():
  101. return self._id_token
  102. # Nope, still expired. Needs user interaction.
  103. logging.error('Failed to create id token')
  104. raise LoginRequiredError()
  105. def authorize(self, http, use_id_token=False):
  106. """Monkey patches authentication logic of httplib2.Http instance.
  107. The modified http.request method will add authentication headers to each
  108. request.
  109. Args:
  110. http: An instance of httplib2.Http.
  111. Returns:
  112. A modified instance of http that was passed in.
  113. """
  114. # Adapted from oauth2client.OAuth2Credentials.authorize.
  115. request_orig = http.request
  116. @functools.wraps(request_orig)
  117. def new_request(uri,
  118. method='GET',
  119. body=None,
  120. headers=None,
  121. redirections=httplib2.DEFAULT_MAX_REDIRECTS,
  122. connection_type=None):
  123. headers = (headers or {}).copy()
  124. auth_token = self.get_access_token(
  125. ) if not use_id_token else self.get_id_token()
  126. headers['Authorization'] = 'Bearer %s' % auth_token.token
  127. return request_orig(uri, method, body, headers, redirections,
  128. connection_type)
  129. http.request = new_request
  130. return http
  131. ## Private methods.
  132. def _run_luci_auth_login(self):
  133. """Run luci-auth login.
  134. Returns:
  135. AccessToken with credentials.
  136. """
  137. logging.debug('Running luci-auth login')
  138. subprocess2.check_call(['luci-auth', 'login', '-scopes', self._scopes])
  139. return self._get_luci_auth_token()
  140. def _get_luci_auth_token(self, use_id_token=False):
  141. logging.debug('Running luci-auth token')
  142. if use_id_token:
  143. args = ['-use-id-token'] + ['-audience', self._audience
  144. ] if self._audience else []
  145. else:
  146. args = ['-scopes', self._scopes]
  147. try:
  148. out, err = subprocess2.check_call_out(['luci-auth', 'token'] +
  149. args + ['-json-output', '-'],
  150. stdout=subprocess2.PIPE,
  151. stderr=subprocess2.PIPE)
  152. logging.debug('luci-auth token stderr:\n%s', err)
  153. token_info = json.loads(out)
  154. return Token(
  155. token_info['token'],
  156. datetime.datetime.utcfromtimestamp(token_info['expiry']))
  157. except subprocess2.CalledProcessError as e:
  158. # subprocess2.CalledProcessError.__str__ nicely formats
  159. # stdout/stderr.
  160. logging.error('luci-auth token failed: %s', e)
  161. return None