Skip to content

Commit

Permalink
Merge pull request #39 from z4r/QueryStringAuth
Browse files Browse the repository at this point in the history
Query string auth
  • Loading branch information
z4r committed Dec 2, 2014
2 parents 80221db + 6615c76 commit ba1486d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.rst
Expand Up @@ -106,6 +106,22 @@ Cookie-based Authentication

resource = RTResource('http://<HOST>/REST/1.0/', '<USER>', '<PWD>', CookieAuthenticator)

QueryString Authentication
---------------------------

::

from rtkit.resource import RTResource
from rtkit.authenticators import QueryStringAuthenticator
from rtkit.errors import RTResourceError

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')

resource = RTResource('http://<HOST>/REST/1.0/', '<USER>', '<PWD>', QueryStringAuthenticator)

Kerberos Authentication
---------------------------

Expand Down
2 changes: 1 addition & 1 deletion rtkit/__init__.py
@@ -1,5 +1,5 @@
__author__ = 'Andrea De Marco <24erre@gmail.com>'
__version__ = '0.6.2'
__version__ = '0.7.0'
__classifiers__ = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down
44 changes: 44 additions & 0 deletions rtkit/authenticators.py
Expand Up @@ -4,6 +4,7 @@
* And the current implementations are:
* :py:class:`~rtkit.authenticators.BasicAuthenticator`
* :py:class:`~rtkit.authenticators.CookieAuthenticator`
* :py:class:`~rtkit.authenticators.QueryStringAuthenticator`
* :py:class:`~rtkit.authenticators.KerberosAuthenticator`
.. seealso::
Expand All @@ -16,10 +17,12 @@
import urllib
import urllib2
import cookielib
from urlparse import urlsplit, parse_qs, urlunsplit

__all__ = [
'BasicAuthenticator',
'CookieAuthenticator',
'QueryStringAuthenticator',
'KerberosAuthenticator',
]
if os.environ.get('__GEN_DOCS__', None):
Expand Down Expand Up @@ -112,6 +115,47 @@ def _login(self):
)


class QueryStringAuthenticator(AbstractAuthenticator):
"""Authenticate against server using a querystring
.. doctest::
from rtkit.resource import RTResource
from rtkit.authenticators import QueryStringAuthenticator
from rtkit.errors import RTResourceError
from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')
resource = RTResource('http://<HOST>/REST/1.0/', '<USER>', '<PWD>', QueryStringAuthenticator)
"""

def __init__(self, username, password, url):
super(QueryStringAuthenticator, self).__init__(username, password, url, QueryStringAuthHandler(username, password))


class QueryStringAuthHandler(urllib2.BaseHandler):
def __init__(self, username, password):
self.username = username
self.password = password

def default_open(self, request):
scheme, netloc, path, query_string, fragment = urlsplit(request.get_full_url())
query_params = parse_qs(query_string)
query_params['user'] = self.username
query_params['pass'] = self.password

request = urllib2.Request(
url=urlunsplit((scheme, netloc, path, urllib.urlencode(query_params, doseq=True), fragment)),
data=request.data,
headers=request.headers
)

return urllib2.urlopen(request)


class KerberosAuthenticator(AbstractAuthenticator):
"""Authenticate using Kerberos
Expand Down
22 changes: 21 additions & 1 deletion rtkit/tests/auth.py
Expand Up @@ -2,7 +2,7 @@
import unittest
from httpretty import httprettified, HTTPretty
from rtkit.resource import RTResource
from rtkit.authenticators import BasicAuthenticator, CookieAuthenticator
from rtkit.authenticators import BasicAuthenticator, CookieAuthenticator, QueryStringAuthenticator


class BasicAuthTestCase(unittest.TestCase):
Expand Down Expand Up @@ -60,3 +60,23 @@ def test_auth(self):
self.assertEqual(HTTPretty.latest_requests[1].path, '/ticket/1')
self.assertEqual(HTTPretty.latest_requests[1].method, 'GET')
self.assertEqual(HTTPretty.latest_requests[1].body, '')


class QueryStringAuthTestCase(unittest.TestCase):
def setUp(self):
self.resource = RTResource('http://rtkit.test/', 'USER', 'PASS', QueryStringAuthenticator)

@httprettified
def test_auth(self):
HTTPretty.register_uri(
HTTPretty.GET,
'http://rtkit.test/ticket/1',
responses=[
HTTPretty.Response(body='RT/3.8.10 200 Ok\n\n# Ticket 1 does not exist.\n\n\n', status=200),
]
)
self.resource.get(
path='ticket/1',
headers={'User-Agent': 'rtkit-ua', }
)
self.assertEqual(HTTPretty.latest_requests[0].path, '/ticket/1?user=USER&pass=PASS')
8 changes: 4 additions & 4 deletions setup.py
Expand Up @@ -15,7 +15,7 @@
version = pkg.__version__
classifiers = pkg.__classifiers__

readme = open(os.path.join(wd, 'README.rst'),'r').readlines()
readme = open(os.path.join(wd, 'README.rst'), 'r').readlines()
description = readme[1]
long_description = ''.join(readme)

Expand All @@ -35,8 +35,8 @@
description=description,
long_description=long_description,
classifiers=classifiers,
install_requires = reqs,
install_requires=reqs,
packages=find_packages(),
license = 'Apache License 2.0',
keywords ='RequestTracker REST',
license='Apache License 2.0',
keywords='RequestTracker REST',
)

0 comments on commit ba1486d

Please sign in to comment.