Commit af2531a2 authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

imeplement new seed system for verification

parent 6892e852
......@@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';
import 'package:bloc/bloc.dart';
import 'package:connectivity/connectivity.dart';
import 'package:digital_3g_common/verification_api.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
......@@ -49,7 +50,8 @@ class VerificationBloc extends Bloc<VerificationEvent, VerificationState> {
_verification = response!;
await _verificationStore!.writeAsString(json.encode(_verification!.toMap));
yield VerificationState.verified;
} else if (_verificationStore?.existsSync() ?? false) {
} else if (((await Connectivity().checkConnectivity()) == ConnectivityResult.none) &&
(_verificationStore?.existsSync() ?? false)) {
final loaded =
VerificationResponse.fromMap(json.decode(await _verificationStore!.readAsString()));
if (loaded.timestamp.isAfter(DateTime.now().subtract(const Duration(minutes: 30)))) {
......@@ -69,11 +71,26 @@ class VerificationBloc extends Bloc<VerificationEvent, VerificationState> {
yield VerificationState.noTicket;
break;
case EventType.add:
// get ticket from backend if longer than 728 (backwards compatibility)
String? ticket;
if ((event.seed?.length ?? 0) > 768) {
try {
ticket = await _api!.generateTicket(event.seed!);
} catch (e) {
_log.severe('error while generating ticket');
yield VerificationState.error;
}
} else {
ticket = event.seed;
}
// save ticket
if (!_ticketStore!.existsSync()) {
await _ticketStore!.create(recursive: true);
}
if (event.ticket != null && event.ticket!.isNotEmpty) {
_ticket = event.ticket;
if (ticket != null && ticket.isNotEmpty) {
_ticket = ticket;
await _ticketStore!.writeAsString(_ticket!, flush: true);
yield VerificationState.unverified;
add(VerificationEvent.check());
......@@ -124,18 +141,18 @@ class VerificationBloc extends Bloc<VerificationEvent, VerificationState> {
class VerificationEvent {
final EventType type;
final String? ticket;
final String? seed;
VerificationEvent({required this.type, this.ticket});
VerificationEvent({required this.type, this.seed});
factory VerificationEvent.add(String ticket) =>
VerificationEvent(type: EventType.add, ticket: ticket);
VerificationEvent(type: EventType.add, seed: ticket);
factory VerificationEvent.check() => VerificationEvent(type: EventType.check);
factory VerificationEvent.revoke() => VerificationEvent(type: EventType.revoke);
Map get toMap => {'type': type, 'ticket': ticket};
Map get toMap => {'type': type, 'seed': seed};
@override
String toString() => '[VerificationEvent $toMap]';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment