Skip to content

Commit e9e48b9

Browse files
committed
Added support for HTTP basic authentication
1 parent 1b14f5c commit e9e48b9

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

gitlab/__init__.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class Gitlab(object):
6262
ssl_verify (bool): Whether SSL certificates should be validated.
6363
timeout (float or tuple(float,float)): Timeout to use for requests to
6464
the GitLab server.
65-
65+
http_username: (str): Username for HTTP authentication
66+
http_password: (str): Password for HTTP authentication
6667
Attributes:
6768
user_keys (UserKeyManager): Manager for GitLab users' SSH keys.
6869
users (UserManager): Manager for GitLab users
@@ -108,8 +109,9 @@ class Gitlab(object):
108109
teams (TeamManager): Manager for GitLab teams
109110
"""
110111

111-
def __init__(self, url, private_token=None,
112-
email=None, password=None, ssl_verify=True, timeout=None):
112+
def __init__(self, url, private_token=None, email=None, password=None,
113+
ssl_verify=True, http_username=None, http_password=None,
114+
timeout=None):
113115

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

127131
#: Create a session object for requests
128132
self.session = requests.Session()
@@ -176,7 +180,9 @@ def from_config(gitlab_id=None, config_files=None):
176180
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
177181
config_files=config_files)
178182
return Gitlab(config.url, private_token=config.token,
179-
ssl_verify=config.ssl_verify, timeout=config.timeout)
183+
ssl_verify=config.ssl_verify, timeout=config.timeout,
184+
http_username=config.http_username,
185+
http_password=config.http_password)
180186

181187
def auth(self):
182188
"""Performs an authentication.
@@ -264,13 +270,15 @@ def set_credentials(self, email, password):
264270
def _raw_get(self, path, content_type=None, **kwargs):
265271
url = '%s%s' % (self._url, path)
266272
headers = self._create_headers(content_type)
267-
268273
try:
269274
return self.session.get(url,
270275
params=kwargs,
271276
headers=headers,
272277
verify=self.ssl_verify,
273-
timeout=self.timeout)
278+
timeout=self.timeout,
279+
auth=requests.auth.HTTPBasicAuth(
280+
self.http_username,
281+
self.http_password))
274282
except Exception as e:
275283
raise GitlabConnectionError(
276284
"Can't connect to GitLab server (%s)" % e)
@@ -307,7 +315,10 @@ def _raw_post(self, path, data=None, content_type=None, **kwargs):
307315
return self.session.post(url, params=kwargs, data=data,
308316
headers=headers,
309317
verify=self.ssl_verify,
310-
timeout=self.timeout)
318+
timeout=self.timeout,
319+
auth=requests.auth.HTTPBasicAuth(
320+
self.http_username,
321+
self.http_password))
311322
except Exception as e:
312323
raise GitlabConnectionError(
313324
"Can't connect to GitLab server (%s)" % e)
@@ -320,7 +331,10 @@ def _raw_put(self, path, data=None, content_type=None, **kwargs):
320331
return self.session.put(url, data=data, params=kwargs,
321332
headers=headers,
322333
verify=self.ssl_verify,
323-
timeout=self.timeout)
334+
timeout=self.timeout,
335+
auth=requests.auth.HTTPBasicAuth(
336+
self.http_username,
337+
self.http_password))
324338
except Exception as e:
325339
raise GitlabConnectionError(
326340
"Can't connect to GitLab server (%s)" % e)
@@ -334,7 +348,10 @@ def _raw_delete(self, path, content_type=None, **kwargs):
334348
params=kwargs,
335349
headers=headers,
336350
verify=self.ssl_verify,
337-
timeout=self.timeout)
351+
timeout=self.timeout,
352+
auth=requests.auth.HTTPBasicAuth(
353+
self.http_username,
354+
self.http_password))
338355
except Exception as e:
339356
raise GitlabConnectionError(
340357
"Can't connect to GitLab server (%s)" % e)
@@ -374,11 +391,13 @@ def list(self, obj_class, **kwargs):
374391
# Also remove the next-url attribute that make queries fail
375392
if 'next_url' in params:
376393
del params['next_url']
377-
378394
try:
379395
r = self.session.get(url, params=params, headers=headers,
380396
verify=self.ssl_verify,
381-
timeout=self.timeout)
397+
timeout=self.timeout,
398+
auth=requests.auth.HTTPBasicAuth(
399+
self.http_username,
400+
self.http_password))
382401
except Exception as e:
383402
raise GitlabConnectionError(
384403
"Can't connect to GitLab server (%s)" % e)
@@ -445,7 +464,10 @@ def get(self, obj_class, id=None, **kwargs):
445464

446465
try:
447466
r = self.session.get(url, params=params, headers=headers,
448-
verify=self.ssl_verify, timeout=self.timeout)
467+
verify=self.ssl_verify, timeout=self.timeout,
468+
auth=requests.auth.HTTPBasicAuth(
469+
self.http_username,
470+
self.http_password))
449471
except Exception as e:
450472
raise GitlabConnectionError(
451473
"Can't connect to GitLab server (%s)" % e)

gitlab/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ def __init__(self, gitlab_id=None, config_files=None):
7878
self.timeout = self._config.getint(self.gitlab_id, 'timeout')
7979
except Exception:
8080
pass
81+
82+
self.http_username = None
83+
self.http_password = None
84+
try:
85+
self.http_username = self._config.get(self.gitlab_id,
86+
'http_username')
87+
self.http_password = self._config.get(self.gitlab_id,
88+
'http_password')
89+
except Exception:
90+
pass

0 commit comments

Comments
 (0)