/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.CredentialFormat
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.xmlsec.AddWssSecurityToken
 *  com.l7tech.security.token.UsernameToken
 *  com.l7tech.security.token.UsernameTokenImpl
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.xml.saml.SamlAssertion
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.CredentialFormat;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.xmlsec.AddWssSecurityToken;
import com.l7tech.security.token.UsernameToken;
import com.l7tech.security.token.UsernameTokenImpl;
import com.l7tech.security.xml.decorator.DecorationRequirements;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.xmlsec.AddWssSignatureSupport;
import com.l7tech.server.policy.assertion.xmlsec.ServerAddWssSecurityToken;
import com.l7tech.xml.saml.SamlAssertion;
import java.security.cert.X509Certificate;
import org.w3c.dom.Document;

class p
implements AddWssSignatureSupport.SignedElementSelector {
    final ServerAddWssSecurityToken this$0;

    p(ServerAddWssSecurityToken serverAddWssSecurityToken) {
        this.this$0 = serverAddWssSecurityToken;
    }

    @Override
    public int selectElementsToSign(PolicyEnforcementContext policyEnforcementContext, AuthenticationContext authenticationContext, Document document, DecorationRequirements decorationRequirements, Message message) throws PolicyAssertionException {
        char[] cArray;
        String string;
        block21: {
            block20: {
                block22: {
                    X509Certificate x509Certificate;
                    SamlAssertion samlAssertion;
                    Object object;
                    LoginCredentials loginCredentials;
                    block19: {
                        String string2;
                        loginCredentials = policyEnforcementContext.getDefaultAuthenticationContext().getLastCredentials();
                        try {
                            string2 = loginCredentials == null ? null : loginCredentials.getLogin();
                        }
                        catch (PolicyAssertionException policyAssertionException) {
                            throw p.a(policyAssertionException);
                        }
                        string = string2;
                        cArray = null;
                        try {
                            if (loginCredentials == null || loginCredentials.getFormat() != CredentialFormat.CLEARTEXT) break block19;
                        }
                        catch (PolicyAssertionException policyAssertionException) {
                            throw p.a(policyAssertionException);
                        }
                        cArray = loginCredentials.getCredentials();
                        break block22;
                    }
                    try {
                        Object object2 = object = loginCredentials == null ? null : loginCredentials.getPayload();
                    }
                    catch (PolicyAssertionException policyAssertionException) {
                        throw p.a(policyAssertionException);
                    }
                    if (object instanceof X509Certificate) {
                        X509Certificate x509Certificate2 = (X509Certificate)object;
                        string = x509Certificate2.getSubjectDN().getName();
                    } else if (object instanceof SamlAssertion && (string = (samlAssertion = (SamlAssertion)object).getNameIdentifierValue()) == null && (x509Certificate = samlAssertion.getSubjectCertificate()) != null) {
                        string = x509Certificate.getSubjectDN().getName();
                    }
                }
                try {
                    if (string == null) {
                        ServerAddWssSecurityToken.access$000(this.this$0, (AuditDetailMessage)AssertionMessages.ADD_WSS_TOKEN_NO_USERNAME);
                        return -1;
                    }
                }
                catch (PolicyAssertionException policyAssertionException) {
                    throw p.a(policyAssertionException);
                }
                try {
                    try {
                        if (!((AddWssSecurityToken)ServerAddWssSecurityToken.access$100(this.this$0)).isIncludePassword()) break block20;
                        if (cArray != null) break block21;
                    }
                    catch (PolicyAssertionException policyAssertionException) {
                        throw p.a(policyAssertionException);
                    }
                    ServerAddWssSecurityToken.access$200(this.this$0, (AuditDetailMessage)AssertionMessages.ADD_WSS_TOKEN_NO_PASSWORD);
                    return -1;
                }
                catch (PolicyAssertionException policyAssertionException) {
                    throw p.a(policyAssertionException);
                }
            }
            cArray = null;
        }
        try {
            decorationRequirements.setUsernameTokenCredentials((UsernameToken)new UsernameTokenImpl(string, cArray));
            if (((AddWssSecurityToken)ServerAddWssSecurityToken.access$300(this.this$0)).isProtectTokens()) {
                decorationRequirements.setSignUsernameToken(true);
                decorationRequirements.setSignTimestamp(true);
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw p.a(policyAssertionException);
        }
        return 1;
    }

    private static PolicyAssertionException a(PolicyAssertionException policyAssertionException) {
        return policyAssertionException;
    }
}

