Skip to content

Commit

Permalink
fix #28
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-convertino committed Jan 28, 2024
1 parent b2d117e commit d3c1371
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/bloc/manual/manual_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:diacritic/diacritic.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:otp_manager/utils/base32.dart';

Expand All @@ -10,7 +11,6 @@ import 'manual_state.dart';

class ManualBloc extends Bloc<ManualEvent, ManualState> {
final Account? account;
//final Box<Account> _accountBox = objectBox.store.box<Account>();
final LocalRepositoryImpl localRepositoryImpl;

ManualBloc({this.account, required this.localRepositoryImpl})
Expand All @@ -32,8 +32,8 @@ class ManualBloc extends Bloc<ManualEvent, ManualState> {
}

void _onAddOrEditAccount(AddOrEditAccount event, Emitter<ManualState> emit) {
String name = Uri.decodeFull(state.name.trim());
String issuer = Uri.decodeFull(state.issuer.trim());
String name = Uri.decodeFull(removeDiacritics(state.name.trim()));
String issuer = Uri.decodeFull(removeDiacritics(state.issuer.trim()));
String secretKey = state.secretKey.trim().toUpperCase();

if (name.isEmpty) {
Expand Down
5 changes: 2 additions & 3 deletions lib/bloc/qr_code_scanner/qr_code_scanner_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class QrCodeScannerBloc extends Bloc<QrCodeScannerEvent, QrCodeScannerState> {

if (!atLeastOneAdded) {
emit(state.copyWith(
error: newAccounts.length > 1
? "These accounts are already registered"
: "This account is already registered"));
error:
"${newAccounts.length > 1 ? "These accounts are already registered" : "This account is already registered"}.\nMake sure you are in sync and try again."));
} else {
emit(state.copyWith(
addWithSuccess: newAccounts.length > 1
Expand Down
2 changes: 1 addition & 1 deletion lib/models/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Account {

if(icon != null && icon != "default") {
iconKey = icon;
} else if(issuer != null) {
} else if(issuer != null && issuer != "") {
String toFind = issuer!.replaceAll(" ", "").toLowerCase();
iconKey = simpleIcons.keys.firstWhere((v) => v.contains(toFind), orElse: () => "default");
} else {
Expand Down
22 changes: 12 additions & 10 deletions lib/screens/qr_code_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ class QrCodeScanner extends HookWidget {
alignment: Alignment.center,
children: [
MobileScanner(
controller: _cameraController,
onDetect: (Barcode barcode, MobileScannerArguments? args) {
if (UriDecoder.isValid(barcode.rawValue!)) {
context.read<QrCodeScannerBloc>().add(
DecodeAndStoreAccounts(accounts: barcode.rawValue!));
} else {
context.read<QrCodeScannerBloc>().add(const ErrorChanged(
error: "The QR code is not correct"));
}
}),
controller: _cameraController,
onDetect: (Barcode barcode, MobileScannerArguments? args) {
if (UriDecoder.isValid(barcode.rawValue!)) {
context.read<QrCodeScannerBloc>().add(
DecodeAndStoreAccounts(accounts: barcode.rawValue!));
} else {
context.read<QrCodeScannerBloc>().add(const ErrorChanged(
error: "The QR code is not correct"));
}
},
),
Container(
decoration: ShapeDecoration(
shape: QrScannerOverlayShape(
Expand All @@ -80,6 +81,7 @@ class QrCodeScanner extends HookWidget {
child: Text(
state.error,
style: const TextStyle(fontSize: 18, color: Colors.white),
textAlign: TextAlign.center,
),
),
],
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/uri_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:base32/base32.dart';
import 'package:diacritic/diacritic.dart';
import 'package:otp/otp.dart';

import '../../generated_protoc/google_auth.pb.dart';
Expand Down Expand Up @@ -80,15 +81,15 @@ class UriDecoder {

payload.otpParameters.asMap().forEach((index, params) {
var tmp = params.toProto3Json() as Map;
tmp["name"] = Uri.decodeFull(tmp["name"].toString());
tmp["name"] = Uri.decodeFull(removeDiacritics(tmp["name"].toString()));

var newAccount = Account(
secret: base32
.encode(Uint8List.fromList(payload.otpParameters[index].secret))
.toUpperCase(),
name:
tmp["name"].contains(':') ? tmp["name"].split(':')[1] : tmp["name"],
issuer: Uri.decodeFull(tmp["issuer"] ?? ""),
issuer: Uri.decodeFull(removeDiacritics(tmp["issuer"] ?? "")),
dbAlgorithm: getAlgorithmFromString(tmp["algorithm"]),
digits: 6,
type: tmp["type"],
Expand Down Expand Up @@ -129,8 +130,8 @@ class UriDecoder {

var newAccount = Account(
secret: tmp["secret"].toString().toUpperCase(),
name: Uri.decodeFull(nameAndIssuer["name"]),
issuer: Uri.decodeFull(nameAndIssuer["issuer"]),
name: removeDiacritics(Uri.decodeFull(nameAndIssuer["name"])),
issuer: removeDiacritics(Uri.decodeFull(nameAndIssuer["issuer"] ?? "")),
dbAlgorithm: getAlgorithmFromString(tmp["algorithm"].toString()),
digits: int.tryParse(tmp["digits"].toString()),
type: uriDecoded.host,
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.4.3+19
version: 1.4.4+20

environment:
sdk: ">=2.16.2 <3.0.0"
Expand Down Expand Up @@ -76,6 +76,7 @@ dependencies:
flutter_animated_icon_button: ^1.0.0
arrow_path: ^3.1.0
pretty_qr_code: ^3.1.0
diacritic: ^0.1.5

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit d3c1371

Please sign in to comment.