Skip to content

Commit

Permalink
Refactor isSameTokenAs method for improved clarity and consistency ac…
Browse files Browse the repository at this point in the history
…ross token classes
  • Loading branch information
frankmer committed Dec 19, 2024
1 parent 985d301 commit 0f47c0b
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 23 deletions.
6 changes: 0 additions & 6 deletions lib/api/impl/privacy_idea_container_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/model/riverpod_states/token_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends Token>(T token) => tokens.firstWhereOrNull((element) => element.isSameTokenAs(token)) as T?;
T? currentOf<T extends Token>(T token) => tokens.firstWhereOrNull((element) => element.isSameTokenAs(token) == true) as T?;
T? currentOfId<T extends Token>(String id) => tokens.firstWhereOrNull((element) => element.id == id) as T?;

TokenState withToken(Token token) {
Expand Down
5 changes: 4 additions & 1 deletion lib/model/tokens/day_password_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/model/tokens/hotp_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions lib/model/tokens/otp_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/model/tokens/push_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 4 additions & 2 deletions lib/model/tokens/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/model/tokens/totp_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ class TokenNotifier extends _$TokenNotifier with ResultHandler {
List<Token> _filterDuplicates(List<Token> tokens) {
final uniqueTokens = <Token>[];
for (var token in tokens) {
if (!uniqueTokens.any((uniqureToken) => uniqureToken.isSameTokenAs(token))) {
if (!uniqueTokens.any((uniqureToken) => uniqureToken.isSameTokenAs(token) == true)) {
uniqueTokens.add(token);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class _ImportFileNoPwState extends ConsumerState<ImportPlainTokensPage> {
List<TokenImportEntry> _initBuildLists(List<TokenImportEntry> 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--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class _TokenImageState extends State<TokenImage> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 0f47c0b

Please sign in to comment.