cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ro...@apache.org
Subject [cloudstack-cloudmonkey] branch master updated: network: expire cookie jar based on initial log-in cookies
Date Mon, 25 Jun 2018 18:47:36 GMT
This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/master by this push:
     new 98b3aa7  network: expire cookie jar based on initial log-in cookies
98b3aa7 is described below

commit 98b3aa7745ae3122b3df4655652ba19648508d9d
Author: Rohit Yadav <rohit@apache.org>
AuthorDate: Tue Jun 26 00:17:16 2018 +0530

    network: expire cookie jar based on initial log-in cookies
    
    Signed-off-by: Rohit Yadav <rohit@apache.org>
---
 cmd/network.go | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/cmd/network.go b/cmd/network.go
index 67d08e6..37e128e 100644
--- a/cmd/network.go
+++ b/cmd/network.go
@@ -27,21 +27,20 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"net/http/cookiejar"
 	"net/url"
 	"sort"
 	"strings"
 	"time"
 )
 
-func findSessionCookie(cookies []*http.Cookie) string {
-	var sessionKey string = ""
+func findSessionCookie(cookies []*http.Cookie) *http.Cookie {
 	for _, cookie := range cookies {
 		if cookie.Name == "sessionkey" {
-			sessionKey = cookie.Value
-			break
+			return cookie
 		}
 	}
-	return sessionKey
+	return nil
 }
 
 // Login logs in a user based on provided request and returns http client and session key
@@ -54,8 +53,8 @@ func Login(r *Request) (string, error) {
 	params.Add("response", "json")
 
 	url, _ := url.Parse(r.Config.ActiveProfile.URL)
-	if sessionKey := findSessionCookie(r.Client().Jar.Cookies(url)); sessionKey != "" {
-		return sessionKey, nil
+	if sessionCookie := findSessionCookie(r.Client().Jar.Cookies(url)); sessionCookie != nil
{
+		return sessionCookie.Value, nil
 	}
 
 	spinner := r.Config.StartSpinner("trying to log in...")
@@ -74,7 +73,22 @@ func Login(r *Request) (string, error) {
 		return "", e
 	}
 
-	return findSessionCookie(resp.Cookies()), nil
+	var sessionKey string
+	curTime := time.Now()
+	for _, cookie := range resp.Cookies() {
+		if cookie.Expires.After(curTime) {
+			expiryDuration := cookie.Expires.Sub(curTime)
+			go func() {
+				time.Sleep(expiryDuration)
+				r.Client().Jar, _ = cookiejar.New(nil)
+			}()
+		}
+		if cookie.Name == "sessionkey" {
+			sessionKey = cookie.Value
+		}
+	}
+
+	return sessionKey, nil
 }
 
 func encodeRequestParams(params url.Values) string {


Mime
View raw message