in cmd/network.go [53:102]
func Login(r *Request) (string, error) {
params := make(url.Values)
params.Add("command", "login")
params.Add("username", r.Config.ActiveProfile.Username)
params.Add("password", r.Config.ActiveProfile.Password)
params.Add("domain", r.Config.ActiveProfile.Domain)
params.Add("response", "json")
msURL, _ := url.Parse(r.Config.ActiveProfile.URL)
if sessionCookie := findSessionCookie(r.Client().Jar.Cookies(msURL)); sessionCookie != nil {
return sessionCookie.Value, nil
}
config.Debug("Login POST URL:", msURL, params)
spinner := r.Config.StartSpinner("trying to log in...")
resp, err := r.Client().PostForm(msURL.String(), params)
r.Config.StopSpinner(spinner)
if err != nil {
return "", errors.New("failed to authenticate with the CloudStack server, please check the settings: " + err.Error())
}
config.Debug("Login POST response status code:", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
e := errors.New("failed to authenticate, please check the credentials")
if err != nil {
e = errors.New("failed to authenticate due to " + err.Error())
}
return "", e
}
var sessionKey string
curTime := time.Now()
expiryDuration := 15 * time.Minute
for _, cookie := range resp.Cookies() {
if cookie.Expires.After(curTime) {
expiryDuration = cookie.Expires.Sub(curTime)
}
if cookie.Name == "sessionkey" {
sessionKey = cookie.Value
}
}
go func() {
time.Sleep(expiryDuration)
r.Client().Jar, _ = cookiejar.New(nil)
}()
config.Debug("Login sessionkey:", sessionKey)
return sessionKey, nil
}