/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.SessionSecurityToken
 */
package com.l7tech.server.identity;

import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.SessionSecurityToken;
import com.l7tech.server.identity.AuthenticationResult;

public class SessionAuthenticator {
    private final Goid a;
    private static final String[] b;

    public SessionAuthenticator(Goid goid) {
        this.a = goid;
    }

    public AuthenticationResult authenticateSessionCredentials(LoginCredentials loginCredentials, User user) throws BadCredentialsException {
        block15: {
            SecurityToken securityToken = loginCredentials.getSecurityToken();
            try {
                if (!(securityToken instanceof SessionSecurityToken)) {
                    throw new BadCredentialsException(b[1]);
                }
            }
            catch (BadCredentialsException badCredentialsException) {
                throw SessionAuthenticator.a(badCredentialsException);
            }
            SessionSecurityToken sessionSecurityToken = (SessionSecurityToken)securityToken;
            try {
                if (!sessionSecurityToken.getProviderId().equals((Object)this.a)) {
                    return null;
                }
            }
            catch (BadCredentialsException badCredentialsException) {
                throw SessionAuthenticator.a(badCredentialsException);
            }
            try {
                block14: {
                    try {
                        try {
                            try {
                                if (user.getId() == null && sessionSecurityToken.getUserId() != null) break block14;
                            }
                            catch (BadCredentialsException badCredentialsException) {
                                throw SessionAuthenticator.a(badCredentialsException);
                            }
                            if (user.getId() == null) break block15;
                        }
                        catch (BadCredentialsException badCredentialsException) {
                            throw SessionAuthenticator.a(badCredentialsException);
                        }
                        if (user.getId().equals(sessionSecurityToken.getUserId())) break block15;
                    }
                    catch (BadCredentialsException badCredentialsException) {
                        throw SessionAuthenticator.a(badCredentialsException);
                    }
                }
                throw new BadCredentialsException(b[0]);
            }
            catch (BadCredentialsException badCredentialsException) {
                throw SessionAuthenticator.a(badCredentialsException);
            }
        }
        return new AuthenticationResult(user, loginCredentials.getSecurityTokens());
    }

    public String getUserId(LoginCredentials loginCredentials) {
        SessionSecurityToken sessionSecurityToken;
        String string = null;
        SecurityToken securityToken = loginCredentials.getSecurityToken();
        if (securityToken instanceof SessionSecurityToken && (sessionSecurityToken = (SessionSecurityToken)securityToken).getProviderId().equals((Object)this.a)) {
            string = sessionSecurityToken.getUserId();
        }
        return string;
    }

    private static BadCredentialsException a(BadCredentialsException badCredentialsException) {
        return badCredentialsException;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "T{\u0006jH\u0005.'j\u001arD\u0004`kq\u0012pOJ$h{\u00069O\u00054's\u0014mB\u0002`nz\u0010wU\u00034~0\u0010Rp\u0010aQ\u000f#s{\u00119U\u0005+bp";
        int n3 = "T{\u0006jH\u0005.'j\u001arD\u0004`kq\u0012pOJ$h{\u00069O\u00054's\u0014mB\u0002`nz\u0010wU\u00034~0\u0010Rp\u0010aQ\u000f#s{\u00119U\u0005+bp".length();
        int n4 = 44;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = SessionAuthenticator.a(26, SessionAuthenticator.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                b = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x5A);
        }
        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 -> 29;
                    case 1 -> 4;
                    case 2 -> 111;
                    case 3 -> 3;
                    case 4 -> 59;
                    case 5 -> 112;
                    default -> 90;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

