VLCKeychainCoordinator.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "VLCAppDelegate.h"
  15. #import "SSKeychain.h"
  16. #import <LocalAuthentication/LocalAuthentication.h>
  17. NSString *const VLCPasscodeValidated = @"VLCPasscodeValidated";
  18. NSString *const VLCPasscode = @"org.videolan.vlc-ios.passcode";
  19. @interface VLCKeychainCoordinator () <PAPasscodeViewControllerDelegate>
  20. {
  21. PAPasscodeViewController *_passcodeLockController;
  22. NSDictionary *_passcodeQuery;
  23. BOOL _inValidation;
  24. BOOL _inTouchID;
  25. }
  26. @end
  27. @implementation VLCKeychainCoordinator
  28. + (instancetype)defaultCoordinator
  29. {
  30. static VLCKeychainCoordinator *sharedInstance = nil;
  31. static dispatch_once_t pred;
  32. dispatch_once(&pred, ^{
  33. sharedInstance = [VLCKeychainCoordinator new];
  34. });
  35. return sharedInstance;
  36. }
  37. - (instancetype)init
  38. {
  39. self = [super init];
  40. if (!self)
  41. return nil;
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(appInForeground:)
  44. name:UIApplicationDidBecomeActiveNotification
  45. object:nil];
  46. return self;
  47. }
  48. - (void)dealloc
  49. {
  50. [[NSNotificationCenter defaultCenter] removeObserver:self];
  51. }
  52. - (void)appInForeground:(NSNotification *)notification
  53. {
  54. /* our touch ID session is killed by the OS if the app moves to background, so re-init */
  55. if (_inValidation) {
  56. if (SYSTEM_RUNS_IOS8_OR_LATER) {
  57. if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeAllowTouchID]) {
  58. [self _touchIDQuery];
  59. }
  60. }
  61. }
  62. }
  63. - (NSString *)_obtainPasscode
  64. {
  65. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  66. BOOL wasReset = [defaults boolForKey:kVLCSettingPasscodeResetOnUpgrade];
  67. if (wasReset) {
  68. NSString *passcode = [SSKeychain passwordForService:VLCPasscode account:VLCPasscode];
  69. return passcode;
  70. }
  71. [SSKeychain deletePasswordForService:VLCPasscode account:VLCPasscode];
  72. [defaults setBool:YES forKey:kVLCSettingPasscodeResetOnUpgrade];
  73. [defaults synchronize];
  74. return nil;
  75. }
  76. - (BOOL)passcodeLockEnabled
  77. {
  78. NSString *passcode = [self _obtainPasscode];
  79. if (!passcode)
  80. return NO;
  81. if (passcode.length == 0)
  82. return NO;
  83. return YES;
  84. }
  85. - (void)validatePasscode
  86. {
  87. /* we may be called repeatedly as Touch ID uses an out-of-process dialog */
  88. if (_inValidation)
  89. return;
  90. _inValidation = YES;
  91. NSString *passcode = [self _obtainPasscode];
  92. if (passcode == nil || [passcode isEqualToString:@""]) {
  93. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  94. return;
  95. }
  96. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  97. _passcodeLockController.delegate = self;
  98. _passcodeLockController.passcode = passcode;
  99. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  100. if (appDelegate.window.rootViewController.presentedViewController)
  101. [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  102. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  103. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  104. [appDelegate.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  105. if (SYSTEM_RUNS_IOS8_OR_LATER) {
  106. if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeAllowTouchID]) {
  107. [self _touchIDQuery];
  108. }
  109. }
  110. }
  111. - (void)_touchIDQuery
  112. {
  113. /* don't launch multiple times */
  114. if (_inTouchID)
  115. return;
  116. _inTouchID = YES;
  117. LAContext *myContext = [[LAContext alloc] init];
  118. if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  119. [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
  120. localizedReason:NSLocalizedString(@"TOUCHID_UNLOCK", nil)
  121. reply:^(BOOL success, NSError *error) {
  122. if (success) {
  123. [self PAPasscodeViewControllerDidEnterPasscode:nil];
  124. } else if (error.code == LAErrorSystemCancel)
  125. _inTouchID = NO;
  126. }];
  127. }
  128. }
  129. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  130. {
  131. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  132. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  133. [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  134. _inValidation = NO;
  135. _inTouchID = NO;
  136. }
  137. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  138. {
  139. // FIXME: handle countless failed passcode attempts
  140. }
  141. - (void)setPasscode:(NSString *)passcode
  142. {
  143. if (!passcode) {
  144. [SSKeychain deletePasswordForService:VLCPasscode account:VLCPasscode];
  145. return;
  146. }
  147. [SSKeychain setPassword:passcode forService:VLCPasscode account:VLCPasscode];
  148. }
  149. @end