-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
61 lines (52 loc) · 1.44 KB
/
Copy pathresponse.go
File metadata and controls
61 lines (52 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fortimgr
import (
"encoding/json"
"errors"
"fmt"
)
// flatUIResponse is the JSON envelope from /cgi-bin/module/forward.
type flatUIResponse struct {
Code int `json:"code"`
Data struct {
Result []flatUIResult `json:"result"`
} `json:"data"`
}
type flatUIResult struct {
Status struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"status"`
Data json.RawMessage `json:"data"`
}
// checkResponse validates the FlatUI response envelope.
// Returns the data payload from result[0] on success.
func checkResponse(resp *flatUIResponse) (json.RawMessage, error) {
if isSessionExpired(resp) {
return nil, ErrSessionExpired
}
if resp.Code != 0 {
return nil, &APIError{Code: resp.Code, Message: "transport error"}
}
if len(resp.Data.Result) == 0 {
return nil, errors.New("fortimgr: empty response")
}
status := resp.Data.Result[0].Status
if status.Code == -11 {
return nil, fmt.Errorf("%w: %s", ErrPermission, status.Message)
}
if status.Code != 0 {
return nil, &APIError{Code: status.Code, Message: status.Message}
}
return resp.Data.Result[0].Data, nil
}
// isSessionExpired checks if the response indicates a session timeout.
// FortiManager signals this with transport code -6 or result status code -6.
func isSessionExpired(resp *flatUIResponse) bool {
if resp.Code == -6 {
return true
}
if len(resp.Data.Result) > 0 && resp.Data.Result[0].Status.Code == -6 {
return true
}
return false
}