VLCKeychainCoordinator.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. NSString *passcode = [SSKeychain passwordForService:VLCPasscode account:VLCPasscode];
  66. if (!passcode) {
  67. /* legacy passcode retrieval */
  68. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  69. passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  70. if (passcode)
  71. [self setPasscode:passcode];
  72. }
  73. return passcode;
  74. }
  75. - (BOOL)passcodeLockEnabled
  76. {
  77. NSString *passcode = [self _obtainPasscode];
  78. if (!passcode)
  79. return NO;
  80. if (passcode.length == 0)
  81. return NO;
  82. return YES;
  83. }
  84. - (void)validatePasscode
  85. {
  86. /* we may be called repeatedly as Touch ID uses an out-of-process dialog */
  87. if (_inValidation)
  88. return;
  89. _inValidation = YES;
  90. NSString *passcode = [self _obtainPasscode];
  91. if ([passcode isEqualToString:@""]) {
  92. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  93. }
  94. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  95. _passcodeLockController.delegate = self;
  96. _passcodeLockController.passcode = passcode;
  97. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  98. if (appDelegate.window.rootViewController.presentedViewController)
  99. [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  100. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  101. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  102. [appDelegate.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  103. if (SYSTEM_RUNS_IOS8_OR_LATER) {
  104. if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingPasscodeAllowTouchID]) {
  105. [self _touchIDQuery];
  106. }
  107. }
  108. }
  109. - (void)_touchIDQuery
  110. {
  111. /* don't launch multiple times */
  112. if (_inTouchID)
  113. return;
  114. _inTouchID = YES;
  115. LAContext *myContext = [[LAContext alloc] init];
  116. if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  117. [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
  118. localizedReason:NSLocalizedString(@"TOUCHID_UNLOCK", nil)
  119. reply:^(BOOL success, NSError *error) {
  120. if (success) {
  121. [self PAPasscodeViewControllerDidEnterPasscode:nil];
  122. } else if (error.code == LAErrorSystemCancel)
  123. _inTouchID = NO;
  124. }];
  125. }
  126. }
  127. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  128. {
  129. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  130. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  131. [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  132. _inValidation = NO;
  133. _inTouchID = NO;
  134. }
  135. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  136. {
  137. // FIXME: handle countless failed passcode attempts
  138. }
  139. - (void)setPasscode:(NSString *)passcode
  140. {
  141. if (!passcode) {
  142. [SSKeychain deletePasswordForService:VLCPasscode account:VLCPasscode];
  143. return;
  144. }
  145. [SSKeychain setPassword:passcode forService:VLCPasscode account:VLCPasscode];
  146. }
  147. @end