Skip to content

fix oauth token access #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To connect to a GitLab server, create a ``gitlab.Gitlab`` object:
gl = gitlab.Gitlab('http://10.0.0.1', 'JVNSESs8EwWRx5yDxM5q')

# oauth authentication
gl = gitlab.Gitlab('http://10.0.0.1', my_oauth2_token)
gl = gitlab.Gitlab('http://10.0.0.1', oauth_token='my_oauth2_token')

# or username/password authentication
gl = gitlab.Gitlab('http://10.0.0.1', email='jdoe', password='s3cr3t')
Expand Down
16 changes: 11 additions & 5 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -397,16 +397,22 @@ def _raw_get(self, path_, content_type=None, streamed=False, **kwargs):
url = '%s%s' % (self._url, path_)

headers = self._create_headers(content_type)

if 'Authorization' in self.headers:
auth = None
else:
auth = requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one thing bothering me here: with oauth token we explicitly define headers, with basic HTTP auth we don't and use a python-requests feature instead. I'd prefer to have the same method for both authentication methods.

Updating the _create_headers method might be the easiest way to achieve consistency. This would also avoid code duplication in the _raw_* methods.

What do you think about this?

try:
return self.session.get(url,
params=kwargs,
headers=headers,
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)
Expand Down Expand Up @@ -438,7 +444,7 @@ def _raw_list(self, path_, cls, extra_attrs={}, **kwargs):
results = [cls(self, item, **params) for item in r.json()
if item is not None]
if ('next' in r.links and 'url' in r.links['next']
and get_all_results is True):
and get_all_results is True):
args = kwargs.copy()
args['next_url'] = r.links['next']['url']
results.extend(self.list(cls, **args))
Expand Down