-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitsession.go
56 lines (46 loc) · 1.52 KB
/
initsession.go
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
package polyanalyst6api
import (
"fmt"
"io/ioutil"
"net/http"
)
// InitSession is used to start a session
func InitSession(server *Server, version string, login string, password string, useLDAP bool, LDAPServer string) (Session, error) {
var session Session
supports, err := server.SupportsAPIVersion(version)
if err != nil {
return session, fmt.Errorf("failed to check if the passed API version is supported: %s", err)
}
if !supports {
return session, fmt.Errorf("the server doesn't support API of version %s", version)
}
url := server.BaseURL() + fmt.Sprintf("/v%s/login?uname=%s&pwd=%s&useLDAP=%s&svr=%s", version, login, password, boolToURLParam(useLDAP), LDAPServer)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return session, fmt.Errorf("building request error: %s", err)
}
client := &http.Client{
Timeout: RequestTimeout,
}
resp, err := client.Do(req)
if err != nil {
return session, fmt.Errorf("request execution error: %s", err)
}
defer closeBody(resp)
if resp.StatusCode != 200 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
msg := ""
if err != nil {
msg = "*failed to retrieve*"
} else {
msg = string(bodyBytes)
}
return session, fmt.Errorf("bad response status: %d. Error: %s", resp.StatusCode, msg)
}
for _, c := range resp.Cookies() {
if c.Name == "sid" {
return Session{SID: c.Value, Server: server, apiVersion: version}, nil
}
}
return session, fmt.Errorf("login response does not contain the sid")
}