Skip to content

Commit b650f07

Browse files
committed
Create keychain category to handle passcode
Needed to be able to check the passcode from outside the core module, such in one's own app delegate, because the passcode field name constant is defined a static. Also eliminates programmer errors by not requiring to know the key.
1 parent a8f0d7e commit b650f07

File tree

8 files changed

+115
-10
lines changed

8 files changed

+115
-10
lines changed

APCAppCore/APCAppCore.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@
322322
CFFDEDFA1A95734000B25581 /* APCSetupTableViewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = CFFDEDC01A95734000B25581 /* APCSetupTableViewCell.h */; settings = {ATTRIBUTES = (Public, ); }; };
323323
CFFDEDFB1A95734000B25581 /* APCSetupTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = CFFDEDC11A95734000B25581 /* APCSetupTableViewCell.m */; };
324324
CFFDEDFC1A95734000B25581 /* APCSetupTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = CFFDEDC21A95734000B25581 /* APCSetupTableViewCell.xib */; };
325+
EE028FE31AF94B36001C8251 /* APCKeychainStore+Passcode.h in Headers */ = {isa = PBXBuildFile; fileRef = EE028FE11AF94B36001C8251 /* APCKeychainStore+Passcode.h */; };
326+
EE028FE41AF94B36001C8251 /* APCKeychainStore+Passcode.m in Sources */ = {isa = PBXBuildFile; fileRef = EE028FE21AF94B36001C8251 /* APCKeychainStore+Passcode.m */; };
325327
F50738C01A682E12004CF100 /* APCDateRange.h in Headers */ = {isa = PBXBuildFile; fileRef = F50738BE1A682E12004CF100 /* APCDateRange.h */; settings = {ATTRIBUTES = (Public, ); }; };
326328
F50738C11A682E12004CF100 /* APCDateRange.m in Sources */ = {isa = PBXBuildFile; fileRef = F50738BF1A682E12004CF100 /* APCDateRange.m */; };
327329
F5306CCD1A8BE7F600732E60 /* ORKQuestionResult+APCHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = F5306CCB1A8BE7F600732E60 /* ORKQuestionResult+APCHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -1005,6 +1007,8 @@
10051007
CFFDEDC01A95734000B25581 /* APCSetupTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APCSetupTableViewCell.h; sourceTree = "<group>"; };
10061008
CFFDEDC11A95734000B25581 /* APCSetupTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APCSetupTableViewCell.m; sourceTree = "<group>"; };
10071009
CFFDEDC21A95734000B25581 /* APCSetupTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = APCSetupTableViewCell.xib; sourceTree = "<group>"; };
1010+
EE028FE11AF94B36001C8251 /* APCKeychainStore+Passcode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "APCKeychainStore+Passcode.h"; sourceTree = "<group>"; };
1011+
EE028FE21AF94B36001C8251 /* APCKeychainStore+Passcode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "APCKeychainStore+Passcode.m"; sourceTree = "<group>"; };
10081012
F50738BE1A682E12004CF100 /* APCDateRange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APCDateRange.h; sourceTree = "<group>"; };
10091013
F50738BF1A682E12004CF100 /* APCDateRange.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APCDateRange.m; sourceTree = "<group>"; };
10101014
F5179B2919D09128001DCCB7 /* APCAppCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = APCAppCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -2253,6 +2257,8 @@
22532257
087255BF1ABA390500586492 /* APCJSONSerializer.m */,
22542258
F5B947751A73272C0034C522 /* APCKeychainStore.h */,
22552259
F5B947761A73272C0034C522 /* APCKeychainStore.m */,
2260+
EE028FE11AF94B36001C8251 /* APCKeychainStore+Passcode.h */,
2261+
EE028FE21AF94B36001C8251 /* APCKeychainStore+Passcode.m */,
22562262
F5B947771A73272C0034C522 /* APCSegmentedButton.h */,
22572263
F5B947781A73272C0034C522 /* APCSegmentedButton.m */,
22582264
F5B947791A73272C0034C522 /* APCTasksReminderManager.h */,
@@ -2995,6 +3001,7 @@
29953001
A7CFE4B71A8B05F4009A171C /* APCStudyOverviewCollectionViewController.h in Headers */,
29963002
F5F12A931A2F78490015982C /* APCStudyDetailsViewController.h in Headers */,
29973003
F5F12AEC1A2F78490015982C /* APCDashboardMessageTableViewCell.h in Headers */,
3004+
EE028FE31AF94B36001C8251 /* APCKeychainStore+Passcode.h in Headers */,
29983005
F5B947B11A73272C0034C522 /* UIFont+APCAppearance.h in Headers */,
29993006
369E28171A96B7A200D35DFA /* APCMedTrackerPrescriptionColor+Helper.h in Headers */,
30003007
F5F12A731A2F78490015982C /* APCConfirmationView.h in Headers */,
@@ -3564,6 +3571,7 @@
35643571
F5F129F71A2F78490015982C /* APCResult+Bridge.m in Sources */,
35653572
36B7D2561A8D848D0043F968 /* ORKAnswerFormat+Helper.m in Sources */,
35663573
08970F131A8D407600EA46C3 /* APCFoodInsight.m in Sources */,
3574+
EE028FE41AF94B36001C8251 /* APCKeychainStore+Passcode.m in Sources */,
35673575
712AB44F1A9E692400556DA2 /* APCMotionHistoryReporter.m in Sources */,
35683576
F5B947B61A73272C0034C522 /* APCDeviceHardware+APCHelper.m in Sources */,
35693577
369E280D1A96B7A200D35DFA /* APCMedTrackerPrescription+Helper.m in Sources */,

APCAppCore/APCAppCore/APCAppCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ FOUNDATION_EXPORT const unsigned char APCAppCoreVersionString[];
271271
------------------------- */
272272
#import <APCAppCore/APCTableViewItem.h>
273273
#import <APCAppCore/APCGroupedScheduledTask.h>
274-
#import <APCAppCore/APCKeychainStore.h>
274+
#import <APCAppCore/APCKeychainStore+Passcode.h>
275275
#import <APCAppCore/APCPresentAnimator.h>
276276
#import <APCAppCore/APCFadeAnimator.h>
277277
#import <APCAppCore/APCScoring.h>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// APCKeychainStore+Passcode.h
3+
// APCAppCore
4+
//
5+
// Copyright (c) 2015 Boston Children's Hospital, Inc. All rights reserved.
6+
//
7+
// Redistribution and use in source and binary forms, with or without modification,
8+
// are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright notice, this
11+
// list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright notice,
14+
// this list of conditions and the following disclaimer in the documentation and/or
15+
// other materials provided with the distribution.
16+
//
17+
// 3. Neither the name of the copyright holder(s) nor the names of any contributors
18+
// may be used to endorse or promote products derived from this software without
19+
// specific prior written permission. No license is granted to the trademarks of
20+
// the copyright holders even if such marks are included in this software.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
34+
#import "APCKeychainStore.h"
35+
36+
@interface APCKeychainStore (Passcode)
37+
38+
/** Store given passcode to the keychain. */
39+
+ (void)setPasscode:(nonnull NSString *)passcode;
40+
41+
/** Return the user's passcode, if any. */
42+
+ (nullable NSString *)passcode;
43+
44+
@end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// APCKeychainStore+Passcode.m
3+
// APCAppCore
4+
//
5+
// Copyright (c) 2015 Boston Children's Hospital, Inc. All rights reserved.
6+
//
7+
// Redistribution and use in source and binary forms, with or without modification,
8+
// are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright notice, this
11+
// list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright notice,
14+
// this list of conditions and the following disclaimer in the documentation and/or
15+
// other materials provided with the distribution.
16+
//
17+
// 3. Neither the name of the copyright holder(s) nor the names of any contributors
18+
// may be used to endorse or promote products derived from this software without
19+
// specific prior written permission. No license is granted to the trademarks of
20+
// the copyright holders even if such marks are included in this software.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
34+
#import "APCKeychainStore+Passcode.h"
35+
36+
static NSString * const kAPCPasscodeKey = @"APCPasscode";
37+
38+
39+
@implementation APCKeychainStore (Passcode)
40+
41+
+ (void)setPasscode:(nonnull NSString *)passcode
42+
{
43+
NSParameterAssert([passcode length] > 0);
44+
[APCKeychainStore setString:passcode forKey:kAPCPasscodeKey];
45+
}
46+
47+
+ (nullable NSString *)passcode
48+
{
49+
return [APCKeychainStore stringForKey:kAPCPasscodeKey];
50+
}
51+
52+
@end

APCAppCore/APCAppCore/Library/Objects/APCKeychainStore.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
+ (NSString *)stringForKey:(NSString *)key;
3939
+ (BOOL)setString:(NSString *)value forKey:(NSString *)key;
40-
+ (void) removeValueForKey: (NSString*) key;
41-
+ (void) resetKeyChain;
40+
+ (void)removeValueForKey:(NSString *)key;
41+
+ (void)resetKeyChain;
4242

4343
@end

APCAppCore/APCAppCore/UI/Onboarding/APCUserInfoConstants.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,4 @@ static NSString * const kAPCMedicalInfoItemSleepTimeFormat = @"hh:mm a";
103103

104104
static NSString * const kAPCAppStateKey = @"APCAppState";
105105

106-
static NSString * const kAPCPasscodeKey = @"APCPasscode";
107-
108106
#endif

APCAppCore/APCAppCore/UI/TabBarControllers/Profile/APCChangePasscodeViewController.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ - (void) passcodeViewDidFinish:(APCPasscodeView *)passcodeView withCode:(NSStrin
9292
switch (self.entryType) {
9393
case kAPCPasscodeEntryTypeOld:
9494
{
95-
if ([passcodeView.code isEqualToString:[APCKeychainStore stringForKey:kAPCPasscodeKey]]) {
95+
if ([passcodeView.code isEqualToString:[APCKeychainStore passcode]]) {
9696
self.textLabel.text = NSLocalizedString(@"Enter your new passcode", nil);
9797
[passcodeView reset];
9898
[passcodeView becomeFirstResponder];
@@ -143,7 +143,9 @@ - (void) passcodeViewDidFinish:(APCPasscodeView *)passcodeView withCode:(NSStrin
143143

144144
- (void)savePasscode
145145
{
146-
[APCKeychainStore setString:self.passcode forKey:kAPCPasscodeKey];
146+
if (self.passcode) {
147+
[APCKeychainStore setPasscode:self.passcode];
148+
}
147149
self.passcode = @"";
148150
}
149151

APCAppCore/APCAppCore/UI/ViewControllers/APCPasscodeViewController.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
#import <LocalAuthentication/LocalAuthentication.h>
3636
#import "UIAlertController+Helper.h"
3737
#import "APCPasscodeView.h"
38+
#import "APCLog.h"
39+
3840
#import "UIColor+APCAppearance.h"
3941
#import "UIFont+APCAppearance.h"
40-
#import "APCKeychainStore.h"
42+
#import "APCKeychainStore+Passcode.h"
4143
#import "APCUserInfoConstants.h"
4244
#import "UIImage+APCHelper.h"
43-
#import "APCLog.h"
4445

4546
@interface APCPasscodeViewController ()<APCPasscodeViewDelegate>
4647

@@ -127,7 +128,7 @@ - (void)setupAppearance
127128
- (void) passcodeViewDidFinish:(APCPasscodeView *) __unused passcodeView withCode:(NSString *) __unused code {
128129

129130
if (self.passcodeView.code.length > 0) {
130-
if ([self.passcodeView.code isEqualToString:[APCKeychainStore stringForKey:kAPCPasscodeKey]]) {
131+
if ([self.passcodeView.code isEqualToString:[APCKeychainStore passcode]]) {
131132
//Authenticate
132133
if ([self.delegate respondsToSelector:@selector(passcodeViewControllerDidSucceed:)]) {
133134
[self.delegate passcodeViewControllerDidSucceed:self];

0 commit comments

Comments
 (0)