Skip to content

Commit

Permalink
Merge pull request #530 from atsign-foundation/enroll_list_filter
Browse files Browse the repository at this point in the history
feat: update enroll regex to accept params and new EnrollParamsField introduction
  • Loading branch information
murali-shris authored Mar 21, 2024
2 parents dc45270 + feddc9e commit 5d9b9b8
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 27 deletions.
2 changes: 2 additions & 0 deletions packages/at_commons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 4.0.4
- feat: Enhance enroll:list to enable filtering based on enrollment status
## 4.0.3
- fix: "toJson()" invoked on "pubKeyHash" leads to NullPointerException.
## 4.0.2
Expand Down
3 changes: 3 additions & 0 deletions packages/at_commons/lib/src/verb/enroll_params.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:json_annotation/json_annotation.dart';

import 'package:at_commons/at_commons.dart';
part 'enroll_params.g.dart';

@JsonSerializable()
Expand All @@ -12,6 +14,7 @@ class EnrollParams {
String? encryptedDefaultSelfEncryptionKey;
String? encryptedAPKAMSymmetricKey;
String? apkamPublicKey;
List<EnrollmentStatus>? enrollmentStatusFilter = EnrollmentStatus.values;
EnrollParams();
factory EnrollParams.fromJson(Map<String, dynamic> json) =>
_$EnrollParamsFromJson(json);
Expand Down
16 changes: 15 additions & 1 deletion packages/at_commons/lib/src/verb/enroll_params.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 43 additions & 17 deletions packages/at_commons/lib/src/verb/enroll_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:at_commons/src/enroll/enrollment.dart';
import 'package:at_commons/src/verb/abstract_verb_builder.dart';
import 'package:at_commons/src/verb/enroll_params.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -35,34 +36,59 @@ class EnrollVerbBuilder extends AbstractVerbBuilder {
String? encryptedDefaultSelfEncryptionKey;
String? encryptedAPKAMSymmetricKey;

/// Filters enrollment requests based on provided [EnrollmentStatus] criteria.
///
/// Accepts a list of enrollment statuses. Defaults to all EnrollmentStatuses
List<EnrollmentStatus> enrollmentStatusFilter = EnrollmentStatus.values;

@override
String buildCommand() {
var sb = StringBuffer();
sb.write('enroll:');
sb.write(getEnrollOperation(operation));

Map<String, dynamic> enrollParams = (EnrollParams()
..enrollmentId = enrollmentId
..appName = appName
..deviceName = deviceName
..apkamPublicKey = apkamPublicKey
..otp = otp
..namespaces = namespaces
..encryptedDefaultEncryptionPrivateKey =
encryptedDefaultEncryptionPrivateKey
..encryptedDefaultSelfEncryptionKey =
encryptedDefaultSelfEncryptionKey
..encryptedAPKAMSymmetricKey = encryptedAPKAMSymmetricKey)
.toJson();
enrollParams
.removeWhere((key, value) => value == null || value.toString().isEmpty);
if (enrollParams.isNotEmpty) {
sb.write(':${jsonEncode(enrollParams)}');
EnrollParams enrollParams = EnrollParams()
..enrollmentId = enrollmentId
..appName = appName
..deviceName = deviceName
..apkamPublicKey = apkamPublicKey
..otp = otp
..namespaces = namespaces
..encryptedDefaultEncryptionPrivateKey =
encryptedDefaultEncryptionPrivateKey
..encryptedDefaultSelfEncryptionKey = encryptedDefaultSelfEncryptionKey
..encryptedAPKAMSymmetricKey = encryptedAPKAMSymmetricKey
..enrollmentStatusFilter = enrollmentStatusFilter;

Map<String, dynamic> enrollParamsJson = enrollParams.toJson();
enrollParamsJson.removeWhere(_removeElements);
if (enrollParamsJson.isNotEmpty) {
sb.write(':${jsonEncode(enrollParamsJson)}');
}
sb.write('\n');
return sb.toString();
}

/// Compares current EnrollOperation with VerbBuilder params and removes any
/// that are NOT applicable
///
///
/// Returning "false" will leave the key and its value in the map, which gets added to the
/// enroll verb command. Returning true will remove the key and its value from the map to
/// to refrain adding the key and its value to the enroll verb command.
bool _removeElements(String key, dynamic value) {
if (value == null || value.toString().isEmpty) {
return true;
}
// EnrollmentStatusFilter is only applicable to EnrollOperation.list
// Remove it from enrollParamsJson for any operation other than list
if (key == 'enrollmentStatusFilter' &&
operation != EnrollOperationEnum.list) {
return true;
}
return false;
}

@override
bool checkParams() {
return appName != null &&
Expand Down
2 changes: 1 addition & 1 deletion packages/at_commons/lib/src/verb/syntax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class VerbSyntax {
static const notifyRemove = r'notify:remove:(?<id>[\w\d\-\_]+)';
static const enroll =
// The non-capturing group (?::)? matches ":" if the operation is request|approve|deny|revoke
r'^enroll:(?<operation>(?:list$|(request|approve|deny|revoke|update)))(?::)?(?<enrollParams>.+)?$';
r'^enroll:(?<operation>(?:(request|approve|deny|revoke|update|list)))(?::)?((?<enrollParams>.+)|(<=list:)<enrollParams>.?)?$';
static const otp =
r'^otp:(?<operation>get|put)(:(?<otp>(?<=put:)\w{6,}))?(:(?:ttl:(?<ttl>\d+)))?$';
static const keys = r'^keys:((?<operation>put|get|delete):?)'
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.0.3
version: 4.0.4
repository: https://github.com/atsign-foundation/at_libraries
homepage: https://atsign.dev

Expand Down
28 changes: 26 additions & 2 deletions packages/at_commons/test/enroll_params_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:convert';
import 'package:at_commons/src/verb/enroll_params.dart';
import 'package:at_commons/at_commons.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -52,6 +52,21 @@ void main() {
var enrollParams = jsonDecode(command);
expect(enrollParams['enrollmentId'], '123');
});

test('A test to verify enroll list regex with params', () {
String command =
'enroll:list:{"enrollmentStatusFilter":"[pending, approved]"}';
expect(RegExp(VerbSyntax.enroll).hasMatch(command), true);

command = command.replaceAll('enroll:list:', '');
var enrollParams = jsonDecode(command);
expect(enrollParams['enrollmentStatusFilter'], '[pending, approved]');
});

test('A test to verify enroll list regex without params', () {
String command = 'enroll:list';
expect(RegExp(VerbSyntax.enroll).hasMatch(command), true);
});
});

group('A group of tests to verify toJson and fromJson in EnrollParams', () {
Expand All @@ -64,7 +79,11 @@ void main() {
..enrollmentId = '1234'
..encryptedAPKAMSymmetricKey = 'dummy_pkam_sym_key'
..encryptedDefaultEncryptionPrivateKey = 'dummy_encrypted_private_key'
..encryptedDefaultSelfEncryptionKey = 'dummy_self_encryption_key';
..encryptedDefaultSelfEncryptionKey = 'dummy_self_encryption_key'
..enrollmentStatusFilter = [
EnrollmentStatus.approved,
EnrollmentStatus.pending
];

Map<String, dynamic> enrollParamsMap = enrollParams.toJson();
expect(enrollParamsMap['appName'], 'wavi');
Expand All @@ -78,6 +97,8 @@ void main() {
'dummy_encrypted_private_key');
expect(enrollParamsMap['encryptedDefaultSelfEncryptionKey'],
'dummy_self_encryption_key');
expect(
enrollParamsMap['enrollmentStatusFilter'], ['approved', 'pending']);
});

test('A test to verify fromJson', () {
Expand All @@ -93,6 +114,7 @@ void main() {
enrollParamsMap['encryptedDefaultSelfEncryptionKey'] =
'dummy_self_encryption_key';
enrollParamsMap['otp'] = '123';
enrollParamsMap['enrollmentStatusFilter'] = ['approved', 'pending'];

var enrollParams = EnrollParams.fromJson(enrollParamsMap);
expect(enrollParams.appName, 'wavi');
Expand All @@ -106,6 +128,8 @@ void main() {
'dummy_self_encryption_key');
expect(enrollParams.otp, '123');
expect(enrollParams.namespaces, {'wavi': 'rw', '__manage': 'r'});
expect(enrollParams.enrollmentStatusFilter,
[EnrollmentStatus.approved, EnrollmentStatus.pending]);
});
});
}
18 changes: 16 additions & 2 deletions packages/at_commons/test/enroll_verb_builder_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:at_commons/src/enroll/enrollment.dart';
import 'package:at_commons/src/verb/enroll_verb_builder.dart';
import 'package:at_commons/src/verb/operation_enum.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -52,11 +53,24 @@ void main() {
expect(command, 'enroll:revoke:{"enrollmentId":"123"}\n');
});

test('A test to verify enroll list operation', () {
test('A test to verify to override enroll list status', () {
var enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.list
..enrollmentStatusFilter = [
EnrollmentStatus.approved,
EnrollmentStatus.pending
];
var command = enrollVerbBuilder.buildCommand();
expect(command,
'enroll:list:{"enrollmentStatusFilter":["approved","pending"]}\n');
});

test('A test to validate enroll list command with default filter', () {
var enrollVerbBuilder = EnrollVerbBuilder()
..operation = EnrollOperationEnum.list;
var command = enrollVerbBuilder.buildCommand();
expect(command, 'enroll:list\n');
expect(command,
'enroll:list:{"enrollmentStatusFilter":["pending","approved","denied","revoked","expired"]}\n');
});
});
}
6 changes: 3 additions & 3 deletions packages/at_commons/test/update_verb_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void main() {
..atKey.key = 'email.wavi'
..atKey.sharedBy = '@alice'
..atKey.sharedWith = '@bob'
// ignore: deprecated_member_use_from_same_package
// ignore: deprecated_member_use_from_same_package
..atKey.metadata.pubKeyCS = pubKeyCS
..atKey.metadata.sharedKeyEnc = ske
..atKey.metadata.skeEncKeyName = skeEncKeyName
Expand Down Expand Up @@ -131,7 +131,7 @@ void main() {
..atKey.key = 'cabbages_and_kings.wonderland'
..atKey.sharedBy = '@walrus'
..atKey.sharedWith = '@carpenter'
// ignore: deprecated_member_use_from_same_package
// ignore: deprecated_member_use_from_same_package
..atKey.metadata.pubKeyCS = pubKeyCS
..atKey.metadata.sharedKeyEnc = ske
..atKey.metadata.skeEncKeyName = skeEncKeyName
Expand Down Expand Up @@ -417,7 +417,7 @@ void main() {
..atKey.metadata.isBinary = isBinary
..atKey.metadata.isEncrypted = isEncrypted
..atKey.metadata.sharedKeyEnc = sharedKeyEncrypted
// ignore: deprecated_member_use_from_same_package
// ignore: deprecated_member_use_from_same_package
..atKey.metadata.pubKeyCS = pubKeyChecksum
..atKey.metadata.encoding = encoding
..atKey.metadata.encKeyName = encKeyName
Expand Down

0 comments on commit 5d9b9b8

Please sign in to comment.