This repository has been archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathASAuthenticationController.mm
executable file
·396 lines (341 loc) · 16.6 KB
/
ASAuthenticationController.mm
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#import "ASAuthenticationController.h"
#include <sys/sysctl.h>
#import <objc/runtime.h>
#import <AudioToolbox/AudioServices.h>
#import "NSTimer+Blocks.h"
#import "ASPreferences.h"
#import "ASPasscodeHandler.h"
#define kBundlePath @"/Library/Application Support/Asphaleia/AsphaleiaAssets.bundle"
#define titleWithSpacingForIcon(t) [NSString stringWithFormat:@"\n\n\n%@",t]
#define titleWithSpacingForSmallIcon(t) [NSString stringWithFormat:@"\n\n%@",t]
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface ASAuthenticationController ()
- (void)receivedNotificationOfName:(NSString *)name fingerprint:(id)fingerprint;
@end
void touchIDNotificationReceived(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
id fingerprint = nil;
if ([(__bridge NSString *)name isEqualToString:@"com.a3tweaks.asphaleia.authsuccess"]) {
fingerprint = [[ASTouchIDController sharedInstance] lastMatchedFingerprint];
}
[[ASAuthenticationController sharedInstance] receivedNotificationOfName:(__bridge NSString *)name fingerprint:fingerprint];
}
@implementation ASAuthenticationController
static ASAuthenticationController *sharedCommonObj;
+ (instancetype)sharedInstance {
static dispatch_once_t token = 0;
dispatch_once(&token, ^{
sharedCommonObj = [[ASAuthenticationController alloc] init];
[sharedCommonObj registerForTouchIDNotifications];
});
return sharedCommonObj;
}
- (void)dealloc {
[self deregisterForTouchIDNotifications];
}
- (ASAuthenticationAlert *)returnAppAuthenticationAlertWithApplication:(NSString *)appIdentifier customMessage:(NSString *)customMessage delegate:(id<ASAuthenticationAlertDelegate>)delegate {
NSString *message;
if (customMessage) {
message = customMessage;
} else {
message = @"Scan fingerprint to open.";
}
ASAuthenticationAlert *alertView = [[objc_getClass("ASAuthenticationAlert") alloc] initWithApplication:appIdentifier
message:message
delegate:delegate];
alertView.tag = ASAuthenticationItem;
currentAuthAppBundleID = appIdentifier;
return alertView;
}
- (ASAuthenticationAlert *)returnAuthenticationAlertOfType:(ASAuthenticationAlertType)alertType delegate:(id<ASAuthenticationAlertDelegate>)delegate {
NSBundle *asphaleiaAssets = [[NSBundle alloc] initWithPath:kBundlePath];
NSString *title;
UIImage *iconImage;
int tag;
switch (alertType) {
case ASAuthenticationAlertAppArranging: {
title = @"Arrange Apps";
iconImage = [UIImage imageNamed:@"IconEditMode.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertSwitcher: {
title = @"Multitasking";
iconImage = [UIImage imageNamed:@"IconMultitasking.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertSpotlight: {
title = @"Spotlight";
iconImage = [UIImage imageNamed:@"IconSpotlight.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertPowerDown: {
title = @"Slide to Power Off";
iconImage = [UIImage imageNamed:@"IconPowerOff.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertControlCentre: {
title = @"Control Center";
iconImage = [UIImage imageNamed:@"IconControlCenter.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertControlPanel: {
title = @"Asphaleia Control Panel";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationSecurityMod;
break;
}
case ASAuthenticationAlertDynamicSelection: {
title = @"Dynamic Selection";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationSecurityMod;
break;
}
case ASAuthenticationAlertPhotos: {
title = @"Photo Library";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
case ASAuthenticationAlertSettingsPanel: {
title = @"Settings Panel";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationItem;
break;
}
case ASAuthenticationAlertFlipswitch: {
title = @"Flipswitch";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationItem;
break;
}
default: {
title = @"Asphaleia";
iconImage = [UIImage imageNamed:@"IconDefault.png" inBundle:asphaleiaAssets compatibleWithTraitCollection:nil];
tag = ASAuthenticationFunction;
break;
}
}
UIImageView *imgView = [[UIImageView alloc] initWithImage:iconImage];
imgView.frame = CGRectMake(0,0,iconImage.size.width,iconImage.size.height);
ASAuthenticationAlert *alertView = [[objc_getClass("ASAuthenticationAlert") alloc] initWithTitle:title
message:@"Scan fingerprint to access."
icon:imgView
smallIcon:YES
delegate:delegate];
alertView.tag = tag;
return alertView;
}
- (BOOL)authenticateAppWithDisplayIdentifier:(NSString *)appIdentifier customMessage:(NSString *)customMessage dismissedHandler:(ASCommonAuthenticationHandler)handler {
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
if (![[ASPreferences sharedInstance] requiresSecurityForApp:appIdentifier]) {
return NO;
}
authHandler = [handler copy];
ASAuthenticationAlert *alertView = [self returnAppAuthenticationAlertWithApplication:appIdentifier customMessage:customMessage delegate:self];
if (![[ASPreferences sharedInstance] touchIDEnabled] && ![[ASPreferences sharedInstance] passcodeEnabled]) {
return NO;
}
if (![[ASPreferences sharedInstance] touchIDEnabled]) {
[[ASPasscodeHandler sharedInstance] showInKeyWindowWithPasscode:[[ASPreferences sharedInstance] getPasscode] iconView:nil eventBlock:^void(BOOL authenticated){
if (authenticated)
_appUserAuthorisedID = appIdentifier;
authHandler(!authenticated);
}];
return YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
[alertView show];
});
return YES;
}
- (BOOL)authenticateFunction:(ASAuthenticationAlertType)alertType dismissedHandler:(ASCommonAuthenticationHandler)handler {
if ([ASPreferences sharedInstance].asphaleiaDisabled) {
return NO;
}
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
authHandler = [handler copy];
ASAuthenticationAlert *alertView = [self returnAuthenticationAlertOfType:alertType delegate:self];
if (![[ASPreferences sharedInstance] touchIDEnabled] && ![[ASPreferences sharedInstance] passcodeEnabled]) {
return NO;
}
if (![[ASPreferences sharedInstance] touchIDEnabled]) {
[[ASPasscodeHandler sharedInstance] showInKeyWindowWithPasscode:[[ASPreferences sharedInstance] getPasscode] iconView:nil eventBlock:^void(BOOL authenticated){
authHandler(!authenticated);
}];
return YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
[alertView show];
});
return YES;
}
- (BOOL)authenticateAppWithIconView:(SBIconView *)iconView authenticatedHandler:(ASCommonAuthenticationHandler)handler {
if (![[NSBundle mainBundle].bundleIdentifier isEqualToString:@"com.apple.springboard"]) {
return NO;
}
if ([ASPreferences sharedInstance].asphaleiaDisabled || [ASPreferences sharedInstance].itemSecurityDisabled || [[iconView icon] isDownloadingIcon]) {
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
[iconView setHighlighted:NO];
return NO;
}
NSString *displayName;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.4")) {
displayName = [iconView.icon displayNameForLocation:iconView.location];
} else {
displayName = [iconView.icon displayName];
}
currentAuthAppBundleID = iconView.icon.applicationBundleID;
if (_fingerglyph && _currentHSIconView) {
[iconView setHighlighted:NO];
if ([iconView isEqual:_currentHSIconView]) {
[[ASPasscodeHandler sharedInstance] showInKeyWindowWithPasscode:[[ASPreferences sharedInstance] getPasscode] iconView:iconView eventBlock:^void(BOOL authenticated){
if (authenticated) {
[ASAuthenticationController sharedInstance].appUserAuthorisedID = iconView.icon.applicationBundleID;
}
handler(!authenticated);
}];
}
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
return YES;
} else if (([iconView.icon isApplicationIcon] && ![[ASPreferences sharedInstance] requiresSecurityForApp:iconView.icon.applicationBundleID]) || ([iconView.icon isFolderIcon] && ![[ASPreferences sharedInstance] requiresSecurityForFolder:displayName])) {
[iconView setHighlighted:NO];
return NO;
} else if (![[ASPreferences sharedInstance] touchIDEnabled] && [[ASPreferences sharedInstance] passcodeEnabled]) {
[iconView setHighlighted:NO];
[[ASPasscodeHandler sharedInstance] showInKeyWindowWithPasscode:[[ASPreferences sharedInstance] getPasscode] iconView:iconView eventBlock:^void(BOOL authenticated){
if (authenticated){
[ASAuthenticationController sharedInstance].appUserAuthorisedID = iconView.icon.applicationBundleID;
}
handler(!authenticated);
}];
return YES;
}
if (!_anywhereTouchWindow) {
_anywhereTouchWindow = [[ASTouchWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
_currentHSIconView = iconView;
[self initialiseGlyphIfRequired];
CGRect fingerframe = _fingerglyph.frame;
fingerframe.size.height = [iconView _iconImageView].frame.size.height-10;
fingerframe.size.width = [iconView _iconImageView].frame.size.width-10;
_fingerglyph.frame = fingerframe;
_fingerglyph.center = CGPointMake(CGRectGetMidX([iconView _iconImageView].bounds),CGRectGetMidY([iconView _iconImageView].bounds));
[[iconView _iconImageView] addSubview:_fingerglyph];
_fingerglyph.transform = CGAffineTransformMakeScale(0.01,0.01);
[UIView animateWithDuration:0.3f animations:^{
_fingerglyph.transform = CGAffineTransformMakeScale(1,1);
}];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.a3tweaks.asphaleia.startmonitoring"), NULL, NULL, YES);
[_currentHSIconView asphaleia_updateLabelWithText:@"Scan finger..."];
[_anywhereTouchWindow blockTouchesAllowingTouchInView:_currentHSIconView touchBlockedHandler:^void(ASTouchWindow *touchWindow, BOOL blockedTouch){
if (blockedTouch) {
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
handler(YES);
}
}];
return YES;
}
- (void)receivedNotificationOfName:(NSString *)name fingerprint:(id)fingerprint {
if (self.currentHSIconView) {
if ([fingerprint isKindOfClass:objc_getClass("BiometricKitIdentity")]) {
if (![[ASPreferences sharedInstance] fingerprintProtectsSecureItems:[fingerprint name]]) {
name = @"com.a3tweaks.asphaleia.authfailed";
}
}
if (![[NSBundle mainBundle].bundleIdentifier isEqualToString:@"com.apple.springboard"]) {
return;
}
if ([name isEqualToString:@"com.a3tweaks.asphaleia.fingerdown"]) {
if (_fingerglyph && _currentHSIconView) {
[_fingerglyph setState:1 animated:YES completionHandler:nil];
[_currentHSIconView asphaleia_updateLabelWithText:@"Scanning..."];
}
} else if ([name isEqualToString:@"com.a3tweaks.asphaleia.fingerup"]) {
if (_fingerglyph) {
[_fingerglyph setState:0 animated:YES completionHandler:nil];
}
} else if ([name isEqualToString:@"com.a3tweaks.asphaleia.authsuccess"]) {
if (_fingerglyph && _currentHSIconView) {
[ASAuthenticationController sharedInstance].appUserAuthorisedID = currentAuthAppBundleID;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.3")) {
[_currentHSIconView.icon launchFromLocation:_currentHSIconView.location context:nil];
} else {
[_currentHSIconView.icon launchFromLocation:_currentHSIconView.location];
}
[[objc_getClass("SBIconController") sharedInstance] asphaleia_resetAsphaleiaIconView];
currentAuthAppBundleID = nil;
}
} else if ([name isEqualToString:@"com.a3tweaks.asphaleia.authfailed"]) {
if (_fingerglyph && _currentHSIconView) {
[_fingerglyph setState:0 animated:YES completionHandler:nil];
[_currentHSIconView asphaleia_updateLabelWithText:@"Scan finger..."];
}
}
}
}
- (void)registerForTouchIDNotifications {
addObserver(touchIDNotificationReceived, "com.a3tweaks.asphaleia.fingerdown");
addObserver(touchIDNotificationReceived, "com.a3tweaks.asphaleia.fingerup");
addObserver(touchIDNotificationReceived, "com.a3tweaks.asphaleia.authsuccess");
addObserver(touchIDNotificationReceived, "com.a3tweaks.asphaleia.authfailed");
}
- (void)deregisterForTouchIDNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dismissAnyAuthenticationAlerts {
if (self.currentAuthAlert)
[self.currentAuthAlert dismiss];
}
- (NSArray *)allSubviewsOfView:(UIView *)view {
NSMutableArray *viewArray = [[NSMutableArray alloc] init];
[viewArray addObject:view];
for (UIView *subview in view.subviews) {
[viewArray addObjectsFromArray:(NSArray *)[self allSubviewsOfView:subview]];
}
return [NSArray arrayWithArray:viewArray];
}
- (void)initialiseGlyphIfRequired {
if (!_fingerglyph) {
_fingerglyph = [(PKGlyphView*)[objc_getClass("PKGlyphView") alloc] initWithStyle:1];
_fingerglyph.secondaryColor = [UIColor grayColor];
_fingerglyph.primaryColor = [UIColor redColor];
}
}
// ASAuthenticationAlert delegate methods
- (void)authAlertView:(ASAuthenticationAlert *)alertView dismissed:(BOOL)dismissed authorised:(BOOL)authorised fingerprint:(BiometricKitIdentity *)fingerprint {
BOOL correctFingerUsed = YES;
if ([fingerprint isKindOfClass:objc_getClass("BiometricKitIdentity")]) {
correctFingerUsed = NO;
switch (self.currentAuthAlert.tag) {
case ASAuthenticationItem:
correctFingerUsed = [[ASPreferences sharedInstance] fingerprintProtectsSecureItems:[fingerprint name]];
break;
case ASAuthenticationFunction:
correctFingerUsed = [[ASPreferences sharedInstance] fingerprintProtectsAdvancedSecurity:[fingerprint name]];
break;
case ASAuthenticationSecurityMod:
correctFingerUsed = [[ASPreferences sharedInstance] fingerprintProtectsSecurityMods:[fingerprint name]];
break;
default:
correctFingerUsed = YES;
break;
}
}
if (!correctFingerUsed) {
return;
} else if (correctFingerUsed && !dismissed) {
[self.currentAuthAlert dismiss];
}
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.a3tweaks.asphaleia.stopmonitoring"), NULL, NULL, YES);
if (authorised) {
_appUserAuthorisedID = currentAuthAppBundleID;
}
authHandler(!(authorised && correctFingerUsed));
self.currentAuthAlert = nil;
currentAuthAppBundleID = nil;
}
@end