Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion zabbix/zabbix_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class APITimeout(ZabbixAPIException):
class ZabbixAPI(object):
__username__ = ''
__password__ = ''
__tokenauth__ = False

auth = ''
url = '/api_jsonrpc.php'
Expand Down Expand Up @@ -185,7 +186,16 @@ def json_obj(self, method, params={}, auth=True):

return json.dumps(obj)

def login(self, user='', password='', save=True):
def login(self, user='', password='', save=True, api_token=None):
if api_token is not None:
# due to ZBX-21688 we are unable to check if the token is valid
# obj = self.json_obj('user.checkAuthentication', {'sessionid': api_token}, auth=False)
# result = self.do_request(obj)
self.debug(logging.DEBUG, "Using API Token for auth")
self.auth=api_token
self.__tokenauth__ = True
return

if user != '':
l_user = user
l_password = password
Expand All @@ -208,6 +218,14 @@ def login(self, user='', password='', save=True):
self.auth = result['result']

def logout(self):
if self.__tokenauth__:
# Do nothing for logout for API tokens.
self.debug(logging.DEBUG, "Clearing auth information due to use of API Token")
self.auth = ''
self.__username__ = ''
self.__password__ = ''
self.__tokenauth__ = False
return
if self.auth == '':
raise ZabbixAPIException("No authentication information available.")
self.debug(logging.DEBUG, "Trying to logout user: %s." % self.__username__)
Expand Down