2020-12-30 14:13:25 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:cubit_form/cubit_form.dart';
|
2022-02-16 07:28:29 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
2022-05-17 13:31:34 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
2022-05-02 11:56:46 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
2020-12-30 14:13:25 +00:00
|
|
|
|
|
|
|
class HetznerFormCubit extends FormCubit {
|
2022-05-19 17:43:25 +00:00
|
|
|
HetznerFormCubit(this.serverInstallationCubit) {
|
2021-01-15 12:24:09 +00:00
|
|
|
var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
2020-12-30 14:13:25 +00:00
|
|
|
apiKey = FieldCubit(
|
|
|
|
initalValue: '',
|
|
|
|
validations: [
|
2021-06-20 21:08:52 +00:00
|
|
|
RequiredStringValidation('validations.required'.tr()),
|
2020-12-30 14:13:25 +00:00
|
|
|
ValidationModel<String>(
|
2021-06-20 21:08:52 +00:00
|
|
|
(s) => regExp.hasMatch(s), 'validations.key_format'.tr()),
|
2022-05-02 11:56:46 +00:00
|
|
|
LengthStringNotEqualValidation(64)
|
2020-12-30 14:13:25 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2021-03-23 19:21:42 +00:00
|
|
|
super.addFields([apiKey]);
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
FutureOr<void> onSubmit() async {
|
2022-05-19 17:43:25 +00:00
|
|
|
serverInstallationCubit.setHetznerKey(apiKey.state.value);
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 17:43:25 +00:00
|
|
|
final ServerInstallationCubit serverInstallationCubit;
|
2020-12-30 14:13:25 +00:00
|
|
|
|
2021-03-15 15:39:44 +00:00
|
|
|
late final FieldCubit<String> apiKey;
|
2020-12-30 14:13:25 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
@override
|
|
|
|
FutureOr<bool> asyncValidation() async {
|
2021-03-15 15:39:44 +00:00
|
|
|
late bool isKeyValid;
|
2021-03-25 23:30:34 +00:00
|
|
|
HetznerApi apiClient = HetznerApi(isWithToken: false);
|
|
|
|
|
2021-01-13 16:45:46 +00:00
|
|
|
try {
|
|
|
|
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
|
|
|
} catch (e) {
|
|
|
|
addError(e);
|
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
if (!isKeyValid) {
|
|
|
|
apiKey.setError('bad key');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-30 14:13:25 +00:00
|
|
|
}
|