VLCKeychainCoordinator.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 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. NSString *passcode = [SSKeychain passwordForService:VLCPasscode account:VLCPasscode];
  37. if (!passcode) {
  38. /* legacy passcode retrieval */
  39. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  40. passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  41. if (passcode)
  42. [self setPasscode:passcode];
  43. }
  44. return passcode;
  45. }
  46. - (BOOL)passcodeLockEnabled
  47. {
  48. NSString *passcode = [self _obtainPasscode];
  49. if (!passcode)
  50. return NO;
  51. if (passcode.length == 0)
  52. return NO;
  53. return YES;
  54. }
  55. - (void)validatePasscode
  56. {
  57. NSString *passcode = [self _obtainPasscode];
  58. if ([passcode isEqualToString:@""]) {
  59. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  60. }
  61. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  62. _passcodeLockController.delegate = self;
  63. _passcodeLockController.passcode = passcode;
  64. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  65. if (appDelegate.window.rootViewController.presentedViewController)
  66. [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  67. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  68. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  69. [appDelegate.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  70. }
  71. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  72. {
  73. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
  74. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  75. [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  76. }
  77. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  78. {
  79. // FIXME: handle countless failed passcode attempts
  80. }
  81. - (void)setPasscode:(NSString *)passcode
  82. {
  83. if (!passcode) {
  84. [SSKeychain deletePasswordForService:VLCPasscode account:VLCPasscode];
  85. return;
  86. }
  87. [SSKeychain setPassword:passcode forService:VLCPasscode account:VLCPasscode];
  88. }
  89. @end