Add cubit methods to try recover the server

pull/90/head
Inex Code 2022-05-18 14:21:11 +03:00
parent d2553b0d08
commit df40a09419
4 changed files with 67 additions and 8 deletions

View File

@ -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<ServerInstallationState> {
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<ServerHostingDetails> 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();

View File

@ -371,9 +371,9 @@ class ServerInstallationRepository {
return 'Unidentified';
}
Future<ServerHostingDetails> authorizeByLoginToken(
Future<ServerHostingDetails> 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<ServerHostingDetails> authorizeByRecoveryToken(
Future<ServerHostingDetails> 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(

View File

@ -271,6 +271,12 @@ enum ServerRecoveryCapabilities {
loginTokens,
}
enum ServerRecoveryMethods {
newDeviceKey,
recoveryKey,
oldToken,
}
class ServerInstallationRecovery extends ServerInstallationState {
final RecoveryStep currentStep;
final ServerRecoveryCapabilities recoveryCapabilities;

View File

@ -1,6 +1,6 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'server_details_screen.dart';
part of 'server_details.dart';
// **************************************************************************
// TypeAdapterGenerator