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!: token #4

Merged
merged 4 commits into from
Feb 21, 2025
Merged
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
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
device_info_plus: bf2e3232933866d73fe290f2942f2156cdd10342
device_info_plus: 21fcca2080fbcd348be798aa36c3e5ed849eefbe
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_blue_plus: 4837da7d00cf5d441fdd6635b3a57f936778ea96
haptic_feedback: ccbfbef39de4757f4bdf0ef03e7dfb3ffb4363d3
sensors_plus: 18a9b346c43e157da17d2c8e99def703f9efb9d8
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
flutter_blue_plus: e5808fc4e5ebc58bb911635f8fdaf5e2b4da2754
haptic_feedback: 6d9ff6a70a5ca4ef470fbc5903c9c6c8d495d861
sensors_plus: 3c3bac724a2128c895623e03efd82cf0e94fd8e8
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7

PODFILE CHECKSUM: 2626946c0f4c5a786ca56bc00721109d51be6950

Expand Down
19 changes: 3 additions & 16 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class _MainAppState extends State<MainApp> {
bool _canDismiss = true;

final UrpBleStrategy _bleStrategy = UrpBleStrategy();

bool _useVirtual = false;

@override
Expand Down Expand Up @@ -84,6 +85,8 @@ class _MainAppState extends State<MainApp> {
}),
SecModalBuilder(
canDismiss: _canDismiss,
turnOffOnClose: false,
disconnectOnClose: true,
strategy: _useVirtual ? virtualStrategy : _bleStrategy,
payload: "<example payload>",
onDismiss: () {
Expand All @@ -103,22 +106,6 @@ class _MainAppState extends State<MainApp> {
);
},
),
ldSpacerL,
LdButton(
onPressed: () async {
final result = await secModal(
canDismiss: _canDismiss,
topRadius: 10,
bottomRadius: 10,
strategy: virtualStrategy,
payload: "<example payload>",
insets: const EdgeInsets.all(1),
useSafeArea: true,
).show(context);
debugPrint("Result: $result");
},
child: const Text("Show using secModal().show()"),
),
]),
).padL(),
),
Expand Down
28 changes: 14 additions & 14 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ packages:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
sha256: b6dc7065e46c974bc7c5f143080a6764ec7a4be6da1285ececdc37be96de53be
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.1.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -321,10 +321,10 @@ packages:
dependency: "direct main"
description:
name: liquid_flutter
sha256: "860b21081222fb6ac77e46014fda0e5e29ff72cb8568c7c532f5fbea90640ca6"
sha256: "0cffcff4333789c569f400b04bf04009ba4a1cd2b4b733bb81cc3a8b112c902c"
url: "https://pub.dev"
source: hosted
version: "19.0.0"
version: "19.1.0"
logger:
dependency: transitive
description:
Expand Down Expand Up @@ -376,42 +376,42 @@ packages:
dependency: "direct main"
description:
name: mtrust_urp_ble_strategy
sha256: "9d1f2269a7f99400468cb98b2089f5831ae46c03bf033a2ef260841a4b7a711f"
sha256: "28ed48645bbd9848392cc02f01c2933066c99c3cecf4bfa685c8c94261ae506b"
url: "https://pub.dev"
source: hosted
version: "9.0.0-6"
version: "9.0.0-7"
mtrust_urp_core:
dependency: transitive
description:
name: mtrust_urp_core
sha256: "3765318619490727496b45c3a5fb041e53b07b0e6f7e099cef1a2de221ef72b5"
sha256: "35f55c815cee8b50c18ce3f5e784d3415d8b0306d570ce6da83ee97cdcb0ccce"
url: "https://pub.dev"
source: hosted
version: "9.0.0-6"
version: "9.0.0-7"
mtrust_urp_types:
dependency: "direct main"
description:
name: mtrust_urp_types
sha256: "65478d9c303a67638e9613d784bd2434240dbfcfc6c6c41c1584b9d77fd07d6b"
sha256: fcc680b35d9511b0aca433575d03d266c992e5ab77b5420b67410bd914e1769d
url: "https://pub.dev"
source: hosted
version: "4.1.0"
version: "5.0.2"
mtrust_urp_ui:
dependency: "direct main"
description:
name: mtrust_urp_ui
sha256: "1ba16568ed9f1e85f94c27a9b958c536c165892d4babdd7ba93ac80cd53d29fa"
sha256: "2cfff43d2a0955fae75f02e02a57c74ccbda5552cfb5c234462d6bcebc259e1a"
url: "https://pub.dev"
source: hosted
version: "9.0.0-6"
version: "9.0.0-7"
mtrust_urp_virtual_strategy:
dependency: "direct main"
description:
name: mtrust_urp_virtual_strategy
sha256: "4c7d0a8dda95c4279da71f1c0cf6f7009e847ac41b9756cd89adb47d9939a86a"
sha256: bbbd868c1f9364daad89e4daca68fefe4b5216c9278ea3d77bfa5e3476d7a8fb
url: "https://pub.dev"
source: hosted
version: "9.0.0-6"
version: "9.0.0-7"
multi_split_view:
dependency: transitive
description:
Expand Down
9 changes: 5 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ dependencies:
sdk: flutter
mtrust_sec_kit:
path: ../
mtrust_urp_types: ^4.1.0
mtrust_urp_ble_strategy: ^9.0.0-6
mtrust_urp_virtual_strategy: ^9.0.0-6
mtrust_urp_ui: ^9.0.0-6
mtrust_urp_types: ^5.0.2
mtrust_urp_ble_strategy: ^9.0.0-7
mtrust_urp_virtual_strategy: ^9.0.0-7
mtrust_urp_ui: ^9.0.0-7
liquid_flutter: ^19.0.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
3 changes: 2 additions & 1 deletion lib/mtrust_sec_kit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'package:mtrust_urp_types/sec.pb.dart';
export 'package:mtrust_urp_ui/mtrust_urp_ui.dart';

export 'src/sec_reader.dart';
export 'src/sec_reader_exception.dart';
export 'src/ui/l10n/sec_locale.dart';
export 'src/ui/sec_modal.dart';
export 'src/ui/sec_view.dart';
export 'src/ui/sec_widget.dart';
100 changes: 94 additions & 6 deletions lib/src/sec_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:io';

import 'package:mtrust_sec_kit/mtrust_sec_kit.dart';
import 'package:mtrust_sec_kit/src/sec_reader_exception.dart';

/// [SECReader] is a class that provides a high-level API to interact with
/// a SEC reader.
Expand Down Expand Up @@ -39,6 +38,15 @@ class SECReader extends CmdWrapper {
/// The origin device.
final UrpDeviceIdentifier origin;

int _requestTokenAmount = 10;

/// Sets the amount of tokens to be requested if the device has no more
/// tokens available. The default is 10.
void setTokenAmount(int amount) {
_requestTokenAmount = amount;
notifyListeners();
}

/// Find and connect to a SEC reader using the given [connectionStrategy].
/// If [deviceAddress] is provided, it will try to connect to the reader
/// with the given address.
Expand Down Expand Up @@ -319,14 +327,84 @@ class SECReader extends CmdWrapper {
}

/// Prepares (primes) a measurement for the given [payload].
Future<void> prime(String payload) async {
Future<UrpSecPrimeResponse?> prime(String payload) async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecPrime,
primeParameters: UrpSecPrimeParameters(payload: payload),
);
try {
final res = await _addCommandToQueue(deviceCommand: cmd);
return UrpSecPrimeResponse.fromBuffer(res.payload);
} catch (e) {
if(e is DeviceError) {
if(e.errorCode == 4) {
final publicKey = await getPublicKey();
final oldToken = await requestToken();
final newToken = await getToken(oldToken, publicKey);
if(newToken == null) {
throw SecReaderException(
message: 'Failed to get new tooken!',
type: SecReaderExceptionType.tokenFailed,
);
}
await setToken(newToken);
return prime(payload);
} else {
rethrow;
}
}
return null;
}
}

/// Request the device to send a new token request
Future<UrpSecureToken> requestToken() async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecRequestToken,
tokenRequest: UrpTokenRequest(
amount: _requestTokenAmount,
),
);
final res = await _addCommandToQueue(
deviceCommand: cmd,
);

if(!res.hasPayload()) {
throw SecReaderException(
message: 'Failed to request token!',
type: SecReaderExceptionType.tokenFailed,
);
}
return UrpSecureToken.fromBuffer(res.payload);
}

/// Install a new token after a token request on the device
Future<void> setToken(UrpSecureToken token) async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecSetToken,
secureToken: token,
);
await _addCommandToQueue(deviceCommand: cmd);
}

/// Request the currently installed token from the device
Future<UrpSecureToken> getCurrentToken() async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecGetToken,
);
final res = await _addCommandToQueue(
deviceCommand: cmd,
);

if(!res.hasPayload()) {
throw SecReaderException(
message: 'Failed to get current token!',
type: SecReaderExceptionType.tokenFailed,
);
}
return UrpSecureToken.fromBuffer(res.payload);
}

/// Unprime a measurement.
Future<void> unprime() async {
final cmd = UrpSecDeviceCommand(
Expand All @@ -341,12 +419,22 @@ class SECReader extends CmdWrapper {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecStartMeasurement,
);
final result = await _addCommandToQueue(deviceCommand: cmd);
final res = await _addCommandToQueue(deviceCommand: cmd);

if (!result.hasPayload()) {
throw Exception('Failed to measure.');
if (!res.hasPayload()) {
throw SecReaderException(
message: 'Failed to measure!',
type: SecReaderExceptionType.measurementFailed,
);
}
try {
return UrpSecMeasurement.fromBuffer(res.payload);
} catch (e) {
throw SecReaderException(
message: 'Incompatible device firmware version. Please update!',
type: SecReaderExceptionType.incompatibleFirmware,
);
}
return UrpSecMeasurement.fromBuffer(result.payload);
}

/// Stop measurement
Expand Down
18 changes: 18 additions & 0 deletions lib/src/sec_reader_exception.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/// Enumerates the types of exceptions that can be thrown by the SEC reader.
enum SecReaderExceptionType {
/// Firmware version installed on the device is incompatible
incompatibleFirmware,
/// Measurement failed
measurementFailed,
/// Failed to get or install new token
tokenFailed,
/// Unspecified error
unspecified,
}


/// Exception thrown to indicate errors related to the SEC reader.
///
/// This exception extends the [Error] class and is designed
Expand All @@ -9,15 +22,20 @@ class SecReaderException extends Error {
/// The [message] parameter can be used to provide additional details
/// about the nature of the SEC reader error. If not specified,
/// a default message will be used.
/// The [type] parameter can be used to specify the type of exception.
SecReaderException({
this.message = 'Unspecified SEC reader error',
this.type = SecReaderExceptionType.unspecified,
});

/// A message providing additional details about the SEC reader error.
///
/// If not specified during the exception creation, a default message
/// ("Unspecified SEC reader error") will be used.
final String? message;
/// The type of exception that was thrown.
/// The default value is [SecReaderExceptionType.unspecified].
final SecReaderExceptionType? type;
}

/// Exception thrown when a SEC reader is not found.
Expand Down
5 changes: 4 additions & 1 deletion lib/src/ui/l10n/sec_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"searching": "Suchen...",
"scanning": "Scannen...",
"secondsLeft": "{seconds}s\nverbleibend",
"searchingHint": "Stellen Sie sicher, dass die LED \n am Reader blau blinkt."
"searchingHint": "Stellen Sie sicher, dass die LED \n am Reader blau blinkt.",
"incompatibleFirmware": "Inkompatible Firmware. Bitte Update durchführen!",
"tokenFailed": "Neuen Token abrufen fehlgeschlagen!",
"readingsLeft": "Verbleibende Messungen für aktuellen Token:"
}
6 changes: 4 additions & 2 deletions lib/src/ui/l10n/sec_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"disconnecting": "Disconnecting...",
"searching": "Searching...",
"scanning": "Scanning...",
"secondsLeft": "{seconds}s\nleft"

"secondsLeft": "{seconds}s\nleft",
"incompatibleFirmware": "Firmware incompatible. Please update!",
"tokenFailed": "Getting new token failed!",
"readingsLeft": "Readings left with current token:"
}
18 changes: 18 additions & 0 deletions lib/src/ui/l10n/sec_locale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ abstract class SecLocalizations {
/// In en, this message translates to:
/// **'{seconds}s\nleft'**
String secondsLeft(Object seconds);

/// No description provided for @incompatibleFirmware.
///
/// In en, this message translates to:
/// **'Firmware incompatible. Please update!'**
String get incompatibleFirmware;

/// No description provided for @tokenFailed.
///
/// In en, this message translates to:
/// **'Getting new token failed!'**
String get tokenFailed;

/// No description provided for @readingsLeft.
///
/// In en, this message translates to:
/// **'Readings left with current token:'**
String get readingsLeft;
}

class _SecLocalizationsDelegate extends LocalizationsDelegate<SecLocalizations> {
Expand Down
9 changes: 9 additions & 0 deletions lib/src/ui/l10n/sec_locale_de.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ class SecLocalizationsDe extends SecLocalizations {
String secondsLeft(Object seconds) {
return '${seconds}s\nverbleibend';
}

@override
String get incompatibleFirmware => 'Inkompatible Firmware. Bitte Update durchführen!';

@override
String get tokenFailed => 'Neuen Token abrufen fehlgeschlagen!';

@override
String get readingsLeft => 'Verbleibende Messungen für aktuellen Token:';
}
Loading