Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 3ff7718

Browse files
committed
Merge branch 'master' of github.com:DutchCodingCompany/pin_lock into feature/change_retries_to_tries
2 parents 59e537d + ac27f0a commit 3ff7718

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

lib/src/entities/authenticator.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ abstract class Authenticator with WidgetsBindingObserver {
8383
/// Make an attempt to unlock the app using provided [pin]. If the [pin] is correct,
8484
/// [lockState] will be changed and lock screen dismissed
8585
Future<Either<LocalAuthFailure, Unit>> unlockWithPin({required Pin pin});
86+
87+
/// Check if provided [pin] is correct.
88+
Future<bool> isCorrectPin({required Pin pin});
8689
}

lib/src/entities/authenticator_impl.dart

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
7070
final lastActive = await _repository.getPausedTimestamp();
7171
if (lastActive != null) {
7272
final now = DateTime.now();
73-
if (now.millisecondsSinceEpoch - lastActive.millisecondsSinceEpoch > lockAfterDuration.inMilliseconds) {
74-
_lockController.lock(
75-
availableMethods: await getAvailableBiometricMethods(),
76-
);
73+
if (now.millisecondsSinceEpoch - lastActive.millisecondsSinceEpoch >
74+
lockAfterDuration.inMilliseconds) {
75+
_lockWithBiometricMethods();
7776
}
7877
}
7978
break;
@@ -209,6 +208,12 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
209208
case BiometricType.iris:
210209
methods.add(BiometricMethod.iris);
211210
break;
211+
case BiometricType.weak:
212+
methods.add(BiometricMethod.weak);
213+
break;
214+
case BiometricType.strong:
215+
methods.add(BiometricMethod.strong);
216+
break;
212217
default:
213218
break;
214219
}
@@ -298,6 +303,26 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
298303
return const Left(LocalAuthFailure.unknown);
299304
}
300305

306+
@override
307+
Future<bool> isCorrectPin({required Pin pin}) async {
308+
final isEnabled = await isPinAuthenticationEnabled();
309+
if (!isEnabled) {
310+
return true;
311+
}
312+
if (await _isLockedDueToTooManyAttempts()) {
313+
return false;
314+
}
315+
316+
final userPin = await _repository.getPin(forUser: userId);
317+
if (userPin?.value != pin.value) {
318+
await _repository.addFailedAttempt(DateTime.now(), forUser: userId);
319+
return false;
320+
}
321+
await _repository.resetFailedAttempts(ofUser: userId);
322+
_repository.clearLastPausedTimestamp();
323+
return true;
324+
}
325+
301326
/// -- Helpers --
302327
303328
Future<bool> _supportsBiometricAuthentication() {
@@ -319,15 +344,21 @@ class AuthenticatorImpl with WidgetsBindingObserver implements Authenticator {
319344
if (!isEnabled) {
320345
_lockController.unlock();
321346
} else {
322-
final biometric = await getBiometricAuthenticationAvailability();
323-
if (biometric is Available) {
324-
_lockController.lock(
325-
availableMethods: biometric.isEnabled ? await getAvailableBiometricMethods() : const [],
326-
);
327-
}
328-
if (biometric is Unavailable) {
329-
_lockController.lock(availableMethods: const []);
330-
}
347+
_lockWithBiometricMethods();
348+
}
349+
}
350+
351+
Future<void> _lockWithBiometricMethods() async {
352+
final biometric = await getBiometricAuthenticationAvailability();
353+
if (biometric is Available) {
354+
_lockController.lock(
355+
availableMethods: biometric.isEnabled
356+
? await getAvailableBiometricMethods()
357+
: const [],
358+
);
359+
}
360+
if (biometric is Unavailable) {
361+
_lockController.lock(availableMethods: const []);
331362
}
332363
}
333364
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enum BiometricMethod { fingerprint, face, iris }
1+
enum BiometricMethod { fingerprint, face, iris, weak, strong }

lib/src/presentation/authenticator_widget.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ class _AuthenticatorWidgetState extends State<AuthenticatorWidget> {
7070
late final StreamSubscription lockSubscription;
7171
OverlayEntry? overlayEntry;
7272
bool _isShowingSplashScreen = true;
73+
late final Stream<LockState> lockState ;
7374

7475
@override
7576
void initState() {
7677
super.initState();
78+
lockState = widget.authenticator.lockState;
7779
PinLock.setHideAppContent(
7880
preference: widget.hideAppContent,
7981
iosAssetImage: widget.iosImageAsset,
8082
);
81-
lockSubscription = widget.authenticator.lockState.listen((event) {
83+
lockSubscription = lockState.listen((event) {
8284
if (event is Unlocked) {
8385
overlayEntry?.remove();
8486
overlayEntry = null;
@@ -122,7 +124,7 @@ class _AuthenticatorWidgetState extends State<AuthenticatorWidget> {
122124
@override
123125
Widget build(BuildContext context) {
124126
return StreamBuilder<LockState>(
125-
stream: widget.authenticator.lockState,
127+
stream: lockState,
126128
builder: (context, snapshot) {
127129
if (snapshot.hasData && !_isShowingSplashScreen) {
128130
return widget.child;

0 commit comments

Comments
 (0)