diff --git a/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart b/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart index 477dbb888..7cfed0020 100644 --- a/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart +++ b/packages/at_persistence_secondary_server/lib/src/log/commitlog/commit_log_keystore.dart @@ -58,9 +58,11 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } /// Returns the latest committed sequence number with regex - Future lastCommittedSequenceNumberWithRegex(String regex) async { + Future lastCommittedSequenceNumberWithRegex(String regex, + {List? enrolledNamespace}) async { var lastCommittedEntry = (getBox() as Box).values.lastWhere( - (entry) => (_acceptKey(entry.atKey, regex)), + (entry) => (_acceptKey(entry.atKey, regex, + enrolledNamespace: enrolledNamespace)), orElse: () => NullCommitEntry()); var lastCommittedSequenceNum = (lastCommittedEntry != null) ? lastCommittedEntry.key : null; @@ -177,8 +179,30 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } } - bool _acceptKey(String atKey, String regex) { - return _isRegexMatches(atKey, regex) || _isSpecialKey(atKey); + bool _acceptKey(String atKey, String regex, + {List? enrolledNamespace}) { + return _isNamespaceAuthorised(atKey, enrolledNamespace) && + (_isRegexMatches(atKey, regex) || _isSpecialKey(atKey)); + } + + bool _isNamespaceAuthorised(String atKey, List? enrolledNamespace) { + // This is work-around for : https://github.com/atsign-foundation/at_server/issues/1570 + if (atKey == 'configkey') { + return true; + } + String? keyNamespace = AtKey.fromString(atKey).namespace; + // If enrolledNamespace is null or keyNamespace is null, fallback to + // existing behaviour - the key is authorized for the client to receive. So return true. + if (enrolledNamespace == null || + enrolledNamespace.isEmpty || + (keyNamespace == null || keyNamespace.isEmpty)) { + return true; + } + if (enrolledNamespace.contains('*') || + enrolledNamespace.contains(keyNamespace)) { + return true; + } + return false; } bool _isRegexMatches(String atKey, String regex) { @@ -186,10 +210,10 @@ class CommitLogKeyStore extends BaseCommitLogKeyStore { } bool _isSpecialKey(String atKey) { - return atKey.contains(AT_ENCRYPTION_SHARED_KEY) || + return atKey.contains(AtConstants.atEncryptionSharedKey) || atKey.startsWith('public:') || - atKey.contains(AT_PKAM_SIGNATURE) || - atKey.contains(AT_SIGNING_PRIVATE_KEY); + atKey.contains(AtConstants.atPkamSignature) || + atKey.contains(AtConstants.atSigningPrivateKey); } /// Returns the latest commitEntry of the key. diff --git a/packages/at_persistence_secondary_server/test/commit_log_test.dart b/packages/at_persistence_secondary_server/test/commit_log_test.dart index 9b8fb42fa..c393b0c42 100644 --- a/packages/at_persistence_secondary_server/test/commit_log_test.dart +++ b/packages/at_persistence_secondary_server/test/commit_log_test.dart @@ -133,10 +133,10 @@ void main() async { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { await commitLogKeystore.getBox().add(CommitEntry( - 'test_key_false_$i', CommitOp.UPDATE, DateTime.now())); + 'test_key_false_$i.wavi@alice', CommitOp.UPDATE, DateTime.now())); } else { await commitLogKeystore.getBox().add(CommitEntry( - 'test_key_false_$i', CommitOp.UPDATE, DateTime.now()) + 'test_key_false_$i.wavi@alice', CommitOp.UPDATE, DateTime.now()) ..commitId = i); } }