/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.HttpConstants
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.credential.CredentialFinderException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.http.HttpBasic
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.http.HttpBasicToken
 *  com.l7tech.util.Charsets
 *  com.l7tech.util.HexUtils
 */
package com.l7tech.server.policy.assertion.credential.http;

import com.l7tech.common.http.HttpConstants;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.credential.CredentialFinderException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.http.HttpBasic;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.http.HttpBasicToken;
import com.l7tech.server.policy.assertion.credential.http.ServerHttpCredentialSource;
import com.l7tech.util.Charsets;
import com.l7tech.util.HexUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;

public class ServerHttpBasic
extends ServerHttpCredentialSource<HttpBasic> {
    public static final Charset ENCODING;
    public static final String SCHEME;
    public static final String REALM;
    private static final String[] d;

    public ServerHttpBasic(HttpBasic httpBasic) {
        super(httpBasic);
    }

    @Override
    protected Map<String, String> challengeParams(Message message, Map<String, String> map) {
        return Collections.emptyMap();
    }

    @Override
    protected String scheme() {
        return d[3];
    }

    @Override
    public LoginCredentials findCredentials(Message message, Map<String, String> map) throws IOException, CredentialFinderException {
        String string = message.getHttpRequestKnob().getHeaderSingleValue(HttpConstants.HEADER_AUTHORIZATION);
        return this.findCredentials(string);
    }

    public LoginCredentials findCredentials(String string) throws IOException {
        block11: {
            try {
                try {
                    if (string != null && string.length() != 0) break block11;
                }
                catch (IOException iOException) {
                    throw ServerHttpBasic.a(iOException);
                }
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_NO_AUTHN_HEADER);
                return null;
            }
            catch (IOException iOException) {
                throw ServerHttpBasic.a(iOException);
            }
        }
        int n2 = string.indexOf(" ");
        try {
            if (n2 < 0) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_BAD_AUTHN_HEADER, d[2]);
                return null;
            }
        }
        catch (IOException iOException) {
            throw ServerHttpBasic.a(iOException);
        }
        String string2 = string.substring(0, n2);
        String string3 = string.substring(n2 + 1);
        try {
            if (!this.scheme().equals(string2)) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_NA_AUTHN_HEADER);
                return null;
            }
        }
        catch (IOException iOException) {
            throw ServerHttpBasic.a(iOException);
        }
        String string4 = new String(HexUtils.decodeBase64((String)string3, (boolean)true), ENCODING);
        int n3 = string4.indexOf(":");
        if (n3 >= 0) {
            String string5 = string4.substring(0, n3);
            String string6 = string4.substring(n3 + 1);
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_FOUND_USER, string5);
            return LoginCredentials.makeLoginCredentials((SecurityToken)new HttpBasicToken(string5, string6.toCharArray()), ((HttpBasic)this.assertion).getClass());
        }
        this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_BAD_AUTHN_HEADER, d[1]);
        return null;
    }

    @Override
    protected String realm() {
        String string = ((HttpBasic)this.assertion).getRealm();
        if (string == null) {
            string = d[0];
        }
        return string;
    }

    @Override
    protected AssertionStatus checkAuthParams(Map<String, String> map) {
        return AssertionStatus.NONE;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "\u00071}4QjW8oM5sIZ&\b%i\u000e\u0004yDY%\b%i\u000e\u0014fIU.\u0005\tg]\u000eu";
                var4_3 = "\u00071}4QjW8oM5sIZ&\b%i\u000e\u0004yDY%\b%i\u000e\u0014fIU.\u0005\tg]\u000eu".length();
                var1_4 = 15;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerHttpBasic.d(v0, ServerHttpBasic.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0016 l%@{F)~\\$bXK7\u0005\u0018vL\u001fd";
                    var4_3 = "\u0016 l%@{F)~\\$bXK7\u0005\u0018vL\u001fd".length();
                    var1_4 = 15;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 99;
                        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++] = ServerHttpBasic.d(v0, ServerHttpBasic.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;
            }
        }
        ServerHttpBasic.d = var5;
        ServerHttpBasic.SCHEME = ServerHttpBasic.d[5];
        ServerHttpBasic.REALM = ServerHttpBasic.d[4];
        ServerHttpBasic.ENCODING = Charsets.ISO8859;
    }

    private static IOException a(IOException iOException) {
        return iOException;
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String d(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 -> 57;
                    case 1 -> 116;
                    case 2 -> 92;
                    case 3 -> 21;
                    case 4 -> 100;
                    case 5 -> 90;
                    default -> 68;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

