/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.webapp.impl.security.auth;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.NewCookie;

/**
 * <p>Util class allowing to obtain a Cookie from the current Authentications.</p>
 * 
 * <p>List of authentications is written as JSON object</p>
 * 
 * @author Daniel Meyer
 *
 */
public class AuthenticationCookie {
  
  public static final String AUTH_COOKIE_NAME = "CAM-AUTH";
  
  public static NewCookie fromAuthentications(Authentications authentications, HttpServletRequest request) {
    
    final HttpSession session = request.getSession();
    
    String contextPath = session.getServletContext().getContextPath();    
    
    int maxAge = session.getMaxInactiveInterval();
    if(maxAge <= 0) {
      maxAge = 60000;
    }
    
    String value = getCockieValue(authentications);
        
    return new NewCookie(AUTH_COOKIE_NAME, value, contextPath, null, null, maxAge, false);
  }

  private static String getCockieValue(Authentications authentications) {
    List<Authentication> authList = authentications.getAuthentications();
    StringBuilder cookieWriter = new StringBuilder();
    cookieWriter.append("{");    
    for(int i = 0; i < authList.size(); i++) {
      Authentication auth = authList.get(i);
      if(i > 0) {
        cookieWriter.append(",");  
      }
      cookieWriter.append("\"");
      cookieWriter.append(auth.getProcessEngineName());
      cookieWriter.append("\":\"");
      cookieWriter.append(auth.getIdentityId());
      cookieWriter.append("\"");
    }    
    cookieWriter.append("}");
    return cookieWriter.toString();
  }

}

@@ -1297,114 +1297,8 @@
    %0A
-    int maxAge = session.getMaxInactiveInterval();%0A    if(maxAge %3C= 0) %7B%0A      maxAge = 60000;%0A    %7D%0A    %0A
     
@@ -1433,14 +1433,25 @@
 ll, 
-maxAge
+Integer.MAX_VALUE
 , fa

increase cookie lifetime