Skip to content

Added support for HTTP basic authentication #120

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

Merged
merged 1 commit into from
May 30, 2016
Merged
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
46 changes: 34 additions & 12 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Gitlab(object):
ssl_verify (bool): Whether SSL certificates should be validated.
timeout (float or tuple(float,float)): Timeout to use for requests to
the GitLab server.

http_username: (str): Username for HTTP authentication
http_password: (str): Password for HTTP authentication
Attributes:
user_keys (UserKeyManager): Manager for GitLab users' SSH keys.
users (UserManager): Manager for GitLab users
Expand Down Expand Up @@ -108,8 +109,9 @@ class Gitlab(object):
teams (TeamManager): Manager for GitLab teams
"""

def __init__(self, url, private_token=None,
email=None, password=None, ssl_verify=True, timeout=None):
def __init__(self, url, private_token=None, email=None, password=None,
ssl_verify=True, http_username=None, http_password=None,
timeout=None):

self._url = '%s/api/v3' % url
#: Timeout to use for requests to gitlab server
Expand All @@ -123,6 +125,8 @@ def __init__(self, url, private_token=None,
self.password = password
#: Whether SSL certificates should be validated
self.ssl_verify = ssl_verify
self.http_username = http_username
self.http_password = http_password

#: Create a session object for requests
self.session = requests.Session()
Expand Down Expand Up @@ -176,7 +180,9 @@ def from_config(gitlab_id=None, config_files=None):
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
config_files=config_files)
return Gitlab(config.url, private_token=config.token,
ssl_verify=config.ssl_verify, timeout=config.timeout)
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password)

def auth(self):
"""Performs an authentication.
Expand Down Expand Up @@ -264,13 +270,15 @@ def set_credentials(self, email, password):
def _raw_get(self, path, content_type=None, **kwargs):
url = '%s%s' % (self._url, path)
headers = self._create_headers(content_type)

try:
return self.session.get(url,
params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout)
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down Expand Up @@ -307,7 +315,10 @@ def _raw_post(self, path, data=None, content_type=None, **kwargs):
return self.session.post(url, params=kwargs, data=data,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout)
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand All @@ -320,7 +331,10 @@ def _raw_put(self, path, data=None, content_type=None, **kwargs):
return self.session.put(url, data=data, params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout)
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand All @@ -334,7 +348,10 @@ def _raw_delete(self, path, content_type=None, **kwargs):
params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout)
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down Expand Up @@ -374,11 +391,13 @@ def list(self, obj_class, **kwargs):
# Also remove the next-url attribute that make queries fail
if 'next_url' in params:
del params['next_url']

try:
r = self.session.get(url, params=params, headers=headers,
verify=self.ssl_verify,
timeout=self.timeout)
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down Expand Up @@ -445,7 +464,10 @@ def get(self, obj_class, id=None, **kwargs):

try:
r = self.session.get(url, params=params, headers=headers,
verify=self.ssl_verify, timeout=self.timeout)
verify=self.ssl_verify, timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down
10 changes: 10 additions & 0 deletions gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ def __init__(self, gitlab_id=None, config_files=None):
self.timeout = self._config.getint(self.gitlab_id, 'timeout')
except Exception:
pass

self.http_username = None
self.http_password = None
try:
self.http_username = self._config.get(self.gitlab_id,
'http_username')
self.http_password = self._config.get(self.gitlab_id,
'http_password')
except Exception:
pass