Remove encryption key phase

This commit is contained in:
Andrea Cavalli 2022-10-17 13:26:53 +02:00
parent 29cb9f6078
commit e27ffc8d92
9 changed files with 19 additions and 44 deletions

View File

@ -3,7 +3,6 @@ package it.tdlight.reactiveapi;
public enum AuthPhase {
LOGGED_OUT,
PARAMETERS_PHASE,
ENCRYPTION_PHASE,
AUTH_PHASE,
LOGGED_IN,
LOGGING_OUT,

View File

@ -17,13 +17,11 @@ import it.tdlight.jni.TdApi.AuthorizationStateClosed;
import it.tdlight.jni.TdApi.AuthorizationStateWaitOtherDeviceConfirmation;
import it.tdlight.jni.TdApi.AuthorizationStateWaitPassword;
import it.tdlight.jni.TdApi.CheckAuthenticationBotToken;
import it.tdlight.jni.TdApi.CheckDatabaseEncryptionKey;
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.TdApi.Object;
import it.tdlight.jni.TdApi.PhoneNumberAuthenticationSettings;
import it.tdlight.jni.TdApi.SetAuthenticationPhoneNumber;
import it.tdlight.jni.TdApi.SetTdlibParameters;
import it.tdlight.jni.TdApi.TdlibParameters;
import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.Ignored;
import it.tdlight.reactiveapi.Event.OnBotLoginCodeRequested;
@ -267,14 +265,14 @@ public abstract class ReactiveApiPublisher {
private <T extends TdApi.Object> Mono<TdApi.Object> fixBrokenKey(Function<T> function, TdApi.Object result) {
if (result.getConstructor() == TdApi.Error.CONSTRUCTOR
&& function instanceof TdApi.CheckDatabaseEncryptionKey checkDatabaseEncryptionKey) {
&& function instanceof TdApi.SetTdlibParameters setTdlibParameters) {
// Fix legacy "cucumbers" password
if (checkDatabaseEncryptionKey.encryptionKey == null
if (setTdlibParameters.databaseEncryptionKey == null
&& "Wrong password".equals(((TdApi.Error) result).message)) {
var checkOldKeyFunction = new TdApi.CheckDatabaseEncryptionKey("cucumber".getBytes(StandardCharsets.US_ASCII));
setTdlibParameters.databaseEncryptionKey = "cucumber".getBytes(StandardCharsets.US_ASCII);
Mono<TdApi.Object> oldKeyCheckResultMono = Mono
.from(rawTelegramClient.send(checkOldKeyFunction, SPECIAL_RAW_TIMEOUT_DURATION));
.from(rawTelegramClient.send(setTdlibParameters, SPECIAL_RAW_TIMEOUT_DURATION));
return oldKeyCheckResultMono.flatMap(oldKeyCheckResult -> {
if (oldKeyCheckResult.getConstructor() != TdApi.Error.CONSTRUCTOR) {
var fixOldKeyFunction = new TdApi.SetDatabaseEncryptionKey();
@ -351,20 +349,8 @@ public abstract class ReactiveApiPublisher {
var updateAuthorizationState = (TdApi.UpdateAuthorizationState) update;
switch (updateAuthorizationState.authorizationState.getConstructor()) {
case TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR -> {
TdlibParameters parameters = generateTDLibParameters();
return List.of(updateResult, new TDLibBoundResultingEvent<>(new SetTdlibParameters(parameters)));
}
}
}
}
}
case ENCRYPTION_PHASE -> {
switch (update.getConstructor()) {
case TdApi.UpdateAuthorizationState.CONSTRUCTOR -> {
var updateAuthorizationState = (TdApi.UpdateAuthorizationState) update;
switch (updateAuthorizationState.authorizationState.getConstructor()) {
case TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
return List.of(updateResult, new TDLibBoundResultingEvent<>(new CheckDatabaseEncryptionKey()));
SetTdlibParameters parameters = generateTDLibParameters();
return List.of(updateResult, new TDLibBoundResultingEvent<>(parameters));
}
}
}
@ -404,8 +390,8 @@ public abstract class ReactiveApiPublisher {
return List.of();
}
private TdlibParameters generateTDLibParameters() {
var tdlibParameters = new TdlibParameters();
private SetTdlibParameters generateTDLibParameters() {
var tdlibParameters = new SetTdlibParameters();
var path = requireNonNull(this.path.get(), "Path must not be null");
tdlibParameters.databaseDirectory = path + "?use_custom_database_format=true";
tdlibParameters.apiId = 376588;

View File

@ -2,7 +2,6 @@ package it.tdlight.reactiveapi;
import static it.tdlight.reactiveapi.AuthPhase.AUTH_PHASE;
import static it.tdlight.reactiveapi.AuthPhase.BROKEN;
import static it.tdlight.reactiveapi.AuthPhase.ENCRYPTION_PHASE;
import static it.tdlight.reactiveapi.AuthPhase.LOGGED_IN;
import static it.tdlight.reactiveapi.AuthPhase.LOGGED_OUT;
import static it.tdlight.reactiveapi.AuthPhase.LOGGING_OUT;
@ -30,7 +29,7 @@ public record State(AuthPhase authPhase) implements StateBuilder.With {
newState = switch (newState.authPhase) {
// Mark state as broken if the connection is terminated unexpectedly
case PARAMETERS_PHASE, ENCRYPTION_PHASE, AUTH_PHASE, LOGGED_IN -> {
case PARAMETERS_PHASE, AUTH_PHASE, LOGGED_IN -> {
if (signal.isClosed()) {
yield newState.withAuthPhase(BROKEN);
} else {
@ -83,34 +82,25 @@ public record State(AuthPhase authPhase) implements StateBuilder.With {
}
yield newState.withAuthPhase(PARAMETERS_PHASE);
}
case TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
if (newState.authPhase != PARAMETERS_PHASE) {
LOG.warn("Waiting parameters, but the current auth phase is {} instead of {}",
newState.authPhase,
Set.of(PARAMETERS_PHASE)
);
}
yield newState.withAuthPhase(ENCRYPTION_PHASE);
}
case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR,
TdApi.AuthorizationStateWaitRegistration.CONSTRUCTOR,
TdApi.AuthorizationStateWaitCode.CONSTRUCTOR,
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR,
TdApi.AuthorizationStateWaitOtherDeviceConfirmation.CONSTRUCTOR -> {
if (newState.authPhase != ENCRYPTION_PHASE && newState.authPhase != AUTH_PHASE) {
if (newState.authPhase != PARAMETERS_PHASE && newState.authPhase != AUTH_PHASE) {
LOG.warn(
"Waiting for authentication, but the current auth phase is {} instead of {}",
newState.authPhase,
Set.of(ENCRYPTION_PHASE, AUTH_PHASE)
Set.of(PARAMETERS_PHASE, AUTH_PHASE)
);
}
yield newState.withAuthPhase(AUTH_PHASE);
}
case TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
if (newState.authPhase != ENCRYPTION_PHASE && newState.authPhase != AUTH_PHASE) {
if (newState.authPhase != PARAMETERS_PHASE && newState.authPhase != AUTH_PHASE) {
LOG.warn("Logged in, but the current auth phase is {} instead of {}",
newState.authPhase,
Set.of(ENCRYPTION_PHASE, AUTH_PHASE)
Set.of(PARAMETERS_PHASE, AUTH_PHASE)
);
}
yield newState.withAuthPhase(LOGGED_IN);

View File

@ -40,7 +40,7 @@ public class DefaultOptions implements ResultingEventTransformer {
if (event instanceof ClientBoundResultingEvent clientBoundResultingEvent
&& clientBoundResultingEvent.event() instanceof OnUpdateData onUpdate
&& onUpdate.update() instanceof TdApi.UpdateAuthorizationState authorizationState
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitEncryptionKey) {
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitTdlibParameters) {
var resultingEvent = new ArrayList<ResultingEvent>(1 + DEFAULT_OPTIONS.size() + DEFAULT_USER_OPTIONS.size());
// Add the intercepted event

View File

@ -16,7 +16,7 @@ public class DisableChatDatabase implements ResultingEventTransformer {
// Change option
if (event instanceof TDLibBoundResultingEvent tdLibBoundResultingEvent
&& tdLibBoundResultingEvent.action() instanceof TdApi.SetTdlibParameters setTdlibParameters) {
setTdlibParameters.parameters.useChatInfoDatabase = false;
setTdlibParameters.useChatInfoDatabase = false;
}
return List.of(event);

View File

@ -16,7 +16,7 @@ public class DisableFileDatabase implements ResultingEventTransformer {
// Change option
if (event instanceof TDLibBoundResultingEvent tdLibBoundResultingEvent
&& tdLibBoundResultingEvent.action() instanceof TdApi.SetTdlibParameters setTdlibParameters) {
setTdlibParameters.parameters.useFileDatabase = false;
setTdlibParameters.useFileDatabase = false;
}
return List.of(event);

View File

@ -16,7 +16,7 @@ public class DisableMessageDatabase implements ResultingEventTransformer {
// Change option
if (event instanceof TDLibBoundResultingEvent tdLibBoundResultingEvent
&& tdLibBoundResultingEvent.action() instanceof TdApi.SetTdlibParameters setTdlibParameters) {
setTdlibParameters.parameters.useMessageDatabase = false;
setTdlibParameters.useMessageDatabase = false;
}
return List.of(event);

View File

@ -22,7 +22,7 @@ public class EnableMinithumbnails implements ResultingEventTransformer {
if (event instanceof ClientBoundResultingEvent clientBoundResultingEvent
&& clientBoundResultingEvent.event() instanceof OnUpdateData onUpdate
&& onUpdate.update() instanceof TdApi.UpdateAuthorizationState authorizationState
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitEncryptionKey) {
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitTdlibParameters) {
var resultingEvent = new ArrayList<ResultingEvent>(2);
// Add the intercepted event

View File

@ -35,7 +35,7 @@ public class TdlightDefaultOptions implements ResultingEventTransformer {
if (event instanceof ClientBoundResultingEvent clientBoundResultingEvent
&& clientBoundResultingEvent.event() instanceof OnUpdateData onUpdate
&& onUpdate.update() instanceof TdApi.UpdateAuthorizationState authorizationState
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitEncryptionKey) {
&& authorizationState.authorizationState instanceof TdApi.AuthorizationStateWaitTdlibParameters) {
var resultingEvent = new ArrayList<ResultingEvent>(1 + DEFAULT_OPTIONS.size() + DEFAULT_USER_OPTIONS.size());
// Add the intercepted event