From 0f47c0b7ea6bffe6bf61c7d504f94b58793e807c Mon Sep 17 00:00:00 2001 From: Frank Merkel <138444693+frankmer@users.noreply.github.com> Date: Thu, 19 Dec 2024 10:41:33 +0100 Subject: [PATCH] Refactor isSameTokenAs method for improved clarity and consistency across token classes --- lib/api/impl/privacy_idea_container_api.dart | 6 ------ lib/model/riverpod_states/token_state.dart | 4 ++-- lib/model/tokens/day_password_token.dart | 5 ++++- lib/model/tokens/hotp_token.dart | 6 +++++- lib/model/tokens/otp_token.dart | 9 +++++++-- lib/model/tokens/push_token.dart | 10 +++++++++- lib/model/tokens/token.dart | 6 ++++-- lib/model/tokens/totp_token.dart | 7 ++++++- .../generated_providers/token_notifier.dart | 2 +- .../pages/import_plain_tokens_page.dart | 2 +- .../main_view_widgets/token_widgets/token_image.dart | 5 +++++ .../token_widgets/token_widget_tile.dart | 12 +++++++----- 12 files changed, 51 insertions(+), 23 deletions(-) diff --git a/lib/api/impl/privacy_idea_container_api.dart b/lib/api/impl/privacy_idea_container_api.dart index da478d9a..10e81869 100644 --- a/lib/api/impl/privacy_idea_container_api.dart +++ b/lib/api/impl/privacy_idea_container_api.dart @@ -288,13 +288,7 @@ class PiContainerApi implements TokenContainerApi { required SimpleKeyPair encKeyPair, }) async { final publicKey = await encKeyPair.extractPublicKey(); - final privateKeyBytes = await encKeyPair.extractPrivateKeyBytes(); - final publicKeyBase64 = base64.encode(publicKey.bytes); - - Logger.warning('Public key base64: $publicKeyBase64'); - Logger.warning('Private key bytes: ${base64.encode(privateKeyBytes)}'); - final containerDict = { TokenContainer.DICT_SERIAL: container.serial, TokenContainer.DICT_TYPE: TokenContainer.DICT_TYPE_SMARTPHONE, diff --git a/lib/model/riverpod_states/token_state.dart b/lib/model/riverpod_states/token_state.dart index d1f600de..98a8227c 100644 --- a/lib/model/riverpod_states/token_state.dart +++ b/lib/model/riverpod_states/token_state.dart @@ -71,12 +71,12 @@ class TokenState { final stateTokens = this.tokens; for (var token in tokens) { - sameTokensMap[token] = stateTokens.firstWhereOrNull((element) => element.isSameTokenAs(token)); + sameTokensMap[token] = stateTokens.firstWhereOrNull((element) => element.isSameTokenAs(token) == true); } return sameTokensMap; } - T? currentOf(T token) => tokens.firstWhereOrNull((element) => element.isSameTokenAs(token)) as T?; + T? currentOf(T token) => tokens.firstWhereOrNull((element) => element.isSameTokenAs(token) == true) as T?; T? currentOfId(String id) => tokens.firstWhereOrNull((element) => element.id == id) as T?; TokenState withToken(Token token) { diff --git a/lib/model/tokens/day_password_token.dart b/lib/model/tokens/day_password_token.dart index f76e84ec..e22482cf 100644 --- a/lib/model/tokens/day_password_token.dart +++ b/lib/model/tokens/day_password_token.dart @@ -74,7 +74,10 @@ class DayPasswordToken extends OTPToken { @override // It is the same token the the period as to be the same bool isSameTokenAs(Token other) { - return super.isSameTokenAs(other) && other is DayPasswordToken && other.period == period; + if (super.isSameTokenAs(other) != null) return super.isSameTokenAs(other)!; + if (other is! DayPasswordToken) return false; + if (period != other.period) return false; + return true; } @override diff --git a/lib/model/tokens/hotp_token.dart b/lib/model/tokens/hotp_token.dart index 627c3d4f..1ee20c71 100644 --- a/lib/model/tokens/hotp_token.dart +++ b/lib/model/tokens/hotp_token.dart @@ -69,7 +69,11 @@ class HOTPToken extends OTPToken { @override // Counter can be changed even if its the same token - bool isSameTokenAs(Token other) => super.isSameTokenAs(other) && other is HOTPToken; + bool isSameTokenAs(Token other) { + if (super.isSameTokenAs(other) != null) return super.isSameTokenAs(other)!; + if (other is! HOTPToken) return false; + return true; + } @override String get otpValue => algorithm.generateHOTPCodeString( diff --git a/lib/model/tokens/otp_token.dart b/lib/model/tokens/otp_token.dart index 810f55c0..002d6493 100644 --- a/lib/model/tokens/otp_token.dart +++ b/lib/model/tokens/otp_token.dart @@ -78,8 +78,13 @@ abstract class OTPToken extends Token { // bool sameValuesAs(Token other) => super.sameValuesAs(other); @override - bool isSameTokenAs(Token other) { - return super.isSameTokenAs(other) && (other is OTPToken && other.secret == secret) && other.algorithm == algorithm && other.digits == digits; + bool? isSameTokenAs(Token other) { + if (other is! OTPToken) return false; + if (super.isSameTokenAs(other) != null) return super.isSameTokenAs(other)!; + if (secret != other.secret) return false; + if (algorithm != other.algorithm) return false; + if (digits != other.digits) return false; + return null; } @override diff --git a/lib/model/tokens/push_token.dart b/lib/model/tokens/push_token.dart index 75397468..6264b2c9 100644 --- a/lib/model/tokens/push_token.dart +++ b/lib/model/tokens/push_token.dart @@ -133,7 +133,15 @@ class PushToken extends Token { other.privateTokenKey == privateTokenKey; @override - bool isSameTokenAs(Token other) => super.isSameTokenAs(other) && (other is PushToken && other.serial == serial); + bool isSameTokenAs(Token other) { + if (super.isSameTokenAs(other) != null) return super.isSameTokenAs(other)!; + if (other is! PushToken) return false; + if (publicServerKey != null && other.publicServerKey != null && publicServerKey != other.publicServerKey) return false; + if (publicTokenKey != null && other.publicTokenKey != null && publicTokenKey != other.publicTokenKey) return false; + if (privateTokenKey != null && other.privateTokenKey != null && privateTokenKey != other.privateTokenKey) return false; + if (enrollmentCredentials != null && other.enrollmentCredentials != null && enrollmentCredentials != other.enrollmentCredentials) return false; + return true; + } @override PushToken copyWith({ diff --git a/lib/model/tokens/token.dart b/lib/model/tokens/token.dart index 32d42283..bcc76dbe 100644 --- a/lib/model/tokens/token.dart +++ b/lib/model/tokens/token.dart @@ -162,8 +162,10 @@ abstract class Token with SortableMixin { other.label == label && other.issuer == issuer && other.pin == pin && other.isLocked == isLocked && other.tokenImage == tokenImage; /// This is used to identify the same token even if the id is different. - bool isSameTokenAs(Token other) { - return (other.id == id && other.type == type) || (other.serial == serial && other.type == type); + /// + bool? isSameTokenAs(Token other) { + if (serial != null && serial == other.serial && issuer == other.issuer) return true; + return null; } @override diff --git a/lib/model/tokens/totp_token.dart b/lib/model/tokens/totp_token.dart index 36da9117..09cb4750 100644 --- a/lib/model/tokens/totp_token.dart +++ b/lib/model/tokens/totp_token.dart @@ -91,7 +91,12 @@ class TOTPToken extends OTPToken { // bool sameValuesAs(Token other) => super.sameValuesAs(other); @override - bool isSameTokenAs(Token other) => super.isSameTokenAs(other) && other is TOTPToken && other.period == period; + bool isSameTokenAs(Token other) { + if (super.isSameTokenAs(other) != null) return super.isSameTokenAs(other)!; + if (other is! TOTPToken) return false; + if (period != other.period) return false; + return true; + } @override TOTPToken copyWith({ diff --git a/lib/utils/riverpod/riverpod_providers/generated_providers/token_notifier.dart b/lib/utils/riverpod/riverpod_providers/generated_providers/token_notifier.dart index 0300b34c..76755db9 100644 --- a/lib/utils/riverpod/riverpod_providers/generated_providers/token_notifier.dart +++ b/lib/utils/riverpod/riverpod_providers/generated_providers/token_notifier.dart @@ -815,7 +815,7 @@ class TokenNotifier extends _$TokenNotifier with ResultHandler { List _filterDuplicates(List tokens) { final uniqueTokens = []; for (var token in tokens) { - if (!uniqueTokens.any((uniqureToken) => uniqureToken.isSameTokenAs(token))) { + if (!uniqueTokens.any((uniqureToken) => uniqureToken.isSameTokenAs(token) == true)) { uniqueTokens.add(token); } } diff --git a/lib/views/import_tokens_view/pages/import_plain_tokens_page.dart b/lib/views/import_tokens_view/pages/import_plain_tokens_page.dart index 951b83d4..f54f05a2 100644 --- a/lib/views/import_tokens_view/pages/import_plain_tokens_page.dart +++ b/lib/views/import_tokens_view/pages/import_plain_tokens_page.dart @@ -82,7 +82,7 @@ class _ImportFileNoPwState extends ConsumerState { List _initBuildLists(List importTokenEntrys) { for (var i = 0; i < importTokenEntrys.length; i++) { final importTokenEntry = importTokenEntrys[i]; - if ([...newImports, ...appDuplicates, ...conflictedImports].any((import) => import.newToken.isSameTokenAs(importTokenEntry.newToken))) { + if ([...newImports, ...appDuplicates, ...conflictedImports].any((import) => import.newToken.isSameTokenAs(importTokenEntry.newToken) == true)) { importDuplicates.add(importTokenEntry); importTokenEntrys.remove(importTokenEntry); i--; diff --git a/lib/views/main_view/main_view_widgets/token_widgets/token_image.dart b/lib/views/main_view/main_view_widgets/token_widgets/token_image.dart index 57fe15ba..f5232626 100644 --- a/lib/views/main_view/main_view_widgets/token_widgets/token_image.dart +++ b/lib/views/main_view/main_view_widgets/token_widgets/token_image.dart @@ -44,6 +44,11 @@ class _TokenImageState extends State { tokenImages[tokenImageUrl] = null; return null; } + final uri = Uri.parse(tokenImageUrl); + if (uri.host == '') { + tokenImages[tokenImageUrl] = null; + return null; + } http.Response response = await http.get(Uri.parse(tokenImageUrl)); final newTokenImage = response.bodyBytes; tokenImages[tokenImageUrl] = newTokenImage; diff --git a/lib/views/main_view/main_view_widgets/token_widgets/token_widget_tile.dart b/lib/views/main_view/main_view_widgets/token_widgets/token_widget_tile.dart index 2ecb4788..7985beb0 100644 --- a/lib/views/main_view/main_view_widgets/token_widgets/token_widget_tile.dart +++ b/lib/views/main_view/main_view_widgets/token_widgets/token_widget_tile.dart @@ -98,11 +98,13 @@ class TokenWidgetTile extends ConsumerWidget { if (subtitle2.isNotEmpty) Row( children: [ - Text( - subtitle2, - textAlign: TextAlign.left, - overflow: TextOverflow.fade, - softWrap: false, + Flexible( + child: Text( + subtitle2, + textAlign: TextAlign.left, + overflow: TextOverflow.fade, + softWrap: false, + ), ), SizedBox(width: 6), ContainerTokenSyncIcon(token),