package com.google.firebase.appcheck.debug.internal;

import android.util.Log;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.tasks.SuccessContinuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Background;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.debug.InternalDebugSecretProvider;
import com.google.firebase.appcheck.internal.AppCheckTokenResponse;
import com.google.firebase.appcheck.internal.DefaultAppCheckToken;
import com.google.firebase.appcheck.internal.NetworkClient;
import com.google.firebase.appcheck.internal.RetryManager;
import com.google.firebase.inject.Provider;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;

/* loaded from: classes3.dex */
public class DebugAppCheckProvider implements AppCheckProvider {
    private static final String TAG = "com.google.firebase.appcheck.debug.internal.DebugAppCheckProvider";
    private static final String UTF_8 = "UTF-8";
    private final Executor blockingExecutor;
    private final Task<String> debugSecretTask;
    private final Executor liteExecutor;
    private final NetworkClient networkClient;
    private final RetryManager retryManager;

    public DebugAppCheckProvider(FirebaseApp firebaseApp, Provider<InternalDebugSecretProvider> provider, @Lightweight Executor executor, @Background Executor executor2, @Blocking Executor executor3) {
        Task<String> forResult;
        Preconditions.checkNotNull(firebaseApp);
        this.networkClient = new NetworkClient(firebaseApp);
        this.liteExecutor = executor;
        this.blockingExecutor = executor3;
        this.retryManager = new RetryManager();
        String debugSecret = provider.get() != null ? provider.get().getDebugSecret() : null;
        if (debugSecret == null) {
            forResult = determineDebugSecret(firebaseApp, executor2);
        } else {
            forResult = Tasks.forResult(debugSecret);
        }
        this.debugSecretTask = forResult;
    }

    static Task<String> determineDebugSecret(final FirebaseApp firebaseApp, Executor executor) {
        final TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
        executor.execute(new Runnable() { // from class: com.google.firebase.appcheck.debug.internal.b
            @Override // java.lang.Runnable
            public final void run() {
                DebugAppCheckProvider.lambda$determineDebugSecret$0(FirebaseApp.this, taskCompletionSource);
            }
        });
        return taskCompletionSource.getTask();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$determineDebugSecret$0(FirebaseApp firebaseApp, TaskCompletionSource taskCompletionSource) {
        StorageHelper storageHelper = new StorageHelper(firebaseApp.getApplicationContext(), firebaseApp.getPersistenceKey());
        String retrieveDebugSecret = storageHelper.retrieveDebugSecret();
        if (retrieveDebugSecret == null) {
            retrieveDebugSecret = UUID.randomUUID().toString();
            storageHelper.saveDebugSecret(retrieveDebugSecret);
        }
        Log.d(TAG, "Enter this debug secret into the allow list in the Firebase Console for your project: " + retrieveDebugSecret);
        taskCompletionSource.setResult(retrieveDebugSecret);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ AppCheckTokenResponse lambda$getToken$1(ExchangeDebugTokenRequest exchangeDebugTokenRequest) throws Exception {
        return this.networkClient.exchangeAttestationForAppCheckToken(exchangeDebugTokenRequest.toJsonString().getBytes(UTF_8), 2, this.retryManager);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ Task lambda$getToken$2(String str) throws Exception {
        final ExchangeDebugTokenRequest exchangeDebugTokenRequest = new ExchangeDebugTokenRequest(str);
        return Tasks.call(this.blockingExecutor, new Callable() { // from class: com.google.firebase.appcheck.debug.internal.a
            @Override // java.util.concurrent.Callable
            public final Object call() {
                AppCheckTokenResponse lambda$getToken$1;
                lambda$getToken$1 = DebugAppCheckProvider.this.lambda$getToken$1(exchangeDebugTokenRequest);
                return lambda$getToken$1;
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ Task lambda$getToken$3(AppCheckTokenResponse appCheckTokenResponse) throws Exception {
        return Tasks.forResult(DefaultAppCheckToken.constructFromAppCheckTokenResponse(appCheckTokenResponse));
    }

    @Override // com.google.firebase.appcheck.AppCheckProvider
    public Task<AppCheckToken> getToken() {
        return this.debugSecretTask.onSuccessTask(this.liteExecutor, new SuccessContinuation() { // from class: com.google.firebase.appcheck.debug.internal.c
            @Override // com.google.android.gms.tasks.SuccessContinuation
            public final Task then(Object obj) {
                Task lambda$getToken$2;
                lambda$getToken$2 = DebugAppCheckProvider.this.lambda$getToken$2((String) obj);
                return lambda$getToken$2;
            }
        }).onSuccessTask(this.liteExecutor, new SuccessContinuation() { // from class: com.google.firebase.appcheck.debug.internal.d
            @Override // com.google.android.gms.tasks.SuccessContinuation
            public final Task then(Object obj) {
                Task lambda$getToken$3;
                lambda$getToken$3 = DebugAppCheckProvider.lambda$getToken$3((AppCheckTokenResponse) obj);
                return lambda$getToken$3;
            }
        });
    }

    DebugAppCheckProvider(String str, NetworkClient networkClient, Executor executor, Executor executor2, RetryManager retryManager) {
        this.networkClient = networkClient;
        this.liteExecutor = executor;
        this.blockingExecutor = executor2;
        this.retryManager = retryManager;
        this.debugSecretTask = Tasks.forResult(str);
    }
}
