Reland gerrit_util: Support OAuth2 bearer tokens in CookieAuthenticator

Bug: skia:8394
Change-Id: I928eaec8459a5905360ce760c6361e4554852b44
Reviewed-on: https://chromium-review.googlesource.com/c/1261796
Reviewed-by: Vadim Shtayura <vadimsh@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Eric Boren <borenet@chromium.org>
diff --git a/gerrit_util.py b/gerrit_util.py
index e8e9561..c837145 100644
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -219,11 +219,13 @@
             continue
           domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6]
           if xpath == '/' and key == 'o':
-            login, secret_token = value.split('=', 1)
-            gitcookies[domain] = (login, secret_token)
+            if value.startswith('git-'):
+              login, secret_token = value.split('=', 1)
+              gitcookies[domain] = (login, secret_token)
+            else:
+              gitcookies[domain] = ('', value)
         except (IndexError, ValueError, TypeError) as exc:
           LOGGER.warning(exc)
-
     return gitcookies
 
   def _get_auth_for_host(self, host):
@@ -235,7 +237,10 @@
   def get_auth_header(self, host):
     a = self._get_auth_for_host(host)
     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
 
   def get_auth_email(self, host):