@@ -439,6 +439,7 @@ def _auth_headers(self):
439439 :returns: A list of 2-tuples containing key and value
440440 """
441441 if self .cookie is not _NoAuthenticationToken :
442+ #TODO: update docs!
442443 return [("cookie" , self .cookie )]
443444 elif self .token is _NoAuthenticationToken :
444445 return []
@@ -533,6 +534,8 @@ def delete(self, path_segment, owner=None, app=None, sharing=None, **query):
533534 app = app , sharing = sharing )
534535 logging .debug ("DELETE request to %s (body: %s)" , path , repr (query ))
535536 response = self .http .delete (path , self ._auth_headers , ** query )
537+ if response .has_key ("Set-Cookie" ):
538+ self .cookie = response ["Set-Cookie" ]
536539 return response
537540
538541 @_authentication
@@ -591,6 +594,8 @@ def get(self, path_segment, owner=None, app=None, sharing=None, **query):
591594 app = app , sharing = sharing )
592595 logging .debug ("GET request to %s (body: %s)" , path , repr (query ))
593596 response = self .http .get (path , self ._auth_headers , ** query )
597+ if response .has_key ("Set-Cookie" ):
598+ self .cookie = response ["Set-Cookie" ]
594599 return response
595600
596601 @_authentication
@@ -664,6 +669,8 @@ def post(self, path_segment, owner=None, app=None, sharing=None, headers=None, *
664669 logging .debug ("POST request to %s (body: %s)" , path , repr (query ))
665670 all_headers = headers + self ._auth_headers
666671 response = self .http .post (path , all_headers , ** query )
672+ if response .has_key ("Set-Cookie" ):
673+ self .cookie = response ["Set-Cookie" ]
667674 return response
668675
669676 @_authentication
@@ -735,6 +742,8 @@ def request(self, path_segment, method="GET", headers=None, body="",
735742 {'method' : method ,
736743 'headers' : all_headers ,
737744 'body' : body })
745+ if response .has_key ("Set-Cookie" ):
746+ self .cookie = response ["Set-Cookie" ]
738747 return response
739748
740749 def login (self ):
@@ -756,26 +765,34 @@ def login(self):
756765 c = binding.Context(...).login()
757766 # Then issue requests...
758767 """
768+ # If self.cookie and self.token only, use the cookie
769+ if self .cookie is not _NoAuthenticationToken and \
770+ (not self .username and not self .password ):
771+ # If we were passed a session cookie, but no username or
772+ # password, then login is a nop, since we're automatically
773+ # logged in.
774+ return
775+
759776 if self .token is not _NoAuthenticationToken and \
760777 (not self .username and not self .password ):
761778 # If we were passed a session token, but no username or
762779 # password, then login is a nop, since we're automatically
763780 # logged in.
764781 return
782+
783+ # Only try to get a token and updated cookie if username & password are specified
765784 try :
766- #TODO: if self.cookie is set, should we even bother with trying to get a token?
767-
768785 response = self .http .post (
769786 self .authority + self ._abspath ("/services/auth/login" ),
770787 username = self .username ,
771788 password = self .password ,
772789 cookie = "1" ) # In Splunk 6.2+, passing "cookie=1" will return the "set-cookie" header
773790
774- # TODO: Should we return at this point, or also parse the token if we get a cookie?
791+ # Store the cookie
775792 for key , value in response .headers :
776- if key == "set-cookie" :
793+ if key . lower () == "set-cookie" :
777794 self .cookie = value
778- return self
795+ break
779796
780797 body = response .body .read ()
781798 session = XML (body ).findtext ("./sessionKey" )
0 commit comments