Skip to content

Commit d863ea7

Browse files
committed
Merge pull request splunk#133 from splunk/feature/basic_auth
Add Basic Auth support
2 parents 441f017 + da6f56d commit d863ea7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

splunklib/binding.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import sys
3434
import Cookie
3535

36+
from base64 import b64encode
3637
from datetime import datetime
3738
from functools import wraps
3839
from StringIO import StringIO
@@ -471,6 +472,7 @@ def __init__(self, handler=None, **kwargs):
471472
self.namespace = namespace(**kwargs)
472473
self.username = kwargs.get("username", "")
473474
self.password = kwargs.get("password", "")
475+
self.basic = kwargs.get("basic", False)
474476
self.autologin = kwargs.get("autologin", False)
475477

476478
# Store any cookies in the self.http._cookies dict
@@ -507,6 +509,9 @@ def _auth_headers(self):
507509
"""
508510
if self.has_cookies():
509511
return [("Cookie", _make_cookie_header(self.get_cookies().items()))]
512+
elif self.basic and (self.username and self.password):
513+
token = 'Basic %s' % b64encode("%s:%s" % (self.username, self.password))
514+
return [("Authorization", token)]
510515
elif self.token is _NoAuthenticationToken:
511516
return []
512517
else:
@@ -838,6 +843,11 @@ def login(self):
838843
# logged in.
839844
return
840845

846+
if self.basic and (self.username and self.password):
847+
# Basic auth mode requested, so this method is a nop as long
848+
# as credentials were passed in.
849+
return
850+
841851
# Only try to get a token and updated cookie if username & password are specified
842852
try:
843853
response = self.http.post(

tests/test_binding.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,32 @@ def test_namespace(self):
686686
def test_namespace_fails(self):
687687
self.assertRaises(ValueError, binding.namespace, sharing="gobble")
688688

689+
class TestBasicAuthentication(unittest.TestCase):
690+
def setUp(self):
691+
self.opts = testlib.parse([], {}, ".splunkrc")
692+
opts = self.opts.kwargs.copy()
693+
opts["basic"] = True
694+
opts["username"] = self.opts.kwargs["username"]
695+
opts["password"] = self.opts.kwargs["password"]
696+
697+
self.context = binding.connect(**opts)
698+
import splunklib.client as client
699+
service = client.Service(**opts)
700+
701+
if getattr(unittest.TestCase, 'assertIsNotNone', None) is None:
702+
def assertIsNotNone(self, obj, msg=None):
703+
if obj is None:
704+
raise self.failureException, (msg or '%r is not None' % obj)
705+
706+
def test_basic_in_auth_headers(self):
707+
self.assertIsNotNone(self.context._auth_headers)
708+
self.assertNotEqual(self.context._auth_headers, [])
709+
self.assertEqual(len(self.context._auth_headers), 1)
710+
self.assertEqual(len(self.context._auth_headers), 1)
711+
self.assertEqual(self.context._auth_headers[0][0], "Authorization")
712+
self.assertEqual(self.context._auth_headers[0][1][:6], "Basic ")
713+
self.assertEqual(self.context.get("/services").status, 200)
714+
689715
class TestTokenAuthentication(BindingTestCase):
690716
def test_preexisting_token(self):
691717
token = self.context.token

0 commit comments

Comments
 (0)