Skip to content

Commit 46b2537

Browse files
Fix authentication handling for bearer tokens and improve session reload logic.
1 parent b5c0e52 commit 46b2537

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

Nagstamon/servers/Zabbix.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ def api_request(self, cgi_data, no_auth=False):
119119

120120
def login(self):
121121
if conf.servers[self.get_name()].authentication == 'bearer':
122-
return
122+
# In bearer mode the GUI stores the API token in the password field
123+
token = self.password
124+
if token:
125+
self.session.auth = BearerAuth(token)
126+
self.refresh_authentication = False
127+
return
123128

124129
# check version to use the correct keyword for username which changed since 6.4
125130
if version.parse(self.api_version) < version.parse("6.4"):
@@ -130,9 +135,9 @@ def login(self):
130135
obj = self.generate_cgi_data('user.login', {username_keyword: self.username, 'password': self.password}, no_auth=True)
131136
result = self.api_request(obj)
132137
self.auth_token = result['result'] # Store the auth token for later use
133-
# Use bearer authentication for Zabbix versions 6.4 and above
134-
self.authentication = "bearer" if version.parse(self.api_version) >= version.parse("6.4") else "basic"
135-
self.session.auth = BearerAuth(self.auth_token)
138+
# For Zabbix >= 6.4 the server expects HTTP Bearer and no JSON-RPC auth field
139+
if version.parse(self.api_version) >= version.parse("6.4"):
140+
self.session.auth = BearerAuth(self.auth_token)
136141
self.refresh_authentication = False # Reset the flag after successful login
137142

138143
def check_authentication(self):
@@ -488,4 +493,4 @@ def nagiosify_service(self, service):
488493
if (" on " or " is ") in service:
489494
for separator in [" on ", " is "]:
490495
service = service.split(separator)[0]
491-
return service
496+
return service

0 commit comments

Comments
 (0)