Skip to content

Commit

Permalink
Merge pull request #1374 from atsign-foundation/notify_response_trans…
Browse files Browse the repository at this point in the history
…former_fix

fix: Notify response transformer fix
  • Loading branch information
gkc authored Aug 13, 2024
2 parents 660ca57 + c7ceb27 commit 4497cfc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NotificationResponseTransformer
Tuple<AtNotification, NotificationConfig> tuple) async {
// prepare the atKey from the atNotification object.
AtNotification atNotification = tuple.one;
NotificationConfig config = tuple.two;
NotificationConfig notificationConfig = tuple.two;
String sharedBy = atNotification.from;
String sharedWith = atNotification.to;
var key = atNotification.key;
Expand Down Expand Up @@ -62,15 +62,19 @@ class NotificationResponseTransformer
var decryptedValue = await _getDecryptedValue(atKey, atKey.key);
atNotification.key = '${atNotification.to}:$decryptedValue';
return atNotification;
} else if ((atNotification.value.isNotNull) &&
(config.shouldDecrypt && atNotification.id != '-1') &&
}
if (atNotification.value.isNotNull &&
atNotification.id != '-1' &&
// The shared_key (which is a reserved key) has different decryption process
// and is not a user created key.
// Hence do not decrypt if key's are reserved keys
AtKey.getKeyType(atKey.toString()) != KeyType.reservedKey) {
// decrypt the value
atNotification.value =
await _getDecryptedValue(atKey, atNotification.value!);
// decrypt the notification value only if isEncrypted is not set to false and shouldDecrypt is set to true
if (atNotification.isEncrypted != false &&
notificationConfig.shouldDecrypt) {
atNotification.value =
await _getDecryptedValue(atKey, atNotification.value!);
}
return atNotification;
}
return atNotification;
Expand Down
58 changes: 55 additions & 3 deletions packages/at_client/test/notification_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,35 @@ void main() {
});

test(
'A test to verify notification key is decrypted when shouldDecrypt is set to true',
'A test to verify notification value is decrypted when isEncrypted is set to null',
() async {
var isEncrypted = false;
bool? isEncrypted;
var atNotification = at_notification.AtNotification(
'124',
'key-1',
'@alice',
'@bob',
DateTime.now().millisecondsSinceEpoch,
MessageTypeEnum.key.toString(),
isEncrypted,
value: 'encryptedValue');
var notificationResponseTransformer =
NotificationResponseTransformer(mockAtClientImpl);
notificationResponseTransformer.atKeyDecryption = mockSharedKeyDecryption;

var transformedNotification =
await notificationResponseTransformer.transform(Tuple()
..one = atNotification
..two = (NotificationConfig()
..regex = '.*'
..shouldDecrypt = true));
expect(transformedNotification.value, 'decryptedValue');
});

test(
'A test to verify notification value is decrypted when isEncrypted is set to true',
() async {
bool isEncrypted = true;
var atNotification = at_notification.AtNotification(
'124',
'key-1',
Expand All @@ -302,7 +328,7 @@ void main() {
});

test(
'A test to verify notification key is not decrypted when shouldDecrypt is set to false',
'A test to verify notification value is not decrypted when isEncrypted is set to false',
() async {
var isEncrypted = false;
var atNotification = at_notification.AtNotification(
Expand All @@ -327,6 +353,32 @@ void main() {
expect(transformedNotification.value, 'encryptedValue');
});

test(
'A test to verify notification value is not decrypted when isEncrypted is set to true and should decrypt is false',
() async {
var isEncrypted = true;
var atNotification = at_notification.AtNotification(
'124',
'key-1',
'@alice',
'@bob',
DateTime.now().millisecondsSinceEpoch,
MessageTypeEnum.key.toString(),
isEncrypted,
value: 'encryptedValue');
var notificationResponseTransformer =
NotificationResponseTransformer(mockAtClientImpl);
notificationResponseTransformer.atKeyDecryption = mockSharedKeyDecryption;

var transformedNotification =
await notificationResponseTransformer.transform(Tuple()
..one = atNotification
..two = (NotificationConfig()
..regex = '.*'
..shouldDecrypt = false));
expect(transformedNotification.value, 'encryptedValue');
});

test('A test to verify notification is returned as is', () async {
var isEncrypted = false;
var atNotification = at_notification.AtNotification(
Expand Down

0 comments on commit 4497cfc

Please sign in to comment.