VLCKeychainCoordinator.m 5.4 KB

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