<% jagg.template("sso/logout", function(inputs, outputs, jagg) { %>
 
<%
    //jagg.setUser(null);
    //response.sendRedirect("site/pages/list-apis.jag");

	var security =require("sso");
	var ssoRelyingParty = new security.SSORelyingParty(site.ssoConfiguration.issuer);
	
	if(site.ssoConfiguration.externalLogoutPage != null && site.ssoConfiguration.externalLogoutPage != 'null'
			&& site.ssoConfiguration.externalLogoutPage != ""){
		ssoRelyingParty.invalidateSessionBySessionId(session.getId());
		session.invalidate();
		
		response.sendRedirect(site.ssoConfiguration.externalLogoutPage);
		return;
	} else {
		var samlLogoutRequest = ssoRelyingParty.getSAMLLogoutRequest(session.get("LOGGED_IN_USER"),session.getId());
		var encodedRequest = ssoRelyingParty.encode(samlLogoutRequest);
		var relayState = ssoRelyingParty.getUUID();
		var idpURL = ssoRelyingParty.getProperty("identityProviderURL");
%>

		<div>
		   <p>You are now redirected to Stratos Identity. If the
		   redirection fails, please click the post button.</p>

			<form id="logoutForm" method="post" action="<%=idpURL%>">
			<p>
				<input type="hidden" name="SAMLRequest"
					  value="<%= encodedRequest %>"/>
				<input type="hidden" name="RelayState" value="<%= relayState %>"/>
				<button type="submit">POST</button>
			</p>
			</form>
		</div>
		<script type = "text/javascript" >
					   document.forms[0].submit();
		//        $(document).onload(function(){$('#logoutForm').submit();});

		</script>
<%
	}
%>
       
<% }); %>
