auth.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 with its expiration time (UTC datetime or None if unknown).
  25. class AccessToken(
  26. collections.namedtuple('AccessToken', [
  27. 'token',
  28. 'expires_at',
  29. ])):
  30. def needs_refresh(self):
  31. """True if this AccessToken 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 when needed.
  57. Args:
  58. scopes: space separated oauth scopes. Defaults to OAUTH_SCOPE_EMAIL.
  59. """
  60. def __init__(self, scopes=OAUTH_SCOPE_EMAIL):
  61. self._access_token = None
  62. self._scopes = scopes
  63. def has_cached_credentials(self):
  64. """Returns True if credentials can be obtained.
  65. If returns False, get_access_token() later will probably ask for interactive
  66. login by raising LoginRequiredError.
  67. If returns True, get_access_token() won't ask for interactive login.
  68. """
  69. return bool(self._get_luci_auth_token())
  70. def get_access_token(self):
  71. """Returns AccessToken, refreshing it if necessary.
  72. Raises:
  73. LoginRequiredError if user interaction is required.
  74. """
  75. if self._access_token and not self._access_token.needs_refresh():
  76. return self._access_token
  77. # Token expired or missing. Maybe some other process already updated it,
  78. # reload from the cache.
  79. self._access_token = self._get_luci_auth_token()
  80. if self._access_token and not self._access_token.needs_refresh():
  81. return self._access_token
  82. # Nope, still expired. Needs user interaction.
  83. logging.error('Failed to create access token')
  84. raise LoginRequiredError(self._scopes)
  85. def authorize(self, http):
  86. """Monkey patches authentication logic of httplib2.Http instance.
  87. The modified http.request method will add authentication headers to each
  88. request.
  89. Args:
  90. http: An instance of httplib2.Http.
  91. Returns:
  92. A modified instance of http that was passed in.
  93. """
  94. # Adapted from oauth2client.OAuth2Credentials.authorize.
  95. request_orig = http.request
  96. @functools.wraps(request_orig)
  97. def new_request(uri,
  98. method='GET',
  99. body=None,
  100. headers=None,
  101. redirections=httplib2.DEFAULT_MAX_REDIRECTS,
  102. connection_type=None):
  103. headers = (headers or {}).copy()
  104. headers['Authorization'] = 'Bearer %s' % self.get_access_token(
  105. ).token
  106. return request_orig(uri, method, body, headers, redirections,
  107. connection_type)
  108. http.request = new_request
  109. return http
  110. ## Private methods.
  111. def _run_luci_auth_login(self):
  112. """Run luci-auth login.
  113. Returns:
  114. AccessToken with credentials.
  115. """
  116. logging.debug('Running luci-auth login')
  117. subprocess2.check_call(['luci-auth', 'login', '-scopes', self._scopes])
  118. return self._get_luci_auth_token()
  119. def _get_luci_auth_token(self):
  120. logging.debug('Running luci-auth token')
  121. try:
  122. out, err = subprocess2.check_call_out([
  123. 'luci-auth', 'token', '-scopes', self._scopes, '-json-output',
  124. '-'
  125. ],
  126. stdout=subprocess2.PIPE,
  127. stderr=subprocess2.PIPE)
  128. logging.debug('luci-auth token stderr:\n%s', err)
  129. token_info = json.loads(out)
  130. return AccessToken(
  131. token_info['token'],
  132. datetime.datetime.utcfromtimestamp(token_info['expiry']))
  133. except subprocess2.CalledProcessError as e:
  134. # subprocess2.CalledProcessError.__str__ nicely formats
  135. # stdout/stderr.
  136. logging.error('luci-auth token failed: %s', e)
  137. return None