summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2017-11-10 09:39:17 +0100
committerGauvain Pocentek <gauvain@pocentek.net>2017-11-10 09:39:17 +0100
commit07328263c317d7ee78723fee8b66f48abffcfb36 (patch)
tree1922a5ae37ed1903d1e02d35e725f6205081ac56
parente9b158363e5b0ea451638b1c3a660f138a24521d (diff)
downloadgitlab-07328263c317d7ee78723fee8b66f48abffcfb36.tar.gz
Add support for oauth and anonymous auth in config/CLI
-rw-r--r--docs/cli.rst10
-rw-r--r--gitlab/__init__.py3
-rw-r--r--gitlab/cli.py5
-rw-r--r--gitlab/config.py24
-rw-r--r--gitlab/tests/test_config.py27
5 files changed, 58 insertions, 11 deletions
diff --git a/docs/cli.rst b/docs/cli.rst
index e4d3437..f75a46a 100644
--- a/docs/cli.rst
+++ b/docs/cli.rst
@@ -70,8 +70,11 @@ parameters. You can override the values in each GitLab server section.
- Integer
- Number of seconds to wait for an answer before failing.
-You must define the ``url`` and ``private_token`` in each GitLab server
-section.
+You must define the ``url`` in each GitLab server section.
+
+Only one of ``private_token`` or ``oauth_token`` should be defined. If neither
+are defined an anonymous request will be sent to the Gitlab server, with very
+limited permissions.
.. list-table:: GitLab server options
:header-rows: 1
@@ -83,6 +86,9 @@ section.
* - ``private_token``
- Your user token. Login/password is not supported. Refer to `the official
documentation`__ to learn how to obtain a token.
+ * - ``oauth_token``
+ - An Oauth token for authentication. The Gitlab server must be configured
+ to support this authentication method.
* - ``api_version``
- GitLab API version to use (``3`` or ``4``). Defaults to ``3`` for now,
but will switch to ``4`` eventually.
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 5099349..c0f93bf 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -182,7 +182,8 @@ class Gitlab(object):
"""
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
config_files=config_files)
- return Gitlab(config.url, private_token=config.token,
+ return Gitlab(config.url, private_token=config.private_token,
+ oauth_token=config.oauth_token,
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password,
diff --git a/gitlab/cli.py b/gitlab/cli.py
index 1ab7d62..af82c09 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -86,7 +86,7 @@ def _get_base_parser():
help="Verbose mode (legacy format only)",
action="store_true")
parser.add_argument("-d", "--debug",
- help="Debug mode (display HTTP requests",
+ help="Debug mode (display HTTP requests)",
action="store_true")
parser.add_argument("-c", "--config-file", action='append',
help=("Configuration file to use. Can be used "
@@ -147,7 +147,8 @@ def main():
try:
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
- gl.auth()
+ if gl.private_token or gl.oauth_token:
+ gl.auth()
except Exception as e:
die(str(e))
diff --git a/gitlab/config.py b/gitlab/config.py
index d1c29d0..9cf208c 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -53,7 +53,6 @@ class GitlabConfigParser(object):
try:
self.url = self._config.get(self.gitlab_id, 'url')
- self.token = self._config.get(self.gitlab_id, 'private_token')
except Exception:
raise GitlabDataError("Impossible to get gitlab informations from "
"configuration (%s)" % self.gitlab_id)
@@ -96,6 +95,29 @@ class GitlabConfigParser(object):
except Exception:
pass
+ self.private_token = None
+ try:
+ self.private_token = self._config.get(self.gitlab_id,
+ 'private_token')
+ except Exception:
+ pass
+
+ self.oauth_token = None
+ try:
+ self.oauth_token = self._config.get(self.gitlab_id, 'oauth_token')
+ 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
+
self.http_username = None
self.http_password = None
try:
diff --git a/gitlab/tests/test_config.py b/gitlab/tests/test_config.py
index 83d7daa..271fa0b 100644
--- a/gitlab/tests/test_config.py
+++ b/gitlab/tests/test_config.py
@@ -45,6 +45,10 @@ timeout = 10
url = https://three.url
private_token = MNOPQR
ssl_verify = /path/to/CA/bundle.crt
+
+[four]
+url = https://four.url
+oauth_token = STUV
"""
no_default_config = u"""[global]
@@ -85,8 +89,7 @@ class TestConfigParser(unittest.TestCase):
fd = six.StringIO(missing_attr_config)
fd.close = mock.Mock(return_value=None)
m_open.return_value = fd
- self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
- gitlab_id='one')
+ config.GitlabConfigParser('one')
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
gitlab_id='two')
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
@@ -101,7 +104,8 @@ class TestConfigParser(unittest.TestCase):
cp = config.GitlabConfigParser()
self.assertEqual("one", cp.gitlab_id)
self.assertEqual("http://one.url", cp.url)
- self.assertEqual("ABCDEF", cp.token)
+ self.assertEqual("ABCDEF", cp.private_token)
+ self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual(True, cp.ssl_verify)
@@ -111,7 +115,8 @@ class TestConfigParser(unittest.TestCase):
cp = config.GitlabConfigParser(gitlab_id="two")
self.assertEqual("two", cp.gitlab_id)
self.assertEqual("https://two.url", cp.url)
- self.assertEqual("GHIJKL", cp.token)
+ self.assertEqual("GHIJKL", cp.private_token)
+ self.assertEqual(None, cp.oauth_token)
self.assertEqual(10, cp.timeout)
self.assertEqual(False, cp.ssl_verify)
@@ -121,6 +126,18 @@ class TestConfigParser(unittest.TestCase):
cp = config.GitlabConfigParser(gitlab_id="three")
self.assertEqual("three", cp.gitlab_id)
self.assertEqual("https://three.url", cp.url)
- self.assertEqual("MNOPQR", cp.token)
+ self.assertEqual("MNOPQR", cp.private_token)
+ self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
+
+ fd = six.StringIO(valid_config)
+ fd.close = mock.Mock(return_value=None)
+ m_open.return_value = fd
+ cp = config.GitlabConfigParser(gitlab_id="four")
+ self.assertEqual("four", cp.gitlab_id)
+ self.assertEqual("https://four.url", cp.url)
+ self.assertEqual(None, cp.private_token)
+ self.assertEqual("STUV", cp.oauth_token)
+ self.assertEqual(2, cp.timeout)
+ self.assertEqual(True, cp.ssl_verify)