2021-05-09 12:31:27 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/api/api_util.dart' as api_util;
|
2021-05-06 06:57:20 +02:00
|
|
|
import 'package:nc_photos/exception.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
abstract class AppPasswordExchangeBlocEvent {
|
|
|
|
const AppPasswordExchangeBlocEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppPasswordExchangeBlocConnect extends AppPasswordExchangeBlocEvent {
|
|
|
|
const AppPasswordExchangeBlocConnect(this.account);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"account: $account, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class AppPasswordExchangeBlocState {
|
|
|
|
const AppPasswordExchangeBlocState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppPasswordExchangeBlocInit extends AppPasswordExchangeBlocState {
|
|
|
|
const AppPasswordExchangeBlocInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppPasswordExchangeBlocSuccess extends AppPasswordExchangeBlocState {
|
|
|
|
const AppPasswordExchangeBlocSuccess(this.password);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"password: ${kDebugMode ? password : '***'}, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
final String password;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppPasswordExchangeBlocFailure extends AppPasswordExchangeBlocState {
|
|
|
|
const AppPasswordExchangeBlocFailure(this.exception);
|
|
|
|
|
|
|
|
@override
|
|
|
|
toString() {
|
|
|
|
return "$runtimeType {"
|
|
|
|
"exception: $exception, "
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:58:06 +02:00
|
|
|
final dynamic exception;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class AppPasswordExchangeBloc
|
|
|
|
extends Bloc<AppPasswordExchangeBlocEvent, AppPasswordExchangeBlocState> {
|
2022-07-09 07:59:09 +02:00
|
|
|
AppPasswordExchangeBloc() : super(const AppPasswordExchangeBlocInit()) {
|
|
|
|
on<AppPasswordExchangeBlocEvent>(_onEvent);
|
|
|
|
}
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2022-07-09 07:59:09 +02:00
|
|
|
Future<void> _onEvent(AppPasswordExchangeBlocEvent event,
|
|
|
|
Emitter<AppPasswordExchangeBlocState> emit) async {
|
|
|
|
_log.info("[_onEvent] $event");
|
2021-04-10 06:28:12 +02:00
|
|
|
if (event is AppPasswordExchangeBlocConnect) {
|
2022-07-09 07:59:09 +02:00
|
|
|
await _onEventConnect(event, emit);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 07:59:09 +02:00
|
|
|
Future<void> _onEventConnect(AppPasswordExchangeBlocConnect ev,
|
|
|
|
Emitter<AppPasswordExchangeBlocState> emit) async {
|
|
|
|
final account = ev.account;
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
final appPwd = await api_util.exchangePassword(account);
|
2022-07-09 07:59:09 +02:00
|
|
|
emit(AppPasswordExchangeBlocSuccess(appPwd));
|
2021-05-06 06:57:20 +02:00
|
|
|
} on InvalidBaseUrlException catch (e) {
|
|
|
|
_log.warning("[_exchangePassword] Invalid base url");
|
2022-07-09 07:59:09 +02:00
|
|
|
emit(AppPasswordExchangeBlocFailure(e));
|
2021-05-09 12:31:27 +02:00
|
|
|
} on HandshakeException catch (e) {
|
|
|
|
_log.info("[_exchangePassword] Self-signed cert");
|
2022-07-09 07:59:09 +02:00
|
|
|
emit(AppPasswordExchangeBlocFailure(e));
|
2021-04-10 06:28:12 +02:00
|
|
|
} catch (e, stacktrace) {
|
2021-05-07 21:22:21 +02:00
|
|
|
if (e is ApiException && e.response.statusCode == 401) {
|
|
|
|
// wrong password, normal
|
|
|
|
_log.warning(
|
|
|
|
"[_exchangePassword] Server response 401, wrong password?");
|
|
|
|
} else {
|
|
|
|
_log.shout("[_exchangePassword] Failed while exchanging password", e,
|
|
|
|
stacktrace);
|
|
|
|
}
|
2022-07-09 07:59:09 +02:00
|
|
|
emit(AppPasswordExchangeBlocFailure(e));
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _log =
|
|
|
|
Logger("bloc.app_password_exchange.AppPasswordExchangeBloc");
|
|
|
|
}
|