VLCKeychainCoordinator.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "KeychainItemWrapper.h"
  16. NSString *const VLCPasscodeValidated = @"VLCPasscodeValidated";
  17. NSString *const VLCPasscode = @"org.videolan.vlc-ios.passcode";
  18. @interface VLCKeychainCoordinator () <PAPasscodeViewControllerDelegate>
  19. {
  20. PAPasscodeViewController *_passcodeLockController;
  21. NSDictionary *_passcodeQuery;
  22. }
  23. @end
  24. @implementation VLCKeychainCoordinator
  25. + (instancetype)defaultCoordinator
  26. {
  27. static VLCKeychainCoordinator *sharedInstance = nil;
  28. static dispatch_once_t pred;
  29. dispatch_once(&pred, ^{
  30. sharedInstance = [VLCKeychainCoordinator new];
  31. });
  32. return sharedInstance;
  33. }
  34. - (NSString *)_obtainPasscode
  35. {
  36. KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:VLCPasscode
  37. accessGroup:kVLCApplicationGroupIdentifier];
  38. NSString *passcode = [keychain objectForKey:(__bridge id)kSecValueData];
  39. if (!passcode) {
  40. /* legacy passcode retrieval */
  41. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  42. passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  43. if (passcode)
  44. [self setPasscode:passcode];
  45. }
  46. return passcode;
  47. }
  48. - (BOOL)passcodeLockEnabled
  49. {
  50. NSString *passcode = [self _obtainPasscode];
  51. if (!passcode)
  52. return NO;
  53. if (passcode.length == 0)
  54. return NO;
  55. return YES;
  56. }
  57. - (void)validatePasscode
  58. {
  59. NSString *passcode = [self _obtainPasscode];
  60. if ([passcode isEqualToString:@""]) {
  61. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  62. }
  63. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  64. _passcodeLockController.delegate = self;
  65. _passcodeLockController.passcode = passcode;
  66. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  67. if (appDelegate.window.rootViewController.presentedViewController)
  68. [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  69. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  70. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  71. [appDelegate.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  72. }
  73. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  74. {
  75. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  76. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  77. [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  78. }
  79. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  80. {
  81. // FIXME: handle countless failed passcode attempts
  82. }
  83. - (void)setPasscode:(NSString *)passcode
  84. {
  85. if (!passcode)
  86. passcode = @"";
  87. KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:VLCPasscode
  88. accessGroup:kVLCApplicationGroupIdentifier];
  89. [keychain setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlock forKey:(__bridge id)kSecAttrAccessible];
  90. [keychain setObject:passcode forKey:(__bridge id)kSecValueData];
  91. keychain = nil;
  92. }
  93. @end