summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Mosmans <support@go-forward.net>2016-05-30 13:05:58 +1000
committerPeter Mosmans <support@go-forward.net>2016-05-30 13:05:58 +1000
commite9e48b9188e00298573bb2f407a854c8bf8a6dff (patch)
tree4d38387d85a4ed245c6384858ce553c69007608d
parent1b14f5c9b1ff0af083abedff80eafb9adcae629c (diff)
downloadgitlab-e9e48b9188e00298573bb2f407a854c8bf8a6dff.tar.gz
Added support for HTTP basic authentication
-rw-r--r--gitlab/__init__.py46
-rw-r--r--gitlab/config.py10
2 files changed, 44 insertions, 12 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 3320bba..90ffa50 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -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
@@ -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
@@ -123,6 +125,8 @@ class Gitlab(object):
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()
@@ -176,7 +180,9 @@ class Gitlab(object):
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.
@@ -264,13 +270,15 @@ class Gitlab(object):
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)
@@ -307,7 +315,10 @@ class Gitlab(object):
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)
@@ -320,7 +331,10 @@ class Gitlab(object):
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)
@@ -334,7 +348,10 @@ class Gitlab(object):
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)
@@ -374,11 +391,13 @@ class Gitlab(object):
# 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)
@@ -445,7 +464,10 @@ class Gitlab(object):
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)
diff --git a/gitlab/config.py b/gitlab/config.py
index 4d0abb8..3ef2efb 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -78,3 +78,13 @@ class GitlabConfigParser(object):
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