Skip to content

Commit

Permalink
rename TYPE constant and added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer committed Dec 17, 2024
1 parent f58e74d commit 0e62a6b
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 260 deletions.
30 changes: 16 additions & 14 deletions lib/api/impl/privacy_idea_container_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class PiContainerApi implements TokenContainerApi {
////////////////////////////// */

@override
Future<ContainerSyncUpdates> sync(TokenContainerFinalized container, TokenState tokenState) async {
Future<ContainerSyncUpdates> sync(TokenContainerFinalized container, TokenState tokenState, {SimpleKeyPair? withX25519Key}) async {
final containerTokenTemplates = tokenState.containerTokens(container.serial).toTemplates();

final notLinkedTokenTemplates = (container.policies.initialTokenTransfer) ? tokenState.notLinkedTokens.toTemplates() : <TokenTemplate>[];

final ContainerChallenge challenge = await _getChallenge(container, container.syncUrl);

final encKeyPair = await X25519().newKeyPair();
final encKeyPair = withX25519Key ?? await X25519().newKeyPair();
final syncResult = await _getContainerSyncResult(
container: container,
challenge: challenge,
Expand Down Expand Up @@ -144,8 +144,8 @@ class PiContainerApi implements TokenContainerApi {
final body = {
'container_serial': container.serial,
'public_client_key': container.publicClientKey,
'device_brand': InfoUtils.deviceBrand,
'device_model': InfoUtils.deviceModel,
if (container.addDeviceInfos == true) 'device_brand': InfoUtils.deviceBrand,
if (container.addDeviceInfos == true) 'device_model': InfoUtils.deviceModel,
'signature': signature,
};
final Response response = await _ioClient.doPost(url: container.registrationUrl, body: body, sslVerify: container.sslVerify);
Expand Down Expand Up @@ -286,9 +286,13 @@ class PiContainerApi implements TokenContainerApi {
required SimpleKeyPair encKeyPair,
}) async {
final publicKey = await encKeyPair.extractPublicKey();
final privateKeyBytes = await encKeyPair.extractPrivateKeyBytes();

final publicKeyBase64 = base64.encode(publicKey.bytes);

Logger.warning('Public key base64: $publicKeyBase64');
Logger.warning('Private key bytes: ${base64.encode(privateKeyBytes)}');

final containerDict = {
TokenContainer.DICT_SERIAL: container.serial,
TokenContainer.DICT_TYPE: TokenContainer.DICT_TYPE_SMARTPHONE,
Expand Down Expand Up @@ -369,17 +373,13 @@ class PiContainerApi implements TokenContainerApi {
final merged = <TokenTemplate>[];
for (var serverTokenWithOtp in serverTokensWithOtps) {
final otps = (serverTokenWithOtp[OTPToken.OTP_VALUES] as List).cast<String>();
var mergedTemplate = maybePiTokensTemplates.firstWhere(
var mergedTemplate = maybePiTokensTemplates.firstWhereOrNull(
(maybePiToken) => const IterableEquality().equals(otps, maybePiToken.otpValues),
orElse: () => TokenTemplate.withOtps(
otps: serverTokenWithOtp[OTPToken.OTP_VALUES]!,
otpAuthMap: serverTokenWithOtp,
container: container,
additionalData: {
Token.CHECKED_CONTAINERS: [container.serial],
},
),
);
if (mergedTemplate == null) {
Logger.warning('Server token with otps not found in local tokens: ${serverTokenWithOtp[OTPToken.OTP_VALUES]}');
continue;
}
mergedTemplate = mergedTemplate.withOtpAuthData(serverTokenWithOtp);
mergedTemplate = mergedTemplate.copyWith(container: container);
merged.add(mergedTemplate);
Expand All @@ -398,7 +398,9 @@ class PiContainerApi implements TokenContainerApi {
final serverToken = serverTokensWithSerial.firstWhereOrNull((element) => element[Token.SERIAL] == containerToken.serial);
serverTokensWithSerial.remove(serverToken);
if (serverToken == null) {
deleteSerials.add(containerToken.serial!);
if (containerToken.containerSerial == container.serial) {
deleteSerials.add(containerToken.serial!);
}
} else {
var mergedTemplate = containerToken.withOtpAuthData(serverToken);
mergedTemplate = mergedTemplate.copyWith(container: container);
Expand Down
6 changes: 3 additions & 3 deletions lib/model/token_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class TokenContainer with _$TokenContainer {
return null;
}

Logger.warning('PublicServerKey: $publicServerKey'); // TODO: remove
Logger.warning('PublicClientKey: $publicClientKey'); // TODO: remove
Logger.warning('PrivateClientKey: $privateClientKey'); // TODO: remove
return TokenContainerFinalized(
issuer: issuer,
nonce: nonce,
Expand Down Expand Up @@ -223,9 +226,6 @@ class TokenContainer with _$TokenContainer {
);

factory TokenContainer.fromJson(Map<String, dynamic> json) {
print('PublicServerKey: ${json['publicServerKey']}');
print('PrivateClientKey: ${json['privateClientKey']}');
print('PublicClientKey: ${json['publicClientKey']}');
return json["runtimeType"] == "finalized"
? (_$TokenContainerFromJson(json) as TokenContainerFinalized)
.copyWith(syncState: json["syncState"] == "syncing" ? SyncState.failed : SyncState.values.byName(json["syncState"]))
Expand Down
2 changes: 1 addition & 1 deletion lib/model/tokens/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class Token with SortableMixin {
static const String IMAGE = 'image';

// Default data keys
static const String TYPE = 'type';
static const String TYPE = 'tokentype';

/// [String] (optional) default = ''
static const String LABEL = 'label';
Expand Down
Loading

0 comments on commit 0e62a6b

Please sign in to comment.