VLCKeychainCoordinator.m 3.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*****************************************************************************
 * VLCKeychainCoordinator.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCKeychainCoordinator.h"
#import "PAPasscodeViewController.h"
#import "VLCAppDelegate.h"
16
#import "SSKeychain.h"
17
18
19

NSString *const VLCPasscodeValidated = @"VLCPasscodeValidated";

20
21
NSString *const VLCPasscode = @"org.videolan.vlc-ios.passcode";

22
23
24
@interface VLCKeychainCoordinator () <PAPasscodeViewControllerDelegate>
{
    PAPasscodeViewController *_passcodeLockController;
25
    NSDictionary *_passcodeQuery;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}

@end

@implementation VLCKeychainCoordinator

+ (instancetype)defaultCoordinator
{
    static VLCKeychainCoordinator *sharedInstance = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [VLCKeychainCoordinator new];
    });

    return sharedInstance;
}

44
45
46

- (NSString *)_obtainPasscode
{
47
    NSString *passcode = [SSKeychain passwordForService:VLCPasscode account:VLCPasscode];
48
49
50
51
52
53
54
55
56
57
58
59

    if (!passcode) {
        /* legacy passcode retrieval */
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
        if (passcode)
            [self setPasscode:passcode];
    }

    return passcode;
}

60
61
- (BOOL)passcodeLockEnabled
{
62
63
    NSString *passcode = [self _obtainPasscode];

64
65
66
67
68
69
70
71
72
73
74
    if (!passcode)
        return NO;

    if (passcode.length == 0)
        return NO;

    return YES;
}

- (void)validatePasscode
{
75
    NSString *passcode = [self _obtainPasscode];
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    if ([passcode isEqualToString:@""]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];
    }

    _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
    _passcodeLockController.delegate = self;
    _passcodeLockController.passcode = passcode;

    VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;

    if (appDelegate.window.rootViewController.presentedViewController)
        [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [appDelegate.window.rootViewController presentViewController:navCon animated:NO completion:nil];
}


- (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
{
    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPasscodeValidated object:self];

    VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
{
    // FIXME: handle countless failed passcode attempts
}

- (void)setPasscode:(NSString *)passcode
{
110
111
112
113
    if (!passcode) {
        [SSKeychain deletePasswordForService:VLCPasscode account:VLCPasscode];
        return;
    }
114

115
    [SSKeychain setPassword:passcode forService:VLCPasscode account:VLCPasscode];
116
117
118
}

@end