From df40a09419d23a690c97dd6762efa53213f48c8c Mon Sep 17 00:00:00 2001 From: inexcode Date: Wed, 18 May 2022 14:21:11 +0300 Subject: [PATCH] Add cubit methods to try recover the server --- .../server_installation_cubit.dart | 55 ++++++++++++++++++- .../server_installation_repository.dart | 12 ++-- .../server_installation_state.dart | 6 ++ lib/logic/models/hive/server_details.g.dart | 2 +- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index 38b140db..3c80f2eb 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -3,8 +3,8 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart'; -import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/server_details.dart'; +import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; import '../server_installation/server_installation_repository.dart'; @@ -252,6 +252,59 @@ class ServerInstallationCubit extends Cubit { timer = Timer(delay, work); } + void submitDomainForAccessRecovery(String domain) async { + var serverDomain = ServerDomain( + domainName: domain, + provider: DnsProvider.Unknown, + zoneId: '', + ); + final recoveryCapabilities = + await repository.getRecoveryCapabilities(serverDomain); + + emit(ServerInstallationRecovery( + serverDomain: serverDomain, + recoveryCapabilities: recoveryCapabilities, + currentStep: RecoveryStep.Selecting, + )); + } + + void tryToRecover(String token, ServerRecoveryMethods method) async { + final dataState = this.state as ServerInstallationRecovery; + final serverDomain = dataState.serverDomain; + if (serverDomain == null) { + return; + } + try { + Future Function(ServerDomain, String) + recoveryFunction; + switch (method) { + case ServerRecoveryMethods.newDeviceKey: + recoveryFunction = repository.authorizeByNewDeviceKey; + break; + case ServerRecoveryMethods.recoveryKey: + recoveryFunction = repository.authorizeByRecoveryKey; + break; + case ServerRecoveryMethods.oldToken: + recoveryFunction = repository.authorizeByApiToken; + break; + default: + throw Exception('Unknown recovery method'); + } + final serverDetails = await recoveryFunction( + serverDomain, + token, + ); + emit(dataState.copyWith( + serverDetails: serverDetails, + currentStep: RecoveryStep.HetznerToken, + )); + } on ServerAuthorizationException { + return; + } on IpNotFoundException { + return; + } + } + void clearAppConfig() { closeTimer(); diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index 6af170f6..9a0a826c 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -371,9 +371,9 @@ class ServerInstallationRepository { return 'Unidentified'; } - Future authorizeByLoginToken( + Future authorizeByNewDeviceKey( ServerDomain serverDomain, - String loginToken, + String newDeviceKey, ) async { var serverApi = ServerApi( isWithToken: false, @@ -381,7 +381,7 @@ class ServerInstallationRepository { ); final serverIp = await getServerIpFromDomain(serverDomain); final apiResponse = await serverApi.authorizeDevice( - DeviceToken(device: await getDeviceName(), token: loginToken)); + DeviceToken(device: await getDeviceName(), token: newDeviceKey)); if (apiResponse.isSuccess) { return ServerHostingDetails( @@ -403,16 +403,16 @@ class ServerInstallationRepository { ); } - Future authorizeByRecoveryToken( + Future authorizeByRecoveryKey( ServerDomain serverDomain, - String recoveryToken, + String recoveryKey, ) async { var serverApi = ServerApi( isWithToken: false, overrideDomain: serverDomain.domainName, ); final apiResponse = await serverApi.useRecoveryToken( - DeviceToken(device: await getDeviceName(), token: recoveryToken)); + DeviceToken(device: await getDeviceName(), token: recoveryKey)); if (apiResponse.isSuccess) { return ServerHostingDetails( diff --git a/lib/logic/cubit/server_installation/server_installation_state.dart b/lib/logic/cubit/server_installation/server_installation_state.dart index b75b19d6..a318dd18 100644 --- a/lib/logic/cubit/server_installation/server_installation_state.dart +++ b/lib/logic/cubit/server_installation/server_installation_state.dart @@ -271,6 +271,12 @@ enum ServerRecoveryCapabilities { loginTokens, } +enum ServerRecoveryMethods { + newDeviceKey, + recoveryKey, + oldToken, +} + class ServerInstallationRecovery extends ServerInstallationState { final RecoveryStep currentStep; final ServerRecoveryCapabilities recoveryCapabilities; diff --git a/lib/logic/models/hive/server_details.g.dart b/lib/logic/models/hive/server_details.g.dart index 3d5ff9c1..f52e6b37 100644 --- a/lib/logic/models/hive/server_details.g.dart +++ b/lib/logic/models/hive/server_details.g.dart @@ -1,6 +1,6 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'server_details_screen.dart'; +part of 'server_details.dart'; // ************************************************************************** // TypeAdapterGenerator