Commit 18b4479c authored by Eric Boren's avatar Eric Boren Committed by Commit Bot

gerrit_util: Support OAuth2 bearer tokens in CookieAuthenticator

Change-Id: I9fd3e2fa9ea16d7f057290e77a81f7009c7c9d91
Reviewed-on: https://chromium-review.googlesource.com/1221826Reviewed-by: 's avatarVadim Shtayura <vadimsh@chromium.org>
Reviewed-by: 's avatarAndrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Eric Boren <borenet@chromium.org>
parent f9b48459
...@@ -200,11 +200,13 @@ class CookiesAuthenticator(Authenticator): ...@@ -200,11 +200,13 @@ class CookiesAuthenticator(Authenticator):
continue continue
domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6] domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6]
if xpath == '/' and key == 'o': if xpath == '/' and key == 'o':
login, secret_token = value.split('=', 1) if value.startswith('git-'):
gitcookies[domain] = (login, secret_token) login, secret_token = value.split('=', 1)
gitcookies[domain] = (login, secret_token)
else:
gitcookies[domain] = ('', value)
except (IndexError, ValueError, TypeError) as exc: except (IndexError, ValueError, TypeError) as exc:
LOGGER.warning(exc) LOGGER.warning(exc)
return gitcookies return gitcookies
def _get_auth_for_host(self, host): def _get_auth_for_host(self, host):
...@@ -216,7 +218,10 @@ class CookiesAuthenticator(Authenticator): ...@@ -216,7 +218,10 @@ class CookiesAuthenticator(Authenticator):
def get_auth_header(self, host): def get_auth_header(self, host):
a = self._get_auth_for_host(host) a = self._get_auth_for_host(host)
if a: if a:
return 'Basic %s' % (base64.b64encode('%s:%s' % (a[0], a[2]))) if a[0]:
return 'Basic %s' % (base64.b64encode('%s:%s' % (a[0], a[2])))
else:
return 'Bearer %s' % a[2]
return None return None
def get_auth_email(self, host): def get_auth_email(self, host):
......
...@@ -24,6 +24,7 @@ import metrics ...@@ -24,6 +24,7 @@ import metrics
# We have to disable monitoring before importing git_cl. # We have to disable monitoring before importing git_cl.
metrics.DISABLE_METRICS_COLLECTION = True metrics.DISABLE_METRICS_COLLECTION = True
import gerrit_util
import git_cl import git_cl
import git_common import git_common
import git_footers import git_footers
...@@ -2104,6 +2105,18 @@ class TestGitCl(TestCase): ...@@ -2104,6 +2105,18 @@ class TestGitCl(TestCase):
auth={}, skip_auth_check=True) auth={}, skip_auth_check=True)
self.assertIsNone(cl.EnsureAuthenticated(force=False)) self.assertIsNone(cl.EnsureAuthenticated(force=False))
def test_gerrit_ensure_authenticated_bearer_token(self):
cl = self._test_gerrit_ensure_authenticated_common(auth={
'chromium.googlesource.com':
('', None, 'secret'),
'chromium-review.googlesource.com':
('', None, 'secret'),
})
self.assertIsNone(cl.EnsureAuthenticated(force=False))
header = gerrit_util.CookiesAuthenticator().get_auth_header(
'chromium.googlesource.com')
self.assertTrue('Bearer' in header)
def test_cmd_set_commit_rietveld(self): def test_cmd_set_commit_rietveld(self):
self.mock(git_cl._RietveldChangelistImpl, 'SetFlags', self.mock(git_cl._RietveldChangelistImpl, 'SetFlags',
lambda _, v: self._mocked_call(['SetFlags', v])) lambda _, v: self._mocked_call(['SetFlags', v]))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment