| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b15f17b commit f8080db
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,9 @@ To connect to a GitLab server, create a ``gitlab.Gitlab`` object: | |||
| 24 | 24 | # private token authentication | |
| 25 | 25 | gl = gitlab.Gitlab('http://10.0.0.1', 'JVNSESs8EwWRx5yDxM5q') | |
| 26 | 26 | ||
| 27 | + # oauth authentication | ||
| 28 | + gl = gitlab.Gitlab('http://10.0.0.1', my_oauth2_token) | ||
| 29 | + | ||
| 27 | 30 | # or username/password authentication | |
| 28 | 31 | gl = gitlab.Gitlab('http://10.0.0.1', email='jdoe', password='s3cr3t') | |
| 29 | 32 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,6 +58,7 @@ class Gitlab(object): | |||
| 58 | 58 | Args: | |
| 59 | 59 | url (str): The URL of the GitLab server. | |
| 60 | 60 | private_token (str): The user private token | |
| 61 | + oauth_token (str): An oauth token | ||
| 61 | 62 | email (str): The user email or login. | |
| 62 | 63 | password (str): The user password (associated with email). | |
| 63 | 64 | ssl_verify (bool): Whether SSL certificates should be validated. | |
@@ -146,16 +147,16 @@ class Gitlab(object): | |||
| 146 | 147 | todos (TodoManager): Manager for user todos | |
| 147 | 148 | """ | |
| 148 | 149 | ||
| 149 | - def __init__(self, url, private_token=None, email=None, password=None, | ||
| 150 | - ssl_verify=True, http_username=None, http_password=None, | ||
| 151 | - timeout=None): | ||
| 150 | + def __init__(self, url, private_token=None, oauth_token=None, email=None, | ||
| 151 | + password=None, ssl_verify=True, http_username=None, | ||
| 152 | + http_password=None, timeout=None): | ||
| 152 | 153 | ||
| 153 | 154 | self._url = '%s/api/v3' % url | |
| 154 | 155 | #: Timeout to use for requests to gitlab server | |
| 155 | 156 | self.timeout = timeout | |
| 156 | 157 | #: Headers that will be used in request to GitLab | |
| 157 | 158 | self.headers = {} | |
| 158 | - self.set_token(private_token) | ||
| 159 | + self.set_token(private_token, oauth_token) | ||
| 159 | 160 | #: The user email | |
| 160 | 161 | self.email = email | |
| 161 | 162 | #: The user password (associated with email) | |
@@ -239,7 +240,7 @@ def from_config(gitlab_id=None, config_files=None): | |||
| 239 | 240 | """ | |
| 240 | 241 | config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id, | |
| 241 | 242 | config_files=config_files) | |
| 242 | - return Gitlab(config.url, private_token=config.token, | ||
| 243 | + return Gitlab(config.url, private_token=config.token, oauth_token=None, | ||
| 243 | 244 | ssl_verify=config.ssl_verify, timeout=config.timeout, | |
| 244 | 245 | http_username=config.http_username, | |
| 245 | 246 | http_password=config.http_password) | |
@@ -252,7 +253,7 @@ def auth(self): | |||
| 252 | 253 | The `user` attribute will hold a `gitlab.objects.CurrentUser` object on | |
| 253 | 254 | success. | |
| 254 | 255 | """ | |
| 255 | - if self.private_token: | ||
| 256 | + if self.oauth_token or self.private_token: | ||
| 256 | 257 | self.token_auth() | |
| 257 | 258 | else: | |
| 258 | 259 | self.credentials_auth() | |
@@ -335,17 +336,35 @@ def _create_headers(self, content_type=None, headers={}): | |||
| 335 | 336 | request_headers['Content-type'] = content_type | |
| 336 | 337 | return request_headers | |
| 337 | 338 | ||
| 338 | - def set_token(self, token): | ||
| 339 | + def set_token(self, token=None, oauth_token=None): | ||
| 339 | 340 | """Sets the private token for authentication. | |
| 340 | 341 | ||
| 342 | + Only one of ``token`` and ``oauth_token`` should be provided. | ||
| 343 | + | ||
| 344 | + Raises: | ||
| 345 | + GitlabAuthenticationError: When both ``token`` and ``oauth_token`` | ||
| 346 | + are provided. | ||
| 347 | + | ||
| 341 | 348 | Args: | |
| 342 | - token (str): The private token. | ||
| 349 | + token (str): A private token. | ||
| 350 | + oauth_token (str): An oauth token. | ||
| 343 | 351 | """ | |
| 344 | 352 | self.private_token = token if token else None | |
| 345 | - if token: | ||
| 353 | + self.oauth_token = oauth_token if oauth_token else None | ||
| 354 | + | ||
| 355 | + if token is not None and oauth_token is not None: | ||
| 356 | + raise GitlabAuthenticationError("Private and OAuth token both " | ||
| 357 | + "provided: define only one") | ||
| 358 | + | ||
| 359 | + if oauth_token: | ||
| 360 | + self.headers.pop("PRIVATE-TOKEN", None) | ||
| 361 | + self.headers["Authorization"] = "Bearer: %s" % oauth_token | ||
| 362 | + elif token: | ||
| 363 | + self.headers.pop("Authorization", None) | ||
| 346 | 364 | self.headers["PRIVATE-TOKEN"] = token | |
| 347 | - elif "PRIVATE-TOKEN" in self.headers: | ||
| 348 | - del self.headers["PRIVATE-TOKEN"] | ||
| 365 | + else: | ||
| 366 | + self.headers.pop("PRIVATE-TOKEN", None) | ||
| 367 | + self.headers.pop("Authorization", None) | ||
| 349 | 368 | ||
| 350 | 369 | def set_credentials(self, email, password): | |
| 351 | 370 | """Sets the email/login and password for authentication. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments