discord-jadx/app/src/main/java/com/discord/stores/StoreAuthentication.java

450 lines
20 KiB
Java

package com.discord.stores;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Patterns;
import androidx.core.content.ContextCompat;
import c.d.b.a.a;
import com.discord.api.auth.RegisterResponse;
import com.discord.app.AppLog;
import com.discord.models.domain.auth.ModelLoginResult;
import com.discord.restapi.RestAPIParams;
import com.discord.stores.StoreInviteSettings;
import com.discord.stores.StoreNavigation;
import com.discord.utilities.analytics.AnalyticsTracker;
import com.discord.utilities.persister.Persister;
import com.discord.utilities.rest.RestAPI;
import com.discord.utilities.rx.ObservableExtensionsKt;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import d0.z.d.m;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.internal.DefaultConstructorMarker;
import rx.Observable;
import rx.Subscription;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
/* compiled from: StoreAuthentication.kt */
public final class StoreAuthentication extends Store {
private static final String CACHE_KEY_FINGERPRINT = "STORE_AUTHED_FINGERPRINT";
private static final String CACHE_KEY_LOGIN = "STORE_AUTHED_LOGIN";
private static final String CACHE_KEY_TOKEN = "STORE_AUTHED_TOKEN";
public static final Companion Companion = new Companion(null);
private Boolean _isConsentRequired;
private BehaviorSubject<String> ageGateError;
private String authToken;
private final Dispatcher dispatcher;
private String fingerprint;
private final SerializedSubject<String, String> fingerprintSubject = new SerializedSubject<>(BehaviorSubject.l0());
private Subscription isConsentRequiredSubscription;
private final SerializedSubject<Uri, Uri> oauthUriSubject;
private String savedLogin;
private final StoreStream storeStream;
private final SerializedSubject<String, String> tokenSubject = new SerializedSubject<>(BehaviorSubject.l0());
private final PublishSubject<Boolean> userInitiatedAuthEventSubject = PublishSubject.l0();
/* compiled from: StoreAuthentication.kt */
public static final class AuthRequestParams {
private final String guildTemplateCode;
private final StoreInviteSettings.InviteCode inviteCode;
public AuthRequestParams(StoreInviteSettings.InviteCode inviteCode, String str) {
this.inviteCode = inviteCode;
this.guildTemplateCode = str;
}
public static /* synthetic */ AuthRequestParams copy$default(AuthRequestParams authRequestParams, StoreInviteSettings.InviteCode inviteCode, String str, int i, Object obj) {
if ((i & 1) != 0) {
inviteCode = authRequestParams.inviteCode;
}
if ((i & 2) != 0) {
str = authRequestParams.guildTemplateCode;
}
return authRequestParams.copy(inviteCode, str);
}
public final StoreInviteSettings.InviteCode component1() {
return this.inviteCode;
}
public final String component2() {
return this.guildTemplateCode;
}
public final AuthRequestParams copy(StoreInviteSettings.InviteCode inviteCode, String str) {
return new AuthRequestParams(inviteCode, str);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof AuthRequestParams)) {
return false;
}
AuthRequestParams authRequestParams = (AuthRequestParams) obj;
return m.areEqual(this.inviteCode, authRequestParams.inviteCode) && m.areEqual(this.guildTemplateCode, authRequestParams.guildTemplateCode);
}
public final String getGuildTemplateCode() {
return this.guildTemplateCode;
}
public final StoreInviteSettings.InviteCode getInviteCode() {
return this.inviteCode;
}
public int hashCode() {
StoreInviteSettings.InviteCode inviteCode = this.inviteCode;
int i = 0;
int hashCode = (inviteCode != null ? inviteCode.hashCode() : 0) * 31;
String str = this.guildTemplateCode;
if (str != null) {
i = str.hashCode();
}
return hashCode + i;
}
public String toString() {
StringBuilder L = a.L("AuthRequestParams(inviteCode=");
L.append(this.inviteCode);
L.append(", guildTemplateCode=");
return a.D(L, this.guildTemplateCode, ")");
}
}
/* compiled from: StoreAuthentication.kt */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
this();
}
public static final /* synthetic */ String access$getDeviceEmail(Companion companion, Context context) {
return companion.getDeviceEmail(context);
}
@SuppressLint({"MissingPermission"})
private final String getDeviceEmail(Context context) {
Account account;
if (ContextCompat.checkSelfPermission(context, "android.permission.GET_ACCOUNTS") != 0) {
return null;
}
AccountManager accountManager = AccountManager.get(context);
m.checkNotNullExpressionValue(accountManager, "AccountManager.get(context)");
Account[] accounts = accountManager.getAccounts();
m.checkNotNullExpressionValue(accounts, "AccountManager.get(context).accounts");
int length = accounts.length;
int i = 0;
while (true) {
if (i >= length) {
account = null;
break;
}
account = accounts[i];
if (Patterns.EMAIL_ADDRESS.matcher(account.type).matches()) {
break;
}
i++;
}
if (account != null) {
return account.name;
}
return null;
}
}
public StoreAuthentication(StoreStream storeStream, Dispatcher dispatcher) {
m.checkNotNullParameter(storeStream, "storeStream");
m.checkNotNullParameter(dispatcher, "dispatcher");
this.storeStream = storeStream;
this.dispatcher = dispatcher;
BehaviorSubject<String> l0 = BehaviorSubject.l0();
m.checkNotNullExpressionValue(l0, "BehaviorSubject.create()");
this.ageGateError = l0;
this.oauthUriSubject = new SerializedSubject<>(BehaviorSubject.m0(Uri.EMPTY));
}
public static final /* synthetic */ void access$dispatchLogin(StoreAuthentication storeAuthentication, ModelLoginResult modelLoginResult) {
storeAuthentication.dispatchLogin(modelLoginResult);
}
public static final /* synthetic */ Observable access$getFingerprintSnapshotOrGenerate(StoreAuthentication storeAuthentication) {
return storeAuthentication.getFingerprintSnapshotOrGenerate();
}
public static final /* synthetic */ StoreStream access$getStoreStream$p(StoreAuthentication storeAuthentication) {
return storeAuthentication.storeStream;
}
public static final /* synthetic */ boolean access$isConsentRequired$p(StoreAuthentication storeAuthentication) {
return storeAuthentication.isConsentRequired();
}
public static final /* synthetic */ Subscription access$isConsentRequiredSubscription$p(StoreAuthentication storeAuthentication) {
return storeAuthentication.isConsentRequiredSubscription;
}
public static final /* synthetic */ void access$setConsentRequired$p(StoreAuthentication storeAuthentication, boolean z2) {
storeAuthentication.setConsentRequired(z2);
}
public static final /* synthetic */ void access$setConsentRequiredSubscription$p(StoreAuthentication storeAuthentication, Subscription subscription) {
storeAuthentication.isConsentRequiredSubscription = subscription;
}
private final void dispatchLogin(ModelLoginResult modelLoginResult) {
this.dispatcher.schedule(new StoreAuthentication$dispatchLogin$1(this, modelLoginResult));
}
private final Observable<String> getFingerprintSnapshotOrGenerate() {
Observable<String> u = this.fingerprintSubject.a0(1).Z(StoreAuthentication$getFingerprintSnapshotOrGenerate$1.INSTANCE).u(new StoreAuthentication$getFingerprintSnapshotOrGenerate$2(this));
m.checkNotNullExpressionValue(u, "fingerprintSubject\n …int(fingerprint, false) }");
return u;
}
public static /* synthetic */ Observable login$default(StoreAuthentication storeAuthentication, String str, String str2, String str3, boolean z2, String str4, int i, Object obj) {
if ((i & 16) != 0) {
str4 = null;
}
return storeAuthentication.login(str, str2, str3, z2, str4);
}
public static /* synthetic */ Observable register$default(StoreAuthentication storeAuthentication, String str, String str2, String str3, String str4, String str5, boolean z2, String str6, int i, Object obj) {
return storeAuthentication.register(str, str2, str3, str4, str5, z2, (i & 64) != 0 ? null : str6);
}
private final synchronized void resetIsConsentRequired() {
this._isConsentRequired = null;
Subscription subscription = this.isConsentRequiredSubscription;
if (subscription != null) {
subscription.unsubscribe();
}
this.isConsentRequiredSubscription = null;
}
private final synchronized void setConsentRequired(boolean z2) {
this._isConsentRequired = Boolean.valueOf(z2);
}
private final void setSavedLogin(String str) {
this.savedLogin = str;
AppLog.g(null, str, null);
getPrefsSessionDurable().edit().putString("STORE_AUTHED_LOGIN", str).apply();
}
public final Observable<ModelLoginResult> authMFA(String str, String str2) {
m.checkNotNullParameter(str, "code");
m.checkNotNullParameter(str2, "ticket");
Observable<ModelLoginResult> u = ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().postMFACode(new RestAPIParams.MFALogin(str2, str)), false, 1, null).u(new StoreAuthentication$authMFA$1(this));
m.checkNotNullExpressionValue(u, "RestAPI\n .api\n …in(loginResult)\n }");
return u;
}
public final Observable<Void> authorizeIP(String str) {
m.checkNotNullParameter(str, "token");
return ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().authorizeIP(new RestAPIParams.AuthorizeIP(str)), false, 1, null);
}
public final Observable<Void> forgotPassword(String str, String str2) {
m.checkNotNullParameter(str, "login");
return ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().forgotPassword(new RestAPIParams.ForgotPassword(str, str2)), false, 1, null);
}
public final Observable<String> getAgeGateError() {
Observable<String> s2 = this.ageGateError.s();
m.checkNotNullExpressionValue(s2, "ageGateError.distinctUntilChanged()");
return s2;
}
public final String getAuthToken$app_productionBetaRelease() {
return this.authToken;
}
public final Observable<String> getAuthedToken$app_productionBetaRelease() {
Observable<String> s2 = this.tokenSubject.s();
m.checkNotNullExpressionValue(s2, "tokenSubject.distinctUntilChanged()");
return s2;
}
public final Observable<String> getFingerPrint$app_productionBetaRelease() {
Observable<String> s2 = this.fingerprintSubject.s();
m.checkNotNullExpressionValue(s2, "fingerprintSubject.distinctUntilChanged()");
return s2;
}
public final String getFingerprint$app_productionBetaRelease() {
return this.fingerprint;
}
public final Observable<Uri> getOAuthUriObservable() {
Observable<Uri> s2 = this.oauthUriSubject.s();
m.checkNotNullExpressionValue(s2, "oauthUriSubject.distinctUntilChanged()");
return s2;
}
public final Observable<Boolean> getPreLogoutSignal$app_productionBetaRelease() {
Observable<Boolean> y2 = this.userInitiatedAuthEventSubject.y(StoreAuthentication$getPreLogoutSignal$1.INSTANCE);
m.checkNotNullExpressionValue(y2, "userInitiatedAuthEventSu…LoggedIn -> !isLoggedIn }");
return y2;
}
public final String getSavedLogin() {
return this.savedLogin;
}
public final Observable<StoreNavigation.AgeGate> getShouldShowAgeGate() {
Observable<StoreNavigation.AgeGate> s2 = Observable.i(this.storeStream.getUsers$app_productionBetaRelease().observeMe(true), this.storeStream.getAuthentication$app_productionBetaRelease().getAgeGateError(), this.storeStream.getChannelsSelected$app_productionBetaRelease().observeSelectedChannel(), StoreAuthentication$getShouldShowAgeGate$1.INSTANCE).s();
m.checkNotNullExpressionValue(s2, "Observable.combineLatest… .distinctUntilChanged()");
return s2;
}
@StoreThread
public final void handleAgeGateError(String str) {
this.ageGateError.onNext(str);
}
@StoreThread
public final void handleAuthToken$app_productionBetaRelease(String str) {
this.authToken = str;
getPrefs().edit().putString("STORE_AUTHED_TOKEN", str).apply();
if (str == null) {
Persister.Companion.reset();
SharedPreferences.Editor edit = getPrefs().edit();
m.checkExpressionValueIsNotNull(edit, "editor");
edit.clear();
edit.apply();
}
}
@StoreThread
public final void handleLoginResult(ModelLoginResult modelLoginResult) {
m.checkNotNullParameter(modelLoginResult, "loginResult");
setFingerprint(null, true);
setAuthed(modelLoginResult.getToken());
if (modelLoginResult.getToken() != null) {
setAgeGateError(null);
AnalyticsTracker.INSTANCE.appFirstLogin();
}
PublishSubject<Boolean> publishSubject = this.userInitiatedAuthEventSubject;
publishSubject.j.onNext(Boolean.TRUE);
StoreStream.Companion.getNux().setFirstOpen(true);
}
@StoreThread
public final void handlePreLogout$app_productionBetaRelease() {
resetIsConsentRequired();
}
@Override // com.discord.stores.Store
@StoreThread
public synchronized void init(Context context) {
m.checkNotNullParameter(context, "context");
super.init(context);
String string = getPrefsSessionDurable().getString("STORE_AUTHED_FINGERPRINT", null);
if (string != null) {
setFingerprint(string, false);
}
String string2 = getPrefs().getString("STORE_AUTHED_TOKEN", null);
this.authToken = string2;
setAuthed(string2);
handleAgeGateError(null);
setSavedLogin(getPrefsSessionDurable().getString("STORE_AUTHED_LOGIN", Companion.access$getDeviceEmail(Companion, context)));
Observable<Boolean> s2 = this.userInitiatedAuthEventSubject.s();
m.checkNotNullExpressionValue(s2, "userInitiatedAuthEventSu… .distinctUntilChanged()");
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.ui(ObservableExtensionsKt.computationLatest(s2)), getClass(), (Context) null, (Function1) null, (Function1) null, (Function0) null, (Function0) null, new StoreAuthentication$init$1(context), 62, (Object) null);
}
public final boolean isAuthed() {
return this.authToken != null;
}
public final synchronized boolean isConsentRequired() {
Boolean bool;
bool = this._isConsentRequired;
return bool != null ? bool.booleanValue() : true;
}
public final Observable<ModelLoginResult> login(String str, String str2, String str3, boolean z2, String str4) {
m.checkNotNullParameter(str, "login");
m.checkNotNullParameter(str2, "password");
setSavedLogin(str);
Observable<ModelLoginResult> u = getFingerprintSnapshotOrGenerate().A(new StoreAuthentication$login$1(str, str2, str3, z2, str4)).u(new StoreAuthentication$login$2(this));
m.checkNotNullExpressionValue(u, "getFingerprintSnapshotOr…lt)\n }\n }");
return u;
}
public final void logout() {
PublishSubject<Boolean> publishSubject = this.userInitiatedAuthEventSubject;
publishSubject.j.onNext(Boolean.FALSE);
Observable A = ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().logout(new RestAPIParams.UserDevices(StoreStream.Companion.getNotifications().getPushToken())), false, 1, null).A(new StoreAuthentication$logout$1(this));
m.checkNotNullExpressionValue(A, "RestAPI\n .api\n …intSnapshotOrGenerate() }");
ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.takeSingleUntilTimeout$default(A, 0, false, 3, null), (Context) null, "logout", (Function1) null, new StoreAuthentication$logout$2(this), new StoreAuthentication$logout$3(this), (Function0) null, (Function0) null, 101, (Object) null);
}
public final Observable<Boolean> observeIsAuthed$app_productionBetaRelease() {
Observable<R> G = getAuthedToken$app_productionBetaRelease().G(StoreAuthentication$observeIsAuthed$1.INSTANCE);
m.checkNotNullExpressionValue(G, "getAuthedToken()\n .… -> authedToken != null }");
return G;
}
public final Observable<RegisterResponse> register(String str, String str2, String str3, String str4, String str5, boolean z2, String str6) {
m.checkNotNullParameter(str, "username");
m.checkNotNullParameter(str4, "password");
setSavedLogin(str2);
Observable<R> A = getFingerprintSnapshotOrGenerate().A(new StoreAuthentication$register$1(this, str, str2, str3, str4, str5, z2, str6));
m.checkNotNullExpressionValue(A, "getFingerprintSnapshotOr… }\n }");
return A;
}
public final synchronized void requestConsentRequired() {
if (this._isConsentRequired == null) {
resetIsConsentRequired();
Observable restSubscribeOn$default = ObservableExtensionsKt.restSubscribeOn$default(RestAPI.Companion.getApi().getLocationMetadata(), false, 1, null);
Class<?> cls = getClass();
StoreAuthentication$requestConsentRequired$1 storeAuthentication$requestConsentRequired$1 = new StoreAuthentication$requestConsentRequired$1(this);
ObservableExtensionsKt.appSubscribe$default(restSubscribeOn$default, cls, (Context) null, new StoreAuthentication$requestConsentRequired$3(this), new StoreAuthentication$requestConsentRequired$2(this), (Function0) null, (Function0) null, storeAuthentication$requestConsentRequired$1, 50, (Object) null);
}
}
public final void setAgeGateError(String str) {
this.dispatcher.schedule(new StoreAuthentication$setAgeGateError$1(this, str));
}
public final void setAuthed(String str) {
this.tokenSubject.j.onNext(str);
}
/* JADX WARNING: Code restructure failed: missing block: B:4:0x0005, code lost:
if (r1.fingerprint == null) goto L_0x0007;
*/
public final synchronized void setFingerprint(String str, boolean z2) {
if (!z2) {
}
getPrefsSessionDurable().edit().putString("STORE_AUTHED_FINGERPRINT", str).apply();
this.fingerprint = str;
this.fingerprintSubject.j.onNext(str);
if (str != null) {
FirebaseCrashlytics.getInstance().setCustomKey("fingerprint", str);
}
String str2 = this.fingerprint;
if (!(str2 == null || !(!m.areEqual(str2, str)) || str == null)) {
String str3 = this.fingerprint;
m.checkNotNull(str3);
AnalyticsTracker.externalFingerprintDropped(str3, str);
}
}
public final void setOAuthUriSubject(Uri uri) {
m.checkNotNullParameter(uri, "uri");
this.oauthUriSubject.j.onNext(uri);
}
}