From 9f2342da566cec1d5300971512d9211375f52100 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Thu, 27 Oct 2016 12:49:36 +0300 Subject: [PATCH] Fix authorization headers for oauth been overwritten by basic auth & remove uneeded colon --- gitlab/__init__.py | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 1150fd287..1bb43263e 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -358,7 +358,7 @@ def set_token(self, token=None, oauth_token=None): if oauth_token: self.headers.pop("PRIVATE-TOKEN", None) - self.headers["Authorization"] = "Bearer: %s" % oauth_token + self.headers["Authorization"] = "Bearer %s" % oauth_token elif token: self.headers.pop("Authorization", None) self.headers["PRIVATE-TOKEN"] = token @@ -397,6 +397,13 @@ def _raw_get(self, path_, content_type=None, streamed=False, **kwargs): url = '%s%s' % (self._url, path_) headers = self._create_headers(content_type) + auth = requests.auth.HTTPBasicAuth( + self.http_username, + self.http_password) + + if headers["Authorization"]: + auth = None + try: return self.session.get(url, params=kwargs, @@ -404,9 +411,7 @@ def _raw_get(self, path_, content_type=None, streamed=False, **kwargs): verify=self.ssl_verify, timeout=self.timeout, stream=streamed, - auth=requests.auth.HTTPBasicAuth( - self.http_username, - self.http_password)) + auth=auth) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -447,14 +452,19 @@ def _raw_list(self, path_, cls, extra_attrs={}, **kwargs): def _raw_post(self, path_, data=None, content_type=None, **kwargs): url = '%s%s' % (self._url, path_) headers = self._create_headers(content_type) + auth = requests.auth.HTTPBasicAuth( + self.http_username, + self.http_password) + + if headers["Authorization"]: + auth = None + try: return self.session.post(url, params=kwargs, data=data, headers=headers, verify=self.ssl_verify, timeout=self.timeout, - auth=requests.auth.HTTPBasicAuth( - self.http_username, - self.http_password)) + auth=auth) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -462,15 +472,19 @@ def _raw_post(self, path_, data=None, content_type=None, **kwargs): def _raw_put(self, path_, data=None, content_type=None, **kwargs): url = '%s%s' % (self._url, path_) headers = self._create_headers(content_type) + auth = requests.auth.HTTPBasicAuth( + self.http_username, + self.http_password) + + if headers["Authorization"]: + auth = None try: return self.session.put(url, data=data, params=kwargs, headers=headers, verify=self.ssl_verify, timeout=self.timeout, - auth=requests.auth.HTTPBasicAuth( - self.http_username, - self.http_password)) + auth=auth) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -478,6 +492,12 @@ def _raw_put(self, path_, data=None, content_type=None, **kwargs): def _raw_delete(self, path_, content_type=None, **kwargs): url = '%s%s' % (self._url, path_) headers = self._create_headers(content_type) + auth = requests.auth.HTTPBasicAuth( + self.http_username, + self.http_password) + + if headers["Authorization"]: + auth = None try: return self.session.delete(url, @@ -485,9 +505,7 @@ def _raw_delete(self, path_, content_type=None, **kwargs): headers=headers, verify=self.ssl_verify, timeout=self.timeout, - auth=requests.auth.HTTPBasicAuth( - self.http_username, - self.http_password)) + auth=auth) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e)