Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keychain groups support #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions VENTouchLock/VENTouchLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ typedef NS_ENUM(NSUInteger, VENTouchLockTouchIDResponse) {
passcodeAttemptLimit:(NSUInteger)attemptLimit
splashViewControllerClass:(Class)splashViewControllerClass;

/**
Set the defaults. This method should be called at launch. Access group is needed for keychain sharing.
@param service The keychain service for which to set and return a passcode
@param account The keychain account for which to set and return a passcode
@param accessGroup The keychain access group for which to set and return a shared passcode
@param splashViewControllerClass The class of the custom splash view controller. This class should be a subclass of VENTouchLockSplashViewController and any of its custom initialization must be in its init function
@param reason The default message displayed on the TouchID prompt
*/
- (void)setKeychainService:(NSString *)service
keychainAccount:(NSString *)account
keychainAccessGroup:(NSString *)accessGroup
touchIDReason:(NSString *)reason
passcodeAttemptLimit:(NSUInteger)attemptLimit
splashViewControllerClass:(Class)splashViewControllerClass;


/**
Returns YES if a passcode exists, and NO otherwise.
*/
Expand Down
45 changes: 34 additions & 11 deletions VENTouchLock/VENTouchLock.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ @interface VENTouchLock ()

@property (copy, nonatomic) NSString *keychainService;
@property (copy, nonatomic) NSString *keychainAccount;
@property (copy, nonatomic) NSString *keychainAccessGroup;
@property (copy, nonatomic) NSString *touchIDReason;
@property (assign, nonatomic) NSUInteger passcodeAttemptLimit;
@property (assign, nonatomic) Class splashViewControllerClass;
Expand Down Expand Up @@ -62,6 +63,20 @@ - (void)setKeychainService:(NSString *)service
self.splashViewControllerClass = splashViewControllerClass;
}

- (void)setKeychainService:(NSString *)service
keychainAccount:(NSString *)account
keychainAccessGroup:(NSString *)accessGroup
touchIDReason:(NSString *)reason
passcodeAttemptLimit:(NSUInteger)attemptLimit
splashViewControllerClass:(Class)splashViewControllerClass {

[self setKeychainService:service
keychainAccount:account
touchIDReason:reason
passcodeAttemptLimit:attemptLimit
splashViewControllerClass:splashViewControllerClass];
self.keychainAccessGroup = accessGroup;
}

#pragma mark - Keychain Methods

Expand All @@ -70,11 +85,14 @@ - (BOOL)isPasscodeSet
return !![self currentPasscode];
}

- (NSString *)currentPasscode
{
NSString *service = self.keychainService;
NSString *account = self.keychainAccount;
return [SSKeychain passwordForService:service account:account];
- (NSString *)currentPasscode {

SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = self.keychainService;
query.account = self.keychainAccount;
query.accessGroup = self.keychainAccessGroup;
[query fetch:nil];
return query.password;
}

- (BOOL)isPasscodeValid:(NSString *)passcode
Expand All @@ -84,9 +102,12 @@ - (BOOL)isPasscodeValid:(NSString *)passcode

- (void)setPasscode:(NSString *)passcode
{
NSString *service = self.keychainService;
NSString *account = self.keychainAccount;
[SSKeychain setPassword:passcode forService:service account:account];
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = self.keychainService;
query.account = self.keychainAccount;
query.accessGroup = self.keychainAccessGroup;
query.password = passcode;
[query save:nil];
}

- (void)deletePasscode
Expand All @@ -95,9 +116,11 @@ - (void)deletePasscode
[VENTouchLockEnterPasscodeViewController resetPasscodeAttemptHistory];
[[NSUserDefaults standardUserDefaults] synchronize];

NSString *service = self.keychainService;
NSString *account = self.keychainAccount;
[SSKeychain deletePasswordForService:service account:account];
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
query.service = self.keychainService;
query.account = self.keychainAccount;
query.accessGroup = self.keychainAccessGroup;
[query deleteItem:nil];
}


Expand Down
62 changes: 61 additions & 1 deletion VENTouchLockTests/VENTouchLockSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,64 @@

});

SpecEnd
SpecEnd

SpecBegin(VENTouchLockAccessGroup)

beforeAll(^{
[[VENTouchLock sharedInstance] setKeychainService:@"keychainService"
keychainAccount:@"keychainAccount"
keychainAccessGroup:@"keychainAccessGroupAccount"
touchIDReason:@"touchIDReason"
passcodeAttemptLimit:0
splashViewControllerClass:NULL];
});

beforeEach(^{
[[VENTouchLock sharedInstance] deletePasscode];
});

describe(@"setPasscode:", ^{

it(@"should register a passcode with VENTouchLock", ^{
VENTouchLock *touchLock = [VENTouchLock sharedInstance];
expect([touchLock isPasscodeSet]).to.equal(NO);
expect([touchLock currentPasscode]).to.beNil();

[[VENTouchLock sharedInstance] setPasscode:@"testPasscode"];

expect([touchLock isPasscodeSet]).to.equal(YES);
expect([touchLock currentPasscode]).to.equal(@"testPasscode");
});

});

describe(@"isPasscodeValid", ^{

it(@"should return YES if the parameter sent is equal to the set passcode and NO otherwise", ^{
VENTouchLock *touchLock = [VENTouchLock sharedInstance];
[touchLock setPasscode:@"testPasscode"];
expect([touchLock isPasscodeValid:@"testPasscode"]).to.equal(YES);
expect([touchLock isPasscodeValid:@"wrongPasscode"]).to.equal(NO);
});

});

describe(@"deletePasscode", ^{

it(@"should register a passcode with VENTouchLock", ^{
VENTouchLock *touchLock = [VENTouchLock sharedInstance];

[[VENTouchLock sharedInstance] setPasscode:@"testPasscode"];
expect([touchLock isPasscodeSet]).to.equal(YES);
expect([touchLock currentPasscode]).to.equal(@"testPasscode");

[touchLock deletePasscode];

expect([touchLock isPasscodeSet]).to.equal(NO);
expect([touchLock currentPasscode]).to.beNil();
});

});

SpecEnd