VLCKeychainCoordinator.m 5.4 KB

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