VLCKeychainCoordinator.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*****************************************************************************
  2. * VLCKeychainCoordinator.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCKeychainCoordinator.h"
  13. #import "PAPasscodeViewController.h"
  14. #import <XKKeychain/XKKeychainGenericPasswordItem.h>
  15. #import <LocalAuthentication/LocalAuthentication.h>
  16. NSString *const VLCPasscode = @"org.videolan.vlc-ios.passcode";
  17. @interface VLCKeychainCoordinator () <PAPasscodeViewControllerDelegate>
  18. {
  19. PAPasscodeViewController *_passcodeLockController;
  20. void (^_completion)(void);
  21. BOOL _avoidPromptingTouchID;
  22. }
  23. @end
  24. @implementation VLCKeychainCoordinator
  25. + (instancetype)defaultCoordinator
  26. {
  27. static VLCKeychainCoordinator *sharedInstance = nil;
  28. static dispatch_once_t pred;
  29. dispatch_once(&pred, ^{
  30. sharedInstance = [VLCKeychainCoordinator new];
  31. });
  32. return sharedInstance;
  33. }
  34. - (instancetype)init
  35. {
  36. self = [super init];
  37. if (!self)
  38. return nil;
  39. [[NSNotificationCenter defaultCenter] addObserver:self
  40. selector:@selector(appInForeground:)
  41. name:UIApplicationDidBecomeActiveNotification
  42. object:nil];
  43. return self;
  44. }
  45. - (void)appInForeground:(NSNotification *)notification
  46. {
  47. /* our touch ID session is killed by the OS if the app moves to background, so re-init */
  48. UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
  49. if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]){
  50. UINavigationController *navCon = (UINavigationController *)rootViewController.presentedViewController;
  51. if ([navCon.topViewController isKindOfClass:[PAPasscodeViewController class]] && [self touchIDEnabled]){
  52. [self _touchIDQuery];
  53. }
  54. }
  55. }
  56. - (NSString *)passcodeFromKeychain
  57. {
  58. XKKeychainGenericPasswordItem *item = [XKKeychainGenericPasswordItem itemForService:VLCPasscode account:VLCPasscode error:nil];
  59. NSString *passcode = item.secret.stringValue;
  60. return passcode;
  61. }
  62. - (BOOL)touchIDEnabled
  63. {
  64. return [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeAllowTouchID];
  65. }
  66. - (BOOL)passcodeLockEnabled
  67. {
  68. return [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeOnKey];
  69. }
  70. - (void)validatePasscodeWithCompletion:(void(^)(void))completion
  71. {
  72. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  73. _passcodeLockController.delegate = self;
  74. _passcodeLockController.passcode = [self passcodeFromKeychain];
  75. _completion = completion;
  76. UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
  77. if (rootViewController.presentedViewController)
  78. [rootViewController dismissViewControllerAnimated:NO completion:nil];
  79. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  80. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  81. [rootViewController presentViewController:navCon animated:YES completion:^{
  82. if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeAllowTouchID]) {
  83. [self _touchIDQuery];
  84. }
  85. }];
  86. }
  87. - (void)_touchIDQuery
  88. {
  89. //if we just entered background don't show TouchID
  90. if (_avoidPromptingTouchID || [UIApplication sharedApplication].applicationState != UIApplicationStateActive)
  91. return;
  92. LAContext *myContext = [[LAContext alloc] init];
  93. if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  94. _avoidPromptingTouchID = YES;
  95. [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
  96. localizedReason:NSLocalizedString(@"TOUCHID_UNLOCK", nil)
  97. reply:^(BOOL success, NSError *error) {
  98. //if we cancel we don't want to show TouchID again
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. if (success) {
  101. [[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:YES completion:^{
  102. _completion();
  103. _completion = nil;
  104. _avoidPromptingTouchID = NO;
  105. }];
  106. } else {
  107. //user hit cancel and wants to enter the passcode
  108. _avoidPromptingTouchID = YES;
  109. }
  110. });
  111. }];
  112. }
  113. }
  114. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  115. {
  116. _avoidPromptingTouchID = NO;
  117. [[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:YES completion:^{
  118. _completion();
  119. _completion = nil;
  120. }];
  121. }
  122. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  123. {
  124. // FIXME: handle countless failed passcode attempts
  125. }
  126. - (void)setPasscode:(NSString *)passcode
  127. {
  128. if (!passcode) {
  129. [XKKeychainGenericPasswordItem removeItemsForService:VLCPasscode error:nil];
  130. return;
  131. }
  132. XKKeychainGenericPasswordItem *keychainItem = [[XKKeychainGenericPasswordItem alloc] init];
  133. keychainItem.service = VLCPasscode;
  134. keychainItem.account = VLCPasscode;
  135. keychainItem.secret.stringValue = passcode;
  136. [keychainItem saveWithError:nil];
  137. }
  138. @end