Skip to content

Commit 1d9dfea

Browse files
smk762CharlVS
andauthored
Fix failing validation on trezor login (#2740)
* fix pwd validation fail on hidden wallet * fix default pass & ensure auto active coins register * use secure random password * refactor: migrate password generation to SDK --------- Co-authored-by: CharlVS <[email protected]>
1 parent 79eabfe commit 1d9dfea

File tree

2 files changed

+11
-83
lines changed

2 files changed

+11
-83
lines changed

lib/bloc/trezor_init_bloc/trezor_init_bloc.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import 'dart:async';
2-
32
import 'package:easy_localization/easy_localization.dart';
43
import 'package:equatable/equatable.dart';
54
import 'package:flutter_bloc/flutter_bloc.dart';
65
import 'package:komodo_defi_sdk/komodo_defi_sdk.dart';
76
import 'package:komodo_defi_types/komodo_defi_types.dart';
7+
import 'package:web_dex/app_config/app_config.dart';
8+
import 'package:web_dex/shared/utils/password.dart';
89
import 'package:web_dex/bloc/coins_bloc/coins_repo.dart';
910
import 'package:web_dex/bloc/trezor_bloc/trezor_repo.dart';
1011
import 'package:web_dex/generated/codegen_loader.g.dart';
@@ -200,6 +201,7 @@ class TrezorInitBloc extends Bloc<TrezorInitEvent, TrezorInitState> {
200201
// ignore
201202
}
202203
_trezorRepo.subscribeOnConnectionStatus(deviceDetails.pubKey);
204+
await _kdfSdk.addActivatedCoins(enabledByDefaultTrezorCoins);
203205
emit(
204206
state.copyWith(
205207
inProgress: () => false,
@@ -273,8 +275,9 @@ class TrezorInitBloc extends Bloc<TrezorInitEvent, TrezorInitState> {
273275
/// into a static 'hidden' wallet to init trezor
274276
Future<void> _loginToTrezorWallet({
275277
String walletName = 'My Trezor',
276-
String password = 'hidden-login',
278+
String? password
277279
}) async {
280+
password ??= generatePassword();
278281
final bool mm2SignedIn = await _kdfSdk.auth.isSignedIn();
279282
if (state.kdfUser != null && mm2SignedIn) {
280283
return;

lib/shared/utils/password.dart

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,9 @@
1-
import 'dart:math';
1+
import 'package:komodo_defi_types/komodo_defi_type_utils.dart';
22

3-
String generatePassword() {
4-
final List<String> passwords = [];
5-
6-
final rng = Random.secure();
7-
8-
const String lowerCase = 'abcdefghijklmnopqrstuvwxyz';
9-
const String upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
10-
const String digit = '0123456789';
11-
const String punctuation = '*.!@#\$%^(){}:;\',.?/~`_+\\-=|';
12-
13-
final string = [lowerCase, upperCase, digit, punctuation];
14-
15-
final length = rng.nextInt(24) + 8;
16-
17-
final List<String> tab = [];
18-
19-
while (true) {
20-
// This loop make sure the new RPC password will contains all the requirement
21-
// characters type in password, it generate automatically the position.
22-
tab.clear();
23-
for (var x = 0; x < length; x++) {
24-
tab.add(string[rng.nextInt(4)]);
25-
}
26-
27-
if (tab.contains(lowerCase) &&
28-
tab.contains(upperCase) &&
29-
tab.contains(digit) &&
30-
tab.contains(punctuation)) {
31-
break;
32-
}
33-
}
34-
35-
for (int i = 0; i < tab.length; i++) {
36-
// Here we constitute new RPC password, and check the repetition.
37-
final chars = tab[i];
38-
final character = chars[rng.nextInt(chars.length)];
39-
final count = passwords.where((c) => c == character).toList().length;
40-
if (count < 2) {
41-
passwords.add(character);
42-
} else {
43-
tab.add(chars);
44-
}
45-
}
46-
47-
return passwords.join('');
48-
}
3+
/// Generates a password that meets the KDF password policy requirements using
4+
/// the device's secure random number generator.
5+
String generatePassword() => SecurityUtils.generatePasswordSecure(16);
496

507
/// unit tests: [testValidateRPCPassword]
51-
bool validateRPCPassword(String src) {
52-
if (src.isEmpty) return false;
53-
54-
// Password can't contain word 'password'
55-
if (src.toLowerCase().contains('password')) return false;
56-
57-
// Password must contain one digit, one lowercase letter, one uppercase letter,
58-
// one special character and its length must be between 8 and 32 characters
59-
final RegExp exp = RegExp(
60-
r'^(?:(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^A-Za-z0-9])).{8,32}$',
61-
);
62-
if (!src.contains(exp)) return false;
63-
64-
// Password can't contain same character three time in a row,
65-
// so some code below to check that:
66-
67-
// MRC: Divide the password into all possible 3 character blocks
68-
final pieces = <String>[];
69-
for (int start = 0, end = 3; end <= src.length; start += 1, end += 1) {
70-
pieces.add(src.substring(start, end));
71-
}
72-
73-
// If, for any block, all 3 character are the same, block doesn't fit criteria
74-
for (String p in pieces) {
75-
final src = p[0];
76-
int count = 1;
77-
if (p[1] == src) count += 1;
78-
if (p[2] == src) count += 1;
79-
80-
if (count == 3) return false;
81-
}
82-
83-
return true;
84-
}
8+
bool validateRPCPassword(String src) =>
9+
SecurityUtils.checkPasswordRequirements(src).isValid;

0 commit comments

Comments
 (0)