package com.amplifyframework.api.aws.auth;

import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.aws.sigv4.AWS4Signer;
import dn.l;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import rm.n0;
import s6.f;
import s6.g;
import s6.i;
import s6.j;
import uq.b0;
import uq.c0;
import uq.x;

/* loaded from: classes4.dex */
public class IamRequestDecorator implements RequestDecorator {
    private static final String DEFAULT_CONTENT_TYPE = "application/json";
    private final n6.c credentialsProvider;
    private final String serviceName;
    private final AWS4Signer v4Signer;

    public IamRequestDecorator(AWS4Signer aWS4Signer, n6.c cVar, String str) {
        this.v4Signer = aWS4Signer;
        this.credentialsProvider = cVar;
        this.serviceName = str;
    }

    private byte[] getBytes(c0 c0Var) throws ApiException.ApiAuthException {
        if (c0Var == null) {
            return "".getBytes();
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                kr.d dVar = new kr.d();
                c0Var.k(dVar);
                byte[] bArr = new byte[4096];
                while (true) {
                    int read = dVar.R0().read(bArr);
                    if (read == -1) {
                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                        byteArrayOutputStream.close();
                        return byteArray;
                    }
                    byteArrayOutputStream.write(bArr, 0, read);
                }
            } finally {
            }
        } catch (IOException e10) {
            throw new ApiException.ApiAuthException("Unable to calculate SigV4 signature for the request", e10, "Check your application logs for details.");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ n0 lambda$decorate$0(b0 b0Var, g gVar) {
        for (String str : b0Var.e().m()) {
            gVar.h(str, b0Var.d(str));
        }
        gVar.h("Host", b0Var.k().u().getHost());
        return null;
    }

    @Override // com.amplifyframework.api.aws.auth.RequestDecorator
    public final b0 decorate(final b0 b0Var) throws ApiException.ApiAuthException {
        byte[] bytes = getBytes(b0Var.a());
        u6.a aVar = (u6.a) this.v4Signer.signBlocking(u6.d.a(j.Companion.a(b0Var.g()), x6.b.f50509k.b(b0Var.k().t().toString(), x6.c.f50546b.a()), f.f35435b.b(new l() { // from class: com.amplifyframework.api.aws.auth.e
            @Override // dn.l
            public final Object invoke(Object obj) {
                n0 lambda$decorate$0;
                lambda$decorate$0 = IamRequestDecorator.lambda$decorate$0(b0.this, (g) obj);
                return lambda$decorate$0;
            }
        }), i.f35438b.a(bytes), s6.a.f35428a.a()), this.credentialsProvider, this.serviceName).a();
        b0.a aVar2 = new b0.a();
        String str = DEFAULT_CONTENT_TYPE;
        for (Map.Entry entry : aVar.a().a()) {
            String str2 = (String) entry.getKey();
            String str3 = (String) ((List) entry.getValue()).get(0);
            aVar2.a(str2, str3);
            if ("content-type".equalsIgnoreCase(str2)) {
                str = str3;
            }
        }
        x g10 = x.g(str);
        if (g10 == null) {
            g10 = x.g(DEFAULT_CONTENT_TYPE);
        }
        aVar2.A(b0Var.k());
        aVar2.o(b0Var.g(), b0Var.a() == null ? null : c0.h(bytes, g10));
        return aVar2.b();
    }
}
