Update response Cookie value in proxy

The proxy is receiving two cookies (AWSALB and JSESSIONID) from backend response like below.

AWSALB=lnTSVTHpyba6/qH2QWVOVQWEuJjQi1kBnpBP5yt/l628OSKMZtEmZaGHFQojsda11Tl7cn2lOqmXAEWpTsaMyFcd6oPYNAmGZCKpc7BbGi4B+8ZQzoxw0SqbFqw; Expires=Thu,26 Jan 2020 12:21:46 GMT; Path=/,JSESSIONID=B1D80E6395A35D4210B9F254835CC627; Path=/ED; HttpOnly

We are extracting cookie value like below.

var hdr = context.getVariable('response.header.set-cookie.values')+'';
var str = hdr.substring(1, hdr.length-1).split(',');

In proxy we need to update path value of cookie JSESSIONID from /ED to / (Path=/ED; to Path=/;). This is required as our proxy base path starts with /digital. We updated the cookie value using java script and set the cookie using context.setVariable('response.header.set-cookie', <updated cookie>); But it is setting only first value.

We tried to set two cookies using two separate headers like below.

context.setVariable('response.header.set-cookie.1', <aws cookie>);

context.setVariable('response.header.set-cookie.2', <updated jsession cookie>);

But it also setting only the first cookie. I think comma (,) in Expires attribute (Expires=Thu,26 Jan 2020 12:21:46 GMT;) is causing is issue.

Let me know if any other approach to update the cookie value as per above requirement.

@Dino-at-Google

Solved Solved
1 2 1,252
1 ACCEPTED SOLUTION

Following logic/steps resolved the issue

  • Extract set-cookie values from response
  • If cookie contains "Expires= "string then extract the expiry date, convert it to Max-Age as shown in below code snippet.
	if(cookie.includes("Expires=")){	
	 //Assumption Cookie date time always ends with GMT or UTC   
    	var regex1 = /Expires=[a-zA-Z0-9,:/\- ?]*[GMT|UTC]/g;	    
		
    	var find = cookie.match(regex1);
    	var currentDate = new Date();

    	for(i=0; i< find.length; i++){
    		var cookieDt = new Date(find[i].slice(8));    		
    		var maxAge = Math.round((cookieDt - currentDate)/1000) ;
     		maxAge = "Max-Age="+maxAge;
    		regex2 = new RegExp(find[i], "g");
    		cookie = cookie.replace(regex2, maxAge);
	    }
	}
  • Split cookie string by comma parameter (cookie.split(',')) & update the path (path=/digital) in each cookie.
  • Set each cookie value in a separate response Set-Cookie header flow variable ( Ex: response.header.Set-Cookie.1, response.header.Set-Cookie.2 etc)

View solution in original post

2 REPLIES 2

Following logic/steps resolved the issue

  • Extract set-cookie values from response
  • If cookie contains "Expires= "string then extract the expiry date, convert it to Max-Age as shown in below code snippet.
	if(cookie.includes("Expires=")){	
	 //Assumption Cookie date time always ends with GMT or UTC   
    	var regex1 = /Expires=[a-zA-Z0-9,:/\- ?]*[GMT|UTC]/g;	    
		
    	var find = cookie.match(regex1);
    	var currentDate = new Date();

    	for(i=0; i< find.length; i++){
    		var cookieDt = new Date(find[i].slice(8));    		
    		var maxAge = Math.round((cookieDt - currentDate)/1000) ;
     		maxAge = "Max-Age="+maxAge;
    		regex2 = new RegExp(find[i], "g");
    		cookie = cookie.replace(regex2, maxAge);
	    }
	}
  • Split cookie string by comma parameter (cookie.split(',')) & update the path (path=/digital) in each cookie.
  • Set each cookie value in a separate response Set-Cookie header flow variable ( Ex: response.header.Set-Cookie.1, response.header.Set-Cookie.2 etc)

That sounds correct to me. You'll need to use JS to do cookie remapping in Apigee. There's no builtin function that does this.

For an alternative, here's a gist showing recursive descent parsing of cookies in JS. You don't need to replace Expires if you use this approach.