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

Replace encryption methods with atchops #1203

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: at_client_sdk

packages:
- packages/*
- packages/at_client_mobile
- packages/*/example

- tests/*
7 changes: 7 additions & 0 deletions packages/at_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 3.0.72
- build[deps]: Upgraded dependencies for the following packages:
- at_commons to v4.0.0
- at_utils to v3.0.16
- at_lookup to v3.0.44
- at_chops to v1.0.7
- at_persistence_secondary_server to v3.0.60
## 3.0.71
- feat: Replace decryption methods from EncryptionUtil with AtChops methods
## 3.0.70
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AtCollectionMethodImpl {
_logger.finest('Self key to be used : $atKey');
var atOperationItemStatus = AtOperationItemStatus(
atSign: atKey.sharedBy ?? '',
key: atKey.key ?? '',
key: atKey.key,
complete: false,
operation: Operation.save);
try {
Expand Down Expand Up @@ -76,7 +76,7 @@ class AtCollectionMethodImpl {
_logger.finest('Update shared key $sharedKey');
var atOperationItemStatus = AtOperationItemStatus(
atSign: sharedKey.sharedWith ?? '',
key: sharedKey.key ?? '',
key: sharedKey.key,
complete: false,
operation: Operation.share);
try {
Expand Down Expand Up @@ -133,7 +133,7 @@ class AtCollectionMethodImpl {

var atOperationItemStatus = AtOperationItemStatus(
atSign: atSign,
key: selfKey.key ?? '',
key: selfKey.key,
complete: false,
operation: Operation.share);

Expand Down Expand Up @@ -164,7 +164,7 @@ class AtCollectionMethodImpl {

yield AtOperationItemStatus(
atSign: selfAtKey.sharedWith ?? '',
key: selfAtKey.key ?? '',
key: selfAtKey.key,
complete: isSelfKeyDeleted,
operation: Operation.delete);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ class AtCollectionMethodImpl {
for (var sharedKey in sharedAtKeys) {
var atOperationItemStatus = AtOperationItemStatus(
atSign: sharedKey.sharedWith ?? '',
key: sharedKey.key ?? '',
key: sharedKey.key,
complete: false,
operation: Operation.unshare);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class DefaultKeyMaker implements KeyMaker {
return AtKey()
..key = '$keyId.$collectionName.atcollectionmodel.$namespace'
..metadata = Metadata()
..metadata!.ccd = objectLifeCycleOptions?.cascadeDelete ?? true
..metadata!.ttl = objectLifeCycleOptions?.timeToLive?.inMilliseconds
..metadata!.ttb = objectLifeCycleOptions?.timeToBirth?.inMilliseconds
..metadata.ccd = objectLifeCycleOptions?.cascadeDelete ?? true
..metadata.ttl = objectLifeCycleOptions?.timeToLive?.inMilliseconds
..metadata.ttb = objectLifeCycleOptions?.timeToBirth?.inMilliseconds
..sharedBy = _getAtClient().getCurrentAtSign();
}

Expand All @@ -36,10 +36,10 @@ class DefaultKeyMaker implements KeyMaker {
..key = '$keyId.$collectionName.atcollectionmodel.$namespace'
..sharedWith = sharedWith
..metadata = Metadata()
..metadata!.ttr = ttrInSeconds ?? -1
..metadata!.ccd = objectLifeCycleOptions?.cascadeDelete ?? true
..metadata!.ttl = objectLifeCycleOptions?.timeToLive?.inMilliseconds
..metadata!.ttb = objectLifeCycleOptions?.timeToBirth?.inMilliseconds
..metadata.ttr = ttrInSeconds ?? -1
..metadata.ccd = objectLifeCycleOptions?.cascadeDelete ?? true
..metadata.ttl = objectLifeCycleOptions?.timeToLive?.inMilliseconds
..metadata.ttb = objectLifeCycleOptions?.timeToBirth?.inMilliseconds
..sharedBy = _getAtClient().getCurrentAtSign();
}
}
59 changes: 15 additions & 44 deletions packages/at_client/lib/src/client/at_client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,8 @@ class AtClientImpl implements AtClient, AtSignChangeListener {

Future<bool> _delete(AtKey atKey,
{DeleteRequestOptions? deleteRequestOptions}) async {
// If metadata is null, initialize metadata
atKey.metadata ??= Metadata();
String keyWithNamespace;
if (atKey.metadata!.namespaceAware) {
keyWithNamespace = AtClientUtil.getKeyWithNameSpace(atKey, _preference!);
} else {
keyWithNamespace = atKey.key!;
}
atKey.sharedBy ??= _atSign;
var builder = DeleteVerbBuilder()
..isLocal = atKey.isLocal
..isCached = atKey.metadata!.isCached
..isPublic =
(atKey.metadata!.isPublic == null) ? false : atKey.metadata!.isPublic!
..sharedWith = atKey.sharedWith
..atKey = keyWithNamespace
..sharedBy = atKey.sharedBy;
var builder = DeleteVerbBuilder()..atKey = atKey;
var secondary = getSecondary();
if (deleteRequestOptions != null &&
deleteRequestOptions.useRemoteAtServer) {
Expand Down Expand Up @@ -441,10 +426,8 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
Future<AtResponse> putText(AtKey atKey, String value,
{PutRequestOptions? putRequestOptions}) async {
try {
// Set the default metadata if not already set.
atKey.metadata ??= Metadata();
// Setting metadata.isBinary to false for putText
atKey.metadata!.isBinary = false;
atKey.metadata.isBinary = false;
return await _putInternal(atKey, value, putRequestOptions);
} on AtException catch (e) {
throw AtExceptionManager.createException(e);
Expand All @@ -456,10 +439,8 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
Future<AtResponse> putBinary(AtKey atKey, List<int> value,
{PutRequestOptions? putRequestOptions}) async {
try {
// Set the default metadata if not already set.
atKey.metadata ??= Metadata();
// Setting metadata.isBinary to true for putBinary
atKey.metadata!.isBinary = true;
atKey.metadata.isBinary = true;
// Base2e15.encode method converts the List<int> type to String.
return await _putInternal(
atKey, Base2e15.encode(value), putRequestOptions);
Expand All @@ -470,7 +451,7 @@ class AtClientImpl implements AtClient, AtSignChangeListener {

@visibleForTesting
ensureLowerCase(AtKey atKey) {
if ((atKey.key != null && upperCaseRegex.hasMatch(atKey.key!)) ||
if (upperCaseRegex.hasMatch(atKey.key) ||
(atKey.namespace != null &&
upperCaseRegex.hasMatch(atKey.namespace!))) {
_logger.finer('AtKey: ${atKey.toString()} previously contained upper case'
Expand All @@ -487,12 +468,12 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
if (atKey.sharedBy.isNull) {
atKey.sharedBy = _atSign;
}
if (atKey.metadata!.namespaceAware) {
if (atKey.metadata.namespaceAware) {
atKey.namespace ??= preference?.namespace;
}

if (preference!.atProtocolEmitted >= Version(2, 0, 0)) {
atKey.metadata!.ivNonce ??= EncryptionUtil.generateIV();
atKey.metadata.ivNonce ??= EncryptionUtil.generateIV();
}
ensureLowerCase(atKey);

Expand Down Expand Up @@ -521,7 +502,7 @@ class AtClientImpl implements AtClient, AtSignChangeListener {

//Get encryptionPrivateKey for public key to signData
String? encryptionPrivateKey;
if (atKey.metadata!.isPublic != null && atKey.metadata!.isPublic! == true) {
if (atKey.metadata.isPublic == true) {
encryptionPrivateKey = await _localSecondary?.getEncryptionPrivateKey();
}
// Transform put request
Expand All @@ -543,7 +524,7 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
}
// Execute the verb builder
var putResponse = await secondary.executeVerb(verbBuilder,
sync: SyncUtil.shouldSync(atKey.key!));
sync: SyncUtil.shouldSync(atKey.key));
// If putResponse is null or empty, return AtResponse with isError set to true
if (putResponse == null || putResponse.isEmpty) {
return AtResponse()..isError = true;
Expand Down Expand Up @@ -579,27 +560,17 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
@override
Future<bool> putMeta(AtKey atKey) async {
var updateKey = atKey.key;
var metadata = atKey.metadata!;
var metadata = atKey.metadata;
if (metadata.namespaceAware) {
updateKey = _getKeyWithNamespace(atKey.key!);
updateKey = _getKeyWithNamespace(atKey.key);
}
var sharedWith = atKey.sharedWith;
var builder = UpdateVerbBuilder();
builder
..atKey = updateKey
..sharedBy = _atSign
..sharedWith = sharedWith
..ttl = metadata.ttl
..ttb = metadata.ttb
..ttr = metadata.ttr
..ccd = metadata.ccd
..isBinary = metadata.isBinary
..isEncrypted = metadata.isEncrypted
..dataSignature = metadata.dataSignature
..atKey = atKey
..operation = AtConstants.updateMeta;

var updateMetaResult = await getSecondary()
.executeVerb(builder, sync: SyncUtil.shouldSync(updateKey!));
.executeVerb(builder, sync: SyncUtil.shouldSync(updateKey));
return updateMetaResult != null;
}

Expand Down Expand Up @@ -768,9 +739,9 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
..key = key
..sharedWith = sharedWithAtSign
..metadata = Metadata()
..metadata!.ttr = -1
..metadata.ttr = -1
// file transfer key will be deleted after 30 days
..metadata!.ttl = 2592000000
..metadata.ttl = 2592000000
..sharedBy = _atSign;

var notificationResult = await notificationService.notify(
Expand Down Expand Up @@ -949,7 +920,7 @@ class AtClientImpl implements AtClient, AtSignChangeListener {
PriorityEnum? priority,
StrategyEnum? strategy,
int? latestN,
String? notifier = SYSTEM,
String? notifier = AtConstants.system,
bool isDedicated = false}) async {
AtKeyValidators.get().validate(
atKey.toString(),
Expand Down
39 changes: 4 additions & 35 deletions packages/at_client/lib/src/client/local_secondary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,46 +69,15 @@ class LocalSecondary implements Secondary {
var updateKey = builder.buildKey();
switch (builder.operation) {
case AtConstants.updateMeta:
var metadata = Metadata();
metadata
..ttl = builder.ttl
..ttb = builder.ttb
..ttr = builder.ttr
..ccd = builder.ccd
..isBinary = builder.isBinary
..isEncrypted = builder.isEncrypted
..sharedKeyEnc = builder.sharedKeyEncrypted
..pubKeyCS = builder.pubKeyChecksum
..encoding = builder.encoding
..encKeyName = builder.encKeyName
..encAlgo = builder.encAlgo
..ivNonce = builder.ivNonce
..skeEncKeyName = builder.skeEncKeyName
..skeEncAlgo = builder.skeEncAlgo;
var atMetadata = AtMetaData.fromCommonsMetadata(metadata);
var atMetadata =
AtMetaData.fromCommonsMetadata(builder.atKey.metadata);
updateResult = await keyStore!.putMeta(updateKey, atMetadata);
break;
default:
var atData = AtData();
atData.data = builder.value;
var metadata = Metadata();
metadata
..ttl = builder.ttl
..ttb = builder.ttb
..ttr = builder.ttr
..ccd = builder.ccd
..isBinary = builder.isBinary
..isEncrypted = builder.isEncrypted
..dataSignature = builder.dataSignature
..sharedKeyEnc = builder.sharedKeyEncrypted
..pubKeyCS = builder.pubKeyChecksum
..encoding = builder.encoding
..encKeyName = builder.encKeyName
..encAlgo = builder.encAlgo
..ivNonce = builder.ivNonce
..skeEncKeyName = builder.skeEncKeyName
..skeEncAlgo = builder.skeEncAlgo;
var atMetadata = AtMetaData.fromCommonsMetadata(metadata);
var atMetadata =
AtMetaData.fromCommonsMetadata(builder.atKey.metadata);
updateResult = await keyStore!.putAll(updateKey, atData, atMetadata);
break;
}
Expand Down
36 changes: 16 additions & 20 deletions packages/at_client/lib/src/client/verb_builder_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ class LookUpBuilderManager {
{GetRequestOptions? getRequestOptions}) {
// If isPublic is true in metadata, the key is a public key, return PLookupVerbHandler.
if (atKey.sharedBy != currentAtSign &&
(atKey.metadata != null &&
atKey.metadata!.isPublic! &&
!atKey.metadata!.isCached)) {
(atKey.metadata.isPublic && !atKey.metadata.isCached)) {
final plookUpVerbBuilder = PLookupVerbBuilder()
..atKey = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy)
..atKey = (AtKey()
..key = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy))
..operation = 'all';
if (getRequestOptions != null && getRequestOptions.bypassCache == true) {
plookUpVerbBuilder.bypassCache = true;
Expand All @@ -24,12 +23,11 @@ class LookUpBuilderManager {
}
// If sharedBy is not equal to currentAtSign and isCached is false, return LookupVerbHandler
if (atKey.sharedBy != currentAtSign &&
(atKey.metadata != null &&
!atKey.metadata!.isCached &&
!atKey.metadata!.isPublic!)) {
(!atKey.metadata.isCached && !atKey.metadata.isPublic)) {
final lookupVerbBuilder = LookupVerbBuilder()
..atKey = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy)
..atKey = (AtKey()
..key = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy))
..auth = true
..operation = 'all';
if (getRequestOptions != null && getRequestOptions.bypassCache == true) {
Expand All @@ -38,16 +36,14 @@ class LookUpBuilderManager {
return lookupVerbBuilder;
}
return LLookupVerbBuilder()
..atKey = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy)
..sharedWith = AtClientUtil.fixAtSign(atKey.sharedWith)
..isPublic = (atKey.metadata != null && atKey.metadata?.isPublic != null)
? atKey.metadata!.isPublic!
: false
..isCached = (atKey.metadata != null && atKey.metadata?.isCached != null)
? atKey.metadata!.isCached
: false
..isLocal = atKey.isLocal
..atKey = (AtKey()
..key = AtClientUtil.getKeyWithNameSpace(atKey, atClientPreference)
..sharedBy = AtClientUtil.fixAtSign(atKey.sharedBy)
..sharedWith = AtClientUtil.fixAtSign(atKey.sharedWith)
..metadata = (Metadata()
..isPublic = atKey.metadata.isPublic
..isCached = atKey.metadata.isCached)
..isLocal = atKey.isLocal)
..operation = 'all';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AtKeyDecryptionManager {
// Eg: currentAtSign is @bob and _phone.wavi@bob (or) phone@bob (or) @bob:phone@bob
if (((atKey.sharedWith == null || atKey.sharedWith == currentAtSign) &&
atKey.sharedBy == currentAtSign) ||
atKey.key!.startsWith('_')) {
atKey.key.startsWith('_')) {
return SelfKeyDecryption(_atClient);
}
// Returns LocalKeyDecryption to for the keys present in local storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class LocalKeyDecryption extends AbstractAtKeyEncryption
exceptionScenario: ExceptionScenario.fetchEncryptionKeys);
}
InitialisationVector iV;
if (atKey.metadata?.ivNonce != null) {
iV = AtChopsUtil.generateIVFromBase64String(atKey.metadata!.ivNonce!);
if (atKey.metadata.ivNonce != null) {
iV = AtChopsUtil.generateIVFromBase64String(atKey.metadata.ivNonce!);
} else {
iV = AtChopsUtil.generateIVLegacy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class SelfKeyDecryption implements AtKeyDecryption {
}

InitialisationVector iV;
if (atKey.metadata?.ivNonce != null) {
iV = AtChopsUtil.generateIVFromBase64String(atKey.metadata!.ivNonce!);
if (atKey.metadata.ivNonce != null) {
iV = AtChopsUtil.generateIVFromBase64String(atKey.metadata.ivNonce!);
} else {
iV = AtChopsUtil.generateIVLegacy();
}
Expand Down
Loading
Loading