@@ -59,6 +59,7 @@ class Gitlab(object):
5959 Args:
6060 url (str): The URL of the GitLab server.
6161 private_token (str): The user private token
62+ oauth_token (str): An oauth token
6263 email (str): The user email or login.
6364 password (str): The user password (associated with email).
6465 ssl_verify (bool|str): Whether SSL certificates should be validated. If
@@ -82,16 +83,19 @@ def __init__(self, url, private_token=None, oauth_token=None, email=None,
8283 self .timeout = timeout
8384 #: Headers that will be used in request to GitLab
8485 self .headers = {}
85- self ._set_token (private_token , oauth_token )
8686
8787 #: The user email
8888 self .email = email
8989 #: The user password (associated with email)
9090 self .password = password
9191 #: Whether SSL certificates should be validated
9292 self .ssl_verify = ssl_verify
93+
94+ self .private_token = private_token
9395 self .http_username = http_username
9496 self .http_password = http_password
97+ self .oauth_token = oauth_token
98+ self ._set_auth_info ()
9599
96100 #: Create a session object for requests
97101 self .session = session or requests .Session ()
@@ -192,15 +196,12 @@ def auth(self):
192196 The `user` attribute will hold a `gitlab.objects.CurrentUser` object on
193197 success.
194198 """
195- if self .private_token :
199+ if self .private_token or self . oauth_token :
196200 self ._token_auth ()
197201 else :
198202 self ._credentials_auth ()
199203
200204 def _credentials_auth (self ):
201- if not self .email or not self .password :
202- raise GitlabAuthenticationError ("Missing email/password" )
203-
204205 data = {'email' : self .email , 'password' : self .password }
205206 if self .api_version == '3' :
206207 r = self ._raw_post ('/session' , json .dumps (data ),
@@ -211,8 +212,8 @@ def _credentials_auth(self):
211212 r = self .http_post ('/session' , data )
212213 manager = self ._objects .CurrentUserManager (self )
213214 self .user = self ._objects .CurrentUser (manager , r )
214-
215- self ._set_token ( self . user . private_token )
215+ self . private_token = self . user . private_token
216+ self ._set_auth_info ( )
216217
217218 def _token_auth (self ):
218219 if self .api_version == '3' :
@@ -267,18 +268,30 @@ def _construct_url(self, id_, obj, parameters, action=None):
267268 else :
268269 return url
269270
270- def _set_token (self , private_token , oauth_token = None ):
271- self .private_token = private_token if private_token else None
272- self .oauth_token = oauth_token if oauth_token else None
271+ def _set_auth_info (self ):
272+ if self .private_token and self .oauth_token :
273+ raise ValueError ("Only one of private_token or oauth_token should "
274+ "be defined" )
275+ if ((self .http_username and not self .http_password )
276+ or (not self .http_username and self .http_password )):
277+ raise ValueError ("Both http_username and http_password should "
278+ "be defined" )
279+ if self .oauth_token and self .http_username :
280+ raise ValueError ("Only one of oauth authentication or http "
281+ "authentication should be defined" )
282+
283+ self ._http_auth = None
284+ if self .private_token :
285+ self .headers ['PRIVATE-TOKEN' ] = self .private_token
286+ self .headers .pop ('Authorization' , None )
287+
288+ if self .oauth_token :
289+ self .headers ['Authorization' ] = "Bearer %s" % self .oauth_token
290+ self .headers .pop ('PRIVATE-TOKEN' , None )
273291
274- if private_token :
275- self .headers ["PRIVATE-TOKEN" ] = private_token
276- if 'Authorization' in self .headers :
277- del self .headers ["Authorization" ]
278- elif oauth_token :
279- self .headers ['Authorization' ] = "Bearer %s" % oauth_token
280- if "PRIVATE-TOKEN" in self .headers :
281- del self .headers ["PRIVATE-TOKEN" ]
292+ if self .http_username :
293+ self ._http_auth = requests .auth .HTTPBasicAuth (self .http_username ,
294+ self .http_password )
282295
283296 def enable_debug (self ):
284297 import logging
@@ -300,16 +313,10 @@ def _create_headers(self, content_type=None):
300313 request_headers ['Content-type' ] = content_type
301314 return request_headers
302315
303- def _create_auth (self ):
304- if self .http_username and self .http_password :
305- return requests .auth .HTTPBasicAuth (self .http_username ,
306- self .http_password )
307- return None
308-
309316 def _get_session_opts (self , content_type ):
310317 return {
311318 'headers' : self ._create_headers (content_type ),
312- 'auth' : self ._create_auth () ,
319+ 'auth' : self ._http_auth ,
313320 'timeout' : self .timeout ,
314321 'verify' : self .ssl_verify
315322 }
0 commit comments