/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.action;

import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.jsf2.service.FacesService;
import org.gluu.oxtrust.security.Identity;
import org.gluu.oxtrust.security.OauthData;
import org.gluu.oxtrust.service.OpenIdService;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.jboss.resteasy.client.ClientRequest;
import org.slf4j.Logger;
import org.xdi.config.oxtrust.AppConfiguration;
import org.xdi.util.StringHelper;

/**
 * Provides OP/RP-initiated logout functionality
 * 
 * @author Yuriy Movchan Date: 021/25/2019
 */
@Named("logoutAction")
@RequestScoped
public class LogoutAction implements Serializable {

    private static final long serialVersionUID = -1887682170119210113L;

	@Inject
	private Logger log;

	@Inject
	private Identity identity;

    @Inject
    private FacesService facesService;

    @Inject
    private OpenIdService openIdService;

    @Inject
    private AppConfiguration appConfiguration;

	public void processLogout() throws Exception {
	    opLogout();

        identity.logout();
	}

	public void processSsoLogout() throws Exception {
        identity.logout();
    }

	public String postLogout() {
        identity.logout();

		return OxTrustConstants.RESULT_SUCCESS;
	}

	protected void opLogout() throws Exception {
		boolean requireOpLogout = isRequireOpLogout();
		if (!requireOpLogout) {
		    return;
		}

		OauthData oauthData = identity.getOauthData();

		ClientRequest clientRequest = new ClientRequest(openIdService.getOpenIdConfiguration().getEndSessionEndpoint());

		clientRequest.queryParameter(OxTrustConstants.OXAUTH_SESSION_STATE, oauthData.getSessionState());
		clientRequest.queryParameter(OxTrustConstants.OXAUTH_ID_TOKEN_HINT, oauthData.getIdToken());
		clientRequest.queryParameter(OxTrustConstants.OXAUTH_POST_LOGOUT_REDIRECT_URI,
				appConfiguration.getLogoutRedirectUrl());

		facesService.redirectToExternalURL(clientRequest.getUri());
	}

	private boolean isRequireOpLogout() {
        OauthData oauthData = identity.getOauthData();
		if (StringHelper.isEmpty(oauthData.getUserUid())) {
			return false;
		}

		return true;
    }

}

@@ -1526,102 +1526,8 @@
 n %7B%0A
-%09%09boolean requireOpLogout = isRequireOpLogout();%0A%09%09if (!requireOpLogout) %7B%0A%09%09    return;%0A%09%09%7D%0A%0A
 %09%09Oa
@@ -1690,16 +1690,65 @@
 ());%0A%0A%09%09
+if (oauthData.getSessionState() != null) %7B%0A%09%09    
 clientRe
@@ -1835,24 +1835,84 @@
 State());%0A%09%09
+%7D%0A        if (oauthData.getIdToken() != null) %7B%0A            
 clientReques
@@ -1992,16 +1992,26 @@
 ken());%0A
+        %7D%0A
 %09%09client
@@ -2200,198 +2200,6 @@
 %09%7D%0A%0A
-%09private boolean isRequireOpLogout() %7B%0A        OauthData oauthData = identity.getOauthData();%0A%09%09if (StringHelper.isEmpty(oauthData.getUserUid())) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%0A%09%09return true;%0A    %7D%0A%0A
 %7D%0A

Allow logout if id_token expired by session is active