Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: at_commons changes for setting isEncrypted for both true and false #669

Merged
merged 6 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/at_commons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 4.1.3
- feat: Add isEncrypted flag to update verb metadata for both true and false
- fix: update pkam regex to accept sha512 as hashing algo
## 4.1.2
- feat: Add "expiry" enroll params to support apkam keys to auto expiry after specified time duration
Expand Down
6 changes: 3 additions & 3 deletions packages/at_commons/lib/src/keystore/at_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ class Metadata {
if (isBinary) {
sb.write(':isBinary:$isBinary');
}
if (isEncrypted) {
sb.write(':isEncrypted:$isEncrypted');
}

sb.write(':isEncrypted:$isEncrypted');

if (sharedKeyEnc.isNotNullOrEmpty) {
sb.write(':${AtConstants.sharedKeyEncrypted}:$sharedKeyEnc');
}
Expand Down
73 changes: 1 addition & 72 deletions packages/at_commons/lib/src/verb/notify_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:at_commons/src/keystore/at_key.dart';
import 'package:at_commons/src/utils/string_utils.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';
import 'package:uuid/uuid.dart';

Expand Down Expand Up @@ -61,7 +59,7 @@ class NotifyVerbBuilder extends AbstractVerbBuilder {
}

// Add in all of the metadata parameters in atProtocol command format
sb.write(_toAtProtocolFragment(atKey.metadata));
sb.write(atKey.metadata.toAtProtocolFragment());

if (atKey.sharedWith != null) {
sb.write(':${VerbUtil.formatAtSign(atKey.sharedWith)}');
Expand All @@ -84,75 +82,6 @@ class NotifyVerbBuilder extends AbstractVerbBuilder {
return sb.toString();
}

// temporary method till isEncrypted flag changes are done for update verb.
// TODO Remove this and use at_key.metadata.toProtocolFragment
String _toAtProtocolFragment(Metadata metadata) {
StringBuffer sb = StringBuffer();

// NB The order of the verb parameters is important - it MUST match the order
// in the regular expressions [VerbSyntax.update] and [VerbSyntax.update_meta]
if (metadata.ttl != null) {
sb.write(':ttl:${metadata.ttl}');
}
if (metadata.ttb != null) {
sb.write(':ttb:${metadata.ttb}');
}
if (metadata.ttr != null) {
sb.write(':ttr:${metadata.ttr}');
}
if (metadata.ccd != null) {
sb.write(':ccd:${metadata.ccd}');
}
if (metadata.dataSignature.isNotNullOrEmpty) {
sb.write(':${AtConstants.publicDataSignature}:${metadata.dataSignature}');
}
if (metadata.sharedKeyStatus.isNotNullOrEmpty) {
sb.write(':${AtConstants.sharedKeyStatus}:${metadata.sharedKeyStatus}');
}
if (metadata.isBinary) {
sb.write(':isBinary:${metadata.isBinary}');
}

sb.write(':isEncrypted:${metadata.isEncrypted}');

if (metadata.sharedKeyEnc.isNotNullOrEmpty) {
sb.write(':${AtConstants.sharedKeyEncrypted}:${metadata.sharedKeyEnc}');
}
// ignore: deprecated_member_use_from_same_package
if (metadata.pubKeyCS.isNotNullOrEmpty) {
// ignore: deprecated_member_use_from_same_package
sb.write(
':${AtConstants.sharedWithPublicKeyCheckSum}:${metadata.pubKeyCS}');
}
if (metadata.pubKeyHash != null) {
sb.write(
':${AtConstants.sharedWithPublicKeyHashValue}:${metadata.pubKeyHash!.hash}');
sb.write(
':${AtConstants.sharedWithPublicKeyHashAlgo}:${metadata.pubKeyHash!.publicKeyHashingAlgo.name}');
}
if (metadata.encoding.isNotNullOrEmpty) {
sb.write(':${AtConstants.encoding}:${metadata.encoding}');
}
if (metadata.encKeyName.isNotNullOrEmpty) {
sb.write(':${AtConstants.encryptingKeyName}:${metadata.encKeyName}');
}
if (metadata.encAlgo.isNotNullOrEmpty) {
sb.write(':${AtConstants.encryptingAlgo}:${metadata.encAlgo}');
}
if (metadata.ivNonce.isNotNullOrEmpty) {
sb.write(':${AtConstants.ivOrNonce}:${metadata.ivNonce}');
}
if (metadata.skeEncKeyName.isNotNullOrEmpty) {
sb.write(
':${AtConstants.sharedKeyEncryptedEncryptingKeyName}:${metadata.skeEncKeyName}');
}
if (metadata.skeEncAlgo.isNotNullOrEmpty) {
sb.write(
':${AtConstants.sharedKeyEncryptedEncryptingAlgo}:${metadata.skeEncAlgo}');
}
return sb.toString();
}

@override
bool checkParams() {
var isValid = true;
Expand Down
2 changes: 1 addition & 1 deletion packages/at_commons/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: at_commons
description: A library of Dart and Flutter utility classes that are used across other components of the atPlatform.
version: 4.1.2
version: 4.1.3
repository: https://github.com/atsign-foundation/at_libraries
homepage: https://atsign.dev

Expand Down
36 changes: 16 additions & 20 deletions packages/at_commons/test/update_verb_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
..atKey.key = 'email'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommand(),
'update:public:email@alice alice@gmail.com\n');
'update:isEncrypted:false:public:email@alice alice@gmail.com\n');
});

test('verify private at key command', () {
Expand All @@ -22,7 +22,7 @@ void main() {
..atKey.key = 'email'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommand(),
'update:email@alice alice@atsign.com\n');
'update:isEncrypted:false:email@alice alice@atsign.com\n');
});

test(
Expand All @@ -47,7 +47,7 @@ void main() {
var updateCommand = updateBuilder.buildCommand();
expect(
updateCommand,
'update'
'update:isEncrypted:false'
':sharedKeyEnc:$ske'
':pubKeyCS:$pubKeyCS'
':skeEncKeyName:$skeEncKeyName'
Expand Down Expand Up @@ -75,7 +75,7 @@ void main() {
..atKey.sharedBy = '@alice'
..atKey.isLocal = true;
expect(updateBuilder.buildCommand(),
'update:local:email@alice alice@atsign.com\n');
'update:isEncrypted:false:local:email@alice alice@atsign.com\n');
});
});

Expand All @@ -87,7 +87,7 @@ void main() {
..atKey.key = 'phone'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommandForMeta(),
'update:meta:phone@alice:isBinary:true\n');
'update:meta:phone@alice:isBinary:true:isEncrypted:false\n');
});

test('verify ttl metadata', () {
Expand All @@ -96,7 +96,7 @@ void main() {
..atKey.key = 'phone'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommandForMeta(),
'update:meta:phone@alice:ttl:60000\n');
'update:meta:phone@alice:ttl:60000:isEncrypted:false\n');
});

test('verify ttr metadata', () {
Expand All @@ -105,7 +105,7 @@ void main() {
..atKey.key = 'phone'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommandForMeta(),
'update:meta:phone@alice:ttr:50000\n');
'update:meta:phone@alice:ttr:50000:isEncrypted:false\n');
});

test('verify ttb metadata', () {
Expand All @@ -114,7 +114,7 @@ void main() {
..atKey.key = 'phone'
..atKey.sharedBy = '@alice';
expect(updateBuilder.buildCommandForMeta(),
'update:meta:phone@alice:ttb:80000\n');
'update:meta:phone@alice:ttb:80000:isEncrypted:false\n');
});

test(
Expand Down Expand Up @@ -351,7 +351,8 @@ void main() {
..atKey.sharedBy = '@bob'
..value = '+445 334 3423';
var command = updateVerbBuilder.buildCommand();
expect(command, 'update:local:phone@bob +445 334 3423\n');
expect(
command, 'update:isEncrypted:false:local:phone@bob +445 334 3423\n');
expect(() => updateVerbBuilder.atKey.sharedWith = '@alice',
throwsA(predicate((dynamic e) => e is InvalidAtKeyException)));
});
Expand Down Expand Up @@ -534,12 +535,11 @@ void main() {
..atKey.sharedBy = '@alice';
var updateCommand = updateBuilder.buildCommand();
expect(updateCommand,
'update:ttl:5000:public:email@alice alice@gmail.com\n');
'update:ttl:5000:isEncrypted:false:public:email@alice alice@gmail.com\n');
var updateVerbParams =
getVerbParams(VerbSyntax.update, updateCommand.trim());
print(updateVerbParams);
// existing behaviour. TODO change to false after changes for update verb isEncrypted flag
expect(updateVerbParams['isEncrypted'], null);
expect(updateVerbParams['isEncrypted'], 'false');
});
test('for shared key - isEncrypted is true if set in metadata', () {
var updateBuilder = UpdateVerbBuilder()
Expand Down Expand Up @@ -567,14 +567,12 @@ void main() {
..atKey.sharedWith = '@bob';
var updateCommand = updateBuilder.buildCommand();
print(updateCommand);
// existing behaviour. TODO Should contain isEncrypted:false after changes for update verb isEncrypted flag
expect(updateCommand,
'update:ttl:5000:@bob:email@alice sampleEncryptedValue\n');
'update:ttl:5000:isEncrypted:false:@bob:email@alice sampleEncryptedValue\n');
var updateVerbParams =
getVerbParams(VerbSyntax.update, updateCommand.trim());
print(updateVerbParams);
// existing behaviour. TODO Should be false after changes for update verb isEncrypted flag
expect(updateVerbParams['isEncrypted'], null);
expect(updateVerbParams['isEncrypted'], 'false');
});

test('for shared key - isEncrypted is false if set to false in metadata',
Expand All @@ -588,14 +586,12 @@ void main() {
..atKey.sharedWith = '@bob';
var updateCommand = updateBuilder.buildCommand();
print(updateCommand);
// existing behaviour. TODO Should contain isEncrypted:false after changes for update verb isEncrypted flag
expect(updateCommand,
'update:ttl:5000:@bob:email@alice sampleEncryptedValue\n');
'update:ttl:5000:isEncrypted:false:@bob:email@alice sampleEncryptedValue\n');
var updateVerbParams =
getVerbParams(VerbSyntax.update, updateCommand.trim());
print(updateVerbParams);
// existing behaviour. TODO Should be false after changes for update verb isEncrypted flag
expect(updateVerbParams['isEncrypted'], null);
expect(updateVerbParams['isEncrypted'], 'false');
});
});
}