/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.User
 *  com.l7tech.identity.UserBean
 *  com.l7tech.identity.external.PolicyBackedIdentityProviderConfig
 *  com.l7tech.identity.external.VirtualPolicyUser
 *  com.l7tech.identity.ldap.LdapUser
 *  com.l7tech.message.Message
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.EntityHeaderSet
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.IdentityHeader
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.ResourceUtils
 *  jakarta.inject.Inject
 */
package com.l7tech.server.identity.external;

import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.User;
import com.l7tech.identity.UserBean;
import com.l7tech.identity.external.PolicyBackedIdentityProviderConfig;
import com.l7tech.identity.external.VirtualPolicyUser;
import com.l7tech.identity.ldap.LdapUser;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.EntityHeaderSet;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.IdentityHeader;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.identity.external.PolicyBackedIdentityProvider;
import com.l7tech.server.identity.external.PolicyBackedUserManager;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.PolicyEnforcementContextFactory;
import com.l7tech.server.policy.PolicyCache;
import com.l7tech.server.policy.ServerPolicyHandle;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.ResourceUtils;
import jakarta.inject.Inject;
import java.io.Closeable;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PolicyBackedUserManagerImpl
implements PolicyBackedUserManager {
    private static final Logger a;
    private PolicyBackedIdentityProviderConfig b;
    @Inject
    private PolicyCache policyCache;
    private static final String[] c;

    public VirtualPolicyUser findByPrimaryKey(String string) throws FindException {
        return this.findByLogin(string);
    }

    public VirtualPolicyUser findByLogin(String string) throws FindException {
        return this.a(string);
    }

    public void delete(VirtualPolicyUser virtualPolicyUser) throws DeleteException {
        throw new UnsupportedOperationException();
    }

    public void delete(String string) throws DeleteException, FindException {
        throw new UnsupportedOperationException();
    }

    public void deleteAll(Goid goid) throws DeleteException, FindException {
        throw new UnsupportedOperationException();
    }

    public void update(VirtualPolicyUser virtualPolicyUser) throws UpdateException, FindException {
        throw new UnsupportedOperationException();
    }

    public String save(VirtualPolicyUser virtualPolicyUser, Set<IdentityHeader> set) throws SaveException {
        throw new UnsupportedOperationException();
    }

    public String save(Goid goid, VirtualPolicyUser virtualPolicyUser, Set<IdentityHeader> set) throws SaveException {
        throw new UnsupportedOperationException();
    }

    public VirtualPolicyUser reify(UserBean userBean) {
        try {
            if (!userBean.getProviderId().equals((Object)this.b.getGoid())) {
                throw new IllegalArgumentException(c[1]);
            }
        }
        catch (UnsupportedOperationException unsupportedOperationException) {
            throw PolicyBackedUserManagerImpl.a(unsupportedOperationException);
        }
        VirtualPolicyUser virtualPolicyUser = this.a(userBean.getLogin());
        virtualPolicyUser.setProviderId(userBean.getProviderId());
        virtualPolicyUser.setLogin(userBean.getLogin());
        return virtualPolicyUser;
    }

    public void update(VirtualPolicyUser virtualPolicyUser, Set<IdentityHeader> set) throws UpdateException, FindException {
        throw new UnsupportedOperationException();
    }

    public EntityHeaderSet<IdentityHeader> search(String string) throws FindException {
        return new EntityHeaderSet();
    }

    public IdentityHeader userToHeader(VirtualPolicyUser virtualPolicyUser) {
        return new IdentityHeader(virtualPolicyUser.getProviderId(), virtualPolicyUser.getId(), EntityType.USER, virtualPolicyUser.getLogin(), virtualPolicyUser.getName(), null, null);
    }

    public VirtualPolicyUser headerToUser(IdentityHeader identityHeader) {
        VirtualPolicyUser virtualPolicyUser = this.a(identityHeader.getStrId());
        virtualPolicyUser.setProviderId(this.b.getGoid());
        virtualPolicyUser.setLogin(identityHeader.getStrId());
        return virtualPolicyUser;
    }

    public Class<? extends User> getImpClass() {
        return LdapUser.class;
    }

    public EntityHeaderSet<IdentityHeader> findAllHeaders() throws FindException {
        return new EntityHeaderSet();
    }

    @Override
    public void configure(PolicyBackedIdentityProvider policyBackedIdentityProvider) {
        this.b = (PolicyBackedIdentityProviderConfig)policyBackedIdentityProvider.getConfig();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public AuthenticationResult authenticatePasswordCredentials(LoginCredentials loginCredentials) throws BadCredentialsException {
        Goid goid = this.b.getPolicyId();
        Message message = new Message();
        Message message2 = new Message();
        PolicyEnforcementContext policyEnforcementContext = null;
        try {
            policyEnforcementContext = PolicyEnforcementContextFactory.createPolicyEnforcementContext(message, message2);
            String[] stringArray = c;
            policyEnforcementContext.setVariable(stringArray[6], loginCredentials.getLogin());
            policyEnforcementContext.setVariable(stringArray[0], new String(loginCredentials.getCredentials()));
            AssertionStatus assertionStatus = this.a(goid, policyEnforcementContext);
            String string = loginCredentials.getLogin();
            if (!AssertionStatus.NONE.equals(assertionStatus)) {
                String string2 = stringArray[3] + assertionStatus.getMessage() + stringArray[4] + string;
                a.info(string2);
                throw new BadCredentialsException(string2);
            }
            AuthenticationResult authenticationResult = new AuthenticationResult((User)this.a(string), loginCredentials.getSecurityTokens());
            return authenticationResult;
        }
        finally {
            ResourceUtils.closeQuietly((Closeable)policyEnforcementContext);
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private AssertionStatus a(Goid goid, PolicyEnforcementContext policyEnforcementContext) {
        try {
            ServerPolicyHandle serverPolicyHandle;
            block11: {
                AssertionStatus assertionStatus;
                serverPolicyHandle = this.policyCache.getServerPolicy(goid);
                try {
                    if (serverPolicyHandle != null) break block11;
                    String[] stringArray = c;
                    a.log(Level.WARNING, stringArray[7], new Object[]{this.b.getGoid(), goid});
                    assertionStatus = AssertionStatus.SERVER_ERROR;
                }
                catch (Throwable throwable) {
                    if (serverPolicyHandle == null) throw throwable;
                    try {
                        serverPolicyHandle.close();
                        throw throwable;
                    }
                    catch (Throwable throwable2) {
                        throwable.addSuppressed(throwable2);
                    }
                    throw throwable;
                }
                try {
                    if (serverPolicyHandle == null) return assertionStatus;
                    serverPolicyHandle.close();
                    return assertionStatus;
                }
                catch (Throwable throwable) {
                    throw PolicyBackedUserManagerImpl.a(throwable);
                }
            }
            AssertionStatus assertionStatus = serverPolicyHandle.checkRequest(policyEnforcementContext);
            try {
                if (serverPolicyHandle == null) return assertionStatus;
                serverPolicyHandle.close();
                return assertionStatus;
            }
            catch (Throwable throwable) {
                throw PolicyBackedUserManagerImpl.a(throwable);
            }
        }
        catch (Exception exception) {
            String[] stringArray = c;
            a.log(Level.WARNING, stringArray[5] + String.valueOf(this.b.getGoid()) + stringArray[2] + ExceptionUtils.getMessage((Throwable)exception), exception);
            return AssertionStatus.SERVER_ERROR;
        }
    }

    private VirtualPolicyUser a(String string) {
        return new VirtualPolicyUser(this.b.getGoid(), string);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[8];
                var3_1 = 0;
                var2_2 = "\b|6^\r\u000bu\u0012o)\u0002\u0019*4k#\u0002]\bc\u0000vf\u0014\u0012\u000fuAv)\u0004]\bc\rw(\u0017]\u001eiAl.\u0019\u000eJv\u0013w0\u0019\u0019\u000ft\"A5kP\u0018\u0018t\u000ejf\u0007\u0015\u0003j\u00048#\b\u0018\ts\u0015q(\u0017]\u001ai\rq%\tGJ#\u0003y%\u001b\u0014\u0004aAh)\u001c\u0014\t\u007fA~'\u0019\u0011\u000fbAo/\u0004\u0015Ju\u0015y2\u0005\u000eJ$\fC8 \u001f\u000fJj\u000e\u007f/\u001e]=4v'\u0012\u0011\u000f&\u0015wf\u0011\b\u001en\u0004v2\u0019\u001e\u000br\u000481\u0019\t\u0002&\u0011w*\u0019\u001e\u0013+\u0003y%\u001b\u0018\u000e&\b|#\u001e\t\u0003r\u001886\u0002\u0012\u001co\u0005}4P^";
                var4_3 = "\b|6^\r\u000bu\u0012o)\u0002\u0019*4k#\u0002]\bc\u0000vf\u0014\u0012\u000fuAv)\u0004]\bc\rw(\u0017]\u001eiAl.\u0019\u000eJv\u0013w0\u0019\u0019\u000ft\"A5kP\u0018\u0018t\u000ejf\u0007\u0015\u0003j\u00048#\b\u0018\ts\u0015q(\u0017]\u001ai\rq%\tGJ#\u0003y%\u001b\u0014\u0004aAh)\u001c\u0014\t\u007fA~'\u0019\u0011\u000fbAo/\u0004\u0015Ju\u0015y2\u0005\u000eJ$\fC8 \u001f\u000fJj\u000e\u007f/\u001e]=4v'\u0012\u0011\u000f&\u0015wf\u0011\b\u001en\u0004v2\u0019\u001e\u000br\u000481\u0019\t\u0002&\u0011w*\u0019\u001e\u0013+\u0003y%\u001b\u0018\u000e&\b|#\u001e\t\u0003r\u001886\u0002\u0012\u001co\u0005}4P^".length();
                var1_4 = 12;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 40;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = PolicyBackedUserManagerImpl.a(v0, PolicyBackedUserManagerImpl.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0012f,D\u0012\u0003y\tk8\u0087.l=\b\u000b\u0015<\u000fm|\u000b\u0012\u0004t\u001el(\u0003\u0004\u0011h\u001e\"+\u0003\u0013\u0018<\u000bm0\u0003\u0004\t1\u0019c?\u0001\u0002\u0014<\u0012f9\u0004\u0013\u0019h\u0002\",\u0018\b\u0006u\u001fg.JD\u000b,\u0006\"qGG\u001es[r3\u0006\u000e\u0013e[u5\u001e\u000fPU?\"\u007f\u0011V\r<\u0012q|\u001a\u0015\u0015o\u001el(J\u000e\u001e<\u000bm0\u0003\u0004\t<\u0018c?\u0002\u0002P4\u0012l*\u000b\u000b\u0019x[r3\u0006\u000e\u0013eD+";
                    var4_3 = "\u0012f,D\u0012\u0003y\tk8\u0087.l=\b\u000b\u0015<\u000fm|\u000b\u0012\u0004t\u001el(\u0003\u0004\u0011h\u001e\"+\u0003\u0013\u0018<\u000bm0\u0003\u0004\t1\u0019c?\u0001\u0002\u0014<\u0012f9\u0004\u0013\u0019h\u0002\",\u0018\b\u0006u\u001fg.JD\u000b,\u0006\"qGG\u001es[r3\u0006\u000e\u0013e[u5\u001e\u000fPU?\"\u007f\u0011V\r<\u0012q|\u001a\u0015\u0015o\u001el(J\u000e\u001e<\u000bm0\u0003\u0004\t<\u0018c?\u0002\u0002P4\u0012l*\u000b\u000b\u0019x[r3\u0006\u000e\u0013eD+".length();
                    var1_4 = 10;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 50;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = PolicyBackedUserManagerImpl.a(v0, PolicyBackedUserManagerImpl.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        PolicyBackedUserManagerImpl.c = var5;
        PolicyBackedUserManagerImpl.a = Logger.getLogger(PolicyBackedUserManagerImpl.class.getName());
    }

    private static Throwable a(Throwable throwable) {
        return throwable;
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x2E);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 73;
                    case 1 -> 48;
                    case 2 -> 110;
                    case 3 -> 88;
                    case 4 -> 85;
                    case 5 -> 66;
                    default -> 46;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

