Skip to content

Commit 5c03c04

Browse files
authored
Merge pull request #11 from ctjjbdev/develop/auth-headers
Update Zabbix API auth process to 6.4 and over
2 parents 740e1fd + cfe4097 commit 5c03c04

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ Released under the [GNU GPL License](https://github.com/miraclelinux/go-zabbix/b
9090
- Add `proxy.get`
9191
- Add `hostinterface.get`
9292
- Remove cover.run link from README.md
93+
- Add support for authorization via HTTP header for API versions >= 6.4

session.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,27 @@ func (c *Session) AuthToken() string {
119119
//
120120
// Generally Get or a wrapper function will be used instead of Do.
121121
func (c *Session) Do(req *Request) (resp *Response, err error) {
122-
// configure request
123-
req.AuthToken = c.Token
122+
version := 0.0
123+
if req.Method != "apiinfo.version" {
124+
// get Zabbix API version
125+
v, err := c.GetVersion()
126+
if err != nil {
127+
return nil, fmt.Errorf("Failed to retrieve Zabbix API version: %v", err)
128+
}
129+
130+
version, err = strconv.ParseFloat(v[0:3], 64)
131+
if err != nil {
132+
return nil, fmt.Errorf("Failed to convert Zabbix version string to float: %v", err)
133+
}
134+
135+
if version < 6.4 {
136+
// configure request
137+
req.AuthToken = c.Token
138+
} else {
139+
// don't send deprecated auth parameter
140+
req.AuthToken = ""
141+
}
142+
}
124143

125144
// encode request as json
126145
b, err := json.Marshal(req)
@@ -138,6 +157,12 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
138157
r.ContentLength = int64(len(b))
139158
r.Header.Add("Content-Type", "application/json-rpc")
140159

160+
if req.Method != "apiinfo.version" {
161+
if version >= 6.4 {
162+
r.Header.Add("Authorization", "Bearer " + c.Token)
163+
}
164+
}
165+
141166
// send request
142167
client := c.client
143168
if client == nil {

0 commit comments

Comments
 (0)