package com.amplifyframework.api.aws.auth;

import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import kotlin.Metadata;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.text.z;
import org.json.JSONException;
import org.json.JSONObject;

@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u0000 \u00032\u00020\u0001:\u0001\u0003B\u0005¢\u0006\u0002\u0010\u0002¨\u0006\u0004"}, d2 = {"Lcom/amplifyframework/api/aws/auth/CognitoJWTParser;", "", "()V", "Companion", "aws-api_release"}, k = 1, mv = {1, 9, 0}, xi = 48)
/* loaded from: classes4.dex */
public final class CognitoJWTParser {

    /* renamed from: Companion, reason: from kotlin metadata */
    public static final Companion INSTANCE = new Companion(null);
    private static final int JWT_PARTS = 3;
    private static final int PAYLOAD = 1;

    @Metadata(d1 = {"\u0000$\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\u0006\b\u0086\u0003\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u000e\u0010\u000fJ\u0010\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0002J\u000e\u0010\u0007\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u0002J\u0018\u0010\t\u001a\u0004\u0018\u00010\u00022\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\b\u001a\u00020\u0002R\u0014\u0010\u000b\u001a\u00020\n8\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u000b\u0010\fR\u0014\u0010\r\u001a\u00020\n8\u0002X\u0082T¢\u0006\u0006\n\u0004\b\r\u0010\f¨\u0006\u0010"}, d2 = {"Lcom/amplifyframework/api/aws/auth/CognitoJWTParser$Companion;", "", "", "jwt", "Lrm/n0;", "validateJWT", "Lorg/json/JSONObject;", "getPayload", "claim", "getClaim", "", "JWT_PARTS", "I", "PAYLOAD", "<init>", "()V", "aws-api_release"}, k = 1, mv = {1, 9, 0})
    /* loaded from: classes4.dex */
    public static final class Companion {
        private Companion() {
        }

        public /* synthetic */ Companion(k kVar) {
            this();
        }

        private final void validateJWT(String str) {
            List I0;
            I0 = z.I0(str, new String[]{"."}, false, 0, 6, null);
            if (I0.size() != 3) {
                throw new CognitoParameterInvalidException("not a JSON Web Token", null, 2, null);
            }
        }

        public final String getClaim(String jwt, String claim) {
            t.g(jwt, "jwt");
            t.g(claim, "claim");
            try {
                return getPayload(jwt).get(claim).toString();
            } catch (Exception unused) {
                throw new CognitoParameterInvalidException("invalid token", null, 2, null);
            }
        }

        public final JSONObject getPayload(String jwt) {
            List I0;
            t.g(jwt, "jwt");
            try {
                validateJWT(jwt);
                I0 = z.I0(jwt, new String[]{"."}, false, 0, 6, null);
                byte[] decode = Base64.decode((String) I0.get(1), 8);
                t.d(decode);
                Charset forName = Charset.forName("UTF-8");
                t.f(forName, "forName(...)");
                return new JSONObject(new String(decode, forName));
            } catch (UnsupportedEncodingException e10) {
                throw new CognitoParameterInvalidException(e10.getMessage(), null, 2, null);
            } catch (JSONException e11) {
                throw new CognitoParameterInvalidException(e11.getMessage(), null, 2, null);
            } catch (Exception unused) {
                throw new CognitoParameterInvalidException("error in parsing JSON", null, 2, null);
            }
        }
    }
}
