-
Notifications
You must be signed in to change notification settings - Fork 1
/
oob_payload_decode_usecase.dart
60 lines (53 loc) · 2.21 KB
/
oob_payload_decode_usecase.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright © 2022 Nevis Security AG. All rights reserved.
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';
import 'package:nevis_mobile_authentication_sdk/nevis_mobile_authentication_sdk.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/client_provider/client_provider.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/error/error_handler.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/model/error/error_message_types.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/model/operation/operation_type.dart';
import 'package:nevis_mobile_authentication_sdk_example_app_flutter/domain/repository/state_repository.dart';
abstract class OobPayloadDecodeUseCase {
Future<void> execute({
required String json,
required Function(OutOfBandPayload) onSuccess,
});
}
@Injectable(as: OobPayloadDecodeUseCase)
class OobPayloadDecodeUseCaseImpl implements OobPayloadDecodeUseCase {
final ClientProvider _clientProvider;
final StateRepository<OperationType> _operationTypeRepository;
final ErrorHandler _errorHandler;
OobPayloadDecodeUseCaseImpl(
this._clientProvider,
this._operationTypeRepository,
this._errorHandler,
);
@override
Future<void> execute({
required String json,
required Function(OutOfBandPayload) onSuccess,
}) async {
String fixedJson = json;
if (json.length % 4 > 0) {
fixedJson += '=' * (4 - json.length % 4);
}
final decodedJson = utf8.decode(base64.decode(fixedJson));
_operationTypeRepository.save(OperationType.payloadDecode);
await _clientProvider.client.operations.outOfBandPayloadDecode //
.json(decodedJson)
.onSuccess((payload) {
if (payload == null) {
debugPrint('Out of band payload is null.');
_errorHandler.handle(BusinessErrorType.invalidState);
return;
}
debugPrint('Out of band payload decode succeeded.');
onSuccess.call(payload);
}).onError((error) {
debugPrint('Out of band payload decode failed: ${error.runtimeType}.');
_errorHandler.handle(error);
}).execute();
}
}