-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathAppLockViewController.m
224 lines (185 loc) · 8.57 KB
/
AppLockViewController.m
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// PrivacyViewController.m
// Strongbox-iOS
//
// Created by Mark on 14/05/2019.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "AppLockViewController.h"
#import "PinEntryController.h"
#import "Alerts.h"
#import "DatabasePreferences.h"
#import "AutoFillManager.h"
#import "StrongboxiOSFilesManager.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "BiometricsManager.h"
#import "AppPreferences.h"
#import "Utils.h"
@interface AppLockViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonUnlock;
@property (weak, nonatomic) IBOutlet UILabel *labelUnlockAttemptsRemaining;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewLogo;
@property BOOL firstAppearance;
@end
@implementation AppLockViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupImageView];
[self updateFailedUnlockAttemptsUI];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(appWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
self.firstAppearance = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ( self.firstAppearance ) {
self.firstAppearance = NO;
[self attemptBeginUnlockSequence:0];
}
}
- (void)appWillEnterForeground {
slog(@"AppLockViewController::appWillEnterForeground");
[self attemptBeginUnlockSequence:0];
}
- (void)attemptBeginUnlockSequence:(int)attemptCount {
slog(@"AppLockViewController::attemptBeginUnlockSequence: %ld", attemptCount);
if ( Utils.isAppInForeground ) {
[self beginUnlockSequence];
}
else if ( attemptCount < 3 ) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self attemptBeginUnlockSequence:attemptCount + 1];
});
}
}
- (void)setupImageView {
self.imageViewLogo.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onUnlock:)];
tapGesture1.numberOfTapsRequired = 1;
[self.imageViewLogo addGestureRecognizer:tapGesture1];
}
- (IBAction)onUnlock:(id)sender {
[self beginUnlockSequence];
}
- (void)beginUnlockSequence {
slog(@"beginUnlockSequence....");
if(AppPreferences.sharedInstance.appLockMode == kBiometric || AppPreferences.sharedInstance.appLockMode == kBoth) {
if(BiometricsManager.isBiometricIdAvailable) {
[self requestBiometric];
}
else {
[Alerts info:self
title:NSLocalizedString(@"privacy_vc_prompt_biometrics_unavailable_title", @"Biometrics Unavailable")
message:NSLocalizedString(@"privacy_vc_prompt_biometrics_unavailable_message", @"This application requires a biometric unlock but biometrics is unavailable on this device. You must re-enable biometrics to continue unlocking this application.")];
}
}
else if (AppPreferences.sharedInstance.appLockMode == kPinCode || AppPreferences.sharedInstance.appLockMode == kBoth) {
[self requestPin:NO];
}
else {
AppPreferences.sharedInstance.failedUnlockAttempts = 0;
[self onDone:NO];
}
}
- (void)requestBiometric {
[BiometricsManager.sharedInstance requestBiometricId:NSLocalizedString(@"privacy_vc_prompt_identify_to_open", @"Identify to Open Strongbox")
fallbackTitle:AppPreferences.sharedInstance.appLockAllowDevicePasscodeFallbackForBio ? nil : @""
completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
if (AppPreferences.sharedInstance.appLockMode == kPinCode || AppPreferences.sharedInstance.appLockMode == kBoth) {
[self requestPin:YES];
}
else {
AppPreferences.sharedInstance.failedUnlockAttempts = 0;
[self onDone:YES];
}
});
}
else {
if (error.code == LAErrorUserCancel) {
slog(@"User Cancelled - Not Incrementing Fail Count...");
}
else if ( error.code == LAErrorUserFallback ) {
slog(@"LAErrorUserFallback");
}
else {
slog(@"Biometric Fail: [%@]", error);
}
}}];
}
- (void)updateFailedUnlockAttemptsUI {
NSUInteger failed = AppPreferences.sharedInstance.failedUnlockAttempts;
if (failed > 0 ) {
if(AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount > 0) {
NSInteger remaining = AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount - failed;
if(remaining > 0) {
self.labelUnlockAttemptsRemaining.text = [NSString stringWithFormat:NSLocalizedString(@"privacy_vc_label_unlock_attempts_fmt", @"Unlock Attempts Remaining: %ld"), (long)remaining];
}
else {
self.labelUnlockAttemptsRemaining.text = NSLocalizedString(@"privacy_vc_label_unlock_attempts_exceeded", @"Unlock Attempts Exceeded");
}
self.labelUnlockAttemptsRemaining.hidden = NO;
self.labelUnlockAttemptsRemaining.textColor = UIColor.systemRedColor;
}
else {
self.labelUnlockAttemptsRemaining.text = [NSString stringWithFormat:NSLocalizedString(@"privacy_vc_label_number_of_failed_unlock_attempts_fmt", @"%@ Failed Unlock Attempts"), @(failed)];
self.labelUnlockAttemptsRemaining.hidden = NO;
self.labelUnlockAttemptsRemaining.textColor = UIColor.systemOrangeColor;
}
}
else {
self.labelUnlockAttemptsRemaining.hidden = YES;
}
}
- (void)requestPin:(BOOL)afterSuccessfulBiometricAuthentication {
PinEntryController* pinEntryVc = PinEntryController.newControllerForAppLock;
pinEntryVc.pinLength = AppPreferences.sharedInstance.appLockPin.length;
if(AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount > 0 && AppPreferences.sharedInstance.failedUnlockAttempts > 0) {
NSInteger remaining = AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount - AppPreferences.sharedInstance.failedUnlockAttempts;
if(remaining > 0) {
pinEntryVc.warning = [NSString stringWithFormat:NSLocalizedString(@"privacy_vc_prompt_pin_attempts_remaining_fmt", @"%ld Attempts Remaining"), (long)remaining];
}
}
pinEntryVc.onDone = ^(PinEntryResponse response, NSString * _Nullable pin) {
if( response == kPinEntryResponseOk ) {
if([pin isEqualToString:AppPreferences.sharedInstance.appLockPin]) {
AppPreferences.sharedInstance.failedUnlockAttempts = 0;
UINotificationFeedbackGenerator* gen = [[UINotificationFeedbackGenerator alloc] init];
[gen notificationOccurred:UINotificationFeedbackTypeSuccess];
[self onDone:afterSuccessfulBiometricAuthentication];
}
else {
[self incrementFailedUnlockCount];
UINotificationFeedbackGenerator* gen = [[UINotificationFeedbackGenerator alloc] init];
[gen notificationOccurred:UINotificationFeedbackTypeError];
}
}
};
[self presentViewController:pinEntryVc animated:YES completion:nil];
}
- (void)incrementFailedUnlockCount {
dispatch_async(dispatch_get_main_queue(), ^{
AppPreferences.sharedInstance.failedUnlockAttempts = AppPreferences.sharedInstance.failedUnlockAttempts + 1;
slog(@"Failed Unlocks: %lu", (unsigned long)AppPreferences.sharedInstance.failedUnlockAttempts);
[self updateFailedUnlockAttemptsUI];
if(AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount > 0) {
if(AppPreferences.sharedInstance.failedUnlockAttempts >= AppPreferences.sharedInstance.deleteDataAfterFailedUnlockCount) {
[self deleteAllData];
}
}
});
}
- (void)deleteAllData {
[AutoFillManager.sharedInstance clearAutoFillQuickTypeDatabase];
[StrongboxFilesManager.sharedInstance deleteAllLocalAndAppGroupFiles];
[DatabasePreferences nukeAllDeleteUnderlyingIfPossible];
}
- (void)onDone:(BOOL)userJustCompletedBiometricAuthentication {
if ( self.onUnlockDone ) {
self.onUnlockDone (userJustCompletedBiometricAuthentication);
}
}
@end