VLCKeychainCoordinator.m 5.9 KB

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