diff --git a/packages/at_secondary_server/lib/src/verb/handler/monitor_verb_handler.dart b/packages/at_secondary_server/lib/src/verb/handler/monitor_verb_handler.dart index 3f79b7b82..c5f383674 100644 --- a/packages/at_secondary_server/lib/src/verb/handler/monitor_verb_handler.dart +++ b/packages/at_secondary_server/lib/src/verb/handler/monitor_verb_handler.dart @@ -138,10 +138,14 @@ class MonitorVerbHandler extends AbstractVerbHandler { "skeEncKeyName": atNotification.atMetadata?.skeEncKeyName, "skeEncAlgo": atNotification.atMetadata?.skeEncAlgo, "sharedKeyEnc": atNotification.atMetadata?.sharedKeyEnc, - "pubKeyHash": - jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson()) }; + notification.metadata?.putIfAbsent( + "pubKeyHash", + () => (atNotification.atMetadata?.pubKeyHash != null) + ? jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson()) + : null); + await _checkAndSend(notification); } } @@ -224,9 +228,13 @@ class Notification { "availableAt": atNotification.atMetadata?.availableAt.toString(), "expiresAt": (atNotification.atMetadata?.expiresAt ?? atNotification.expiresAt) - .toString(), - 'pubKeyHash': jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson()) + .toString() }; + metadata?.putIfAbsent( + "pubKeyHash", + () => (atNotification.atMetadata?.pubKeyHash != null) + ? jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson()) + : null); } Map toJson() => { diff --git a/packages/at_secondary_server/test/monitor_verb_test.dart b/packages/at_secondary_server/test/monitor_verb_test.dart index a99eca11b..17ddbe476 100644 --- a/packages/at_secondary_server/test/monitor_verb_test.dart +++ b/packages/at_secondary_server/test/monitor_verb_test.dart @@ -1,6 +1,7 @@ import 'dart:collection'; import 'dart:convert'; +import 'package:at_chops/at_chops.dart'; import 'package:at_commons/at_commons.dart'; import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; import 'package:at_secondary/src/connection/inbound/dummy_inbound_connection.dart'; @@ -105,6 +106,69 @@ void main() { expect(notificationMap['messageType'], 'MessageType.key'); expect(notificationMap['operation'], 'update'); }); + + test( + 'A test to verify publicKeyHash is written on InboundConnection when populated', + () async { + HashMap verbParams = HashMap(); + verbParams[AtConstants.regex] = 'wavi'; + inboundConnection.metaData.isAuthenticated = true; + MonitorVerbHandler monitorVerbHandler = + MonitorVerbHandler(secondaryKeyStore); + await monitorVerbHandler.processVerb( + Response(), verbParams, inboundConnection); + + var atNotification = (AtNotificationBuilder() + ..id = 'abc' + ..fromAtSign = '@bob' + ..notificationDateTime = DateTime.now() + ..toAtSign = alice + ..notification = 'phone.wavi' + ..type = NotificationType.received + ..opType = OperationType.update + ..messageType = MessageType.key + ..atMetaData = (AtMetaData() + ..pubKeyHash = + PublicKeyHash('dummy_hash', HashingAlgoType.sha512.name))) + .build(); + await monitorVerbHandler.processAtNotification(atNotification); + inboundConnection.lastWrittenData = inboundConnection.lastWrittenData + ?.replaceAll('notification:', '') + .trim(); + Map notificationMap = jsonDecode(inboundConnection.lastWrittenData!); + expect(notificationMap['metadata']['pubKeyHash'], + '{"hash":"dummy_hash","hashingAlgo":"sha512"}'); + }); + + test( + 'A test to verify publicKeyHash is set to null on InboundConnection when not populated', + () async { + HashMap verbParams = HashMap(); + verbParams[AtConstants.regex] = 'wavi'; + inboundConnection.metaData.isAuthenticated = true; + MonitorVerbHandler monitorVerbHandler = + MonitorVerbHandler(secondaryKeyStore); + await monitorVerbHandler.processVerb( + Response(), verbParams, inboundConnection); + + var atNotification = (AtNotificationBuilder() + ..id = 'abc' + ..fromAtSign = '@bob' + ..notificationDateTime = DateTime.now() + ..toAtSign = alice + ..notification = 'phone.wavi' + ..type = NotificationType.received + ..opType = OperationType.update + ..messageType = MessageType.key + ..atMetaData = (AtMetaData()..encKeyName = 'encKeyName')) + .build(); + await monitorVerbHandler.processAtNotification(atNotification); + inboundConnection.lastWrittenData = inboundConnection.lastWrittenData + ?.replaceAll('notification:', '') + .trim(); + var notificationMap = jsonDecode(inboundConnection.lastWrittenData!); + expect(notificationMap['metadata']['pubKeyHash'], null); + }); tearDown(() async { await verbTestsTearDown(); AtNotificationCallback.getInstance().callbackMethods.clear();