Skip to content

Commit 87a8a2a

Browse files
committed
ease_SecAttrAccount
1 parent d24deeb commit 87a8a2a

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

Coding_iOS/Ease_2FA/Controllers/OTPListViewController.m

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "OTPTableViewCell.h"
1212

1313
#import "OTPAuthURL.h"
14+
#import <SSKeychain/SSKeychain.h>
1415

1516
static NSString *const kOTPKeychainEntriesArray = @"OTPKeychainEntries";
1617

@@ -38,24 +39,16 @@ - (void)viewDidLoad{
3839
}
3940

4041
- (void)loadKeychainArray{
41-
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
42-
NSArray *savedKeychainReferences = [ud arrayForKey:kOTPKeychainEntriesArray];
43-
self.authURLs = [NSMutableArray arrayWithCapacity:[savedKeychainReferences count]];
44-
for (NSData *keychainRef in savedKeychainReferences) {
45-
OTPAuthURL *authURL = [OTPAuthURL authURLWithKeychainItemRef:keychainRef];
42+
NSArray *otpAccountDictList = [SSKeychain accountsForService:kOTPService];
43+
self.authURLs = [NSMutableArray arrayWithCapacity:[otpAccountDictList count]];
44+
for (NSDictionary *otpAccountDict in otpAccountDictList) {
45+
OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary:otpAccountDict];
4646
if (authURL) {
4747
[self.authURLs addObject:authURL];
4848
}
4949
}
5050
}
5151

52-
- (void)saveKeychainArray {
53-
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
54-
NSArray *keychainReferences = [self valueForKeyPath:@"authURLs.keychainItemRef"];
55-
[ud setObject:keychainReferences forKey:kOTPKeychainEntriesArray];
56-
[ud synchronize];
57-
}
58-
5952
- (void)viewWillAppear:(BOOL)animated{
6053
[super viewWillAppear:animated];
6154
[self configUI];
@@ -141,7 +134,6 @@ - (void)beginButtonClicked:(id)sender{
141134
- (void)addOneAuthURL:(OTPAuthURL *)authURL{
142135
[authURL saveToKeychain];
143136
[self.authURLs addObject:authURL];
144-
[self saveKeychainArray];
145137
[self configUI];
146138
}
147139

@@ -197,7 +189,6 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
197189
OTPAuthURL *authURL = self.authURLs[indexPath.section];
198190
[self.authURLs removeObject:authURL];
199191
[authURL removeFromKeychain];
200-
[self saveKeychainArray];
201192
[self configUI];
202193
}
203194
}

Coding_iOS/Ease_2FA/GoogleOTP/OTPAuthURL.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//
1818

1919
#import <Foundation/Foundation.h>
20+
static NSString *const kOTPService = @"com.google.otp.authentication";
2021

2122
@class OTPGenerator;
2223

@@ -29,7 +30,7 @@
2930
@interface OTPAuthURL : NSObject
3031

3132
// |name| is an arbitrary UTF8 text string extracted from the url path.
32-
@property (nonatomic, copy) NSString *name, *issuer;
33+
@property (nonatomic, copy) NSString *name, *issuer, *ease_SecAttrAccount;
3334
@property (nonatomic, copy, readonly) NSString *otpCode;
3435
@property (nonatomic, copy, readonly) NSString *checkCode;
3536
@property (nonatomic, strong, readonly) NSData *keychainItemRef;
@@ -38,6 +39,8 @@
3839
secret:(NSData *)secret;
3940
+ (OTPAuthURL *)authURLWithKeychainItemRef:(NSData *)keychainItemRef;
4041

42+
+ (OTPAuthURL *)ease_authURLWithKeychainDictionary:(NSDictionary *)dict;//
43+
4144
// Returns a reconstructed NSURL object representing the current state of the
4245
// |generator|.
4346
- (NSURL *)url;

Coding_iOS/Ease_2FA/GoogleOTP/OTPAuthURL.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
#import "HOTPGenerator.h"
2929
#import "TOTPGenerator.h"
3030

31+
#import <SSKeychain/SSKeychain.h>
32+
3133
static NSString *const kOTPAuthScheme = @"otpauth";
3234
static NSString *const kTOTPAuthScheme = @"totp";
33-
static NSString *const kOTPService = @"com.google.otp.authentication";
3435
// These are keys in the otpauth:// query string.
3536
static NSString *const kQueryAlgorithmKey = @"algorithm";
3637
static NSString *const kQuerySecretKey = @"secret";
@@ -165,6 +166,17 @@ + (OTPAuthURL *)authURLWithKeychainItemRef:(NSData *)data {
165166
return authURL;
166167
}
167168

169+
+ (OTPAuthURL *)ease_authURLWithKeychainDictionary:(NSDictionary *)dict{
170+
OTPAuthURL *authURL = [self authURLWithKeychainDictionary:dict];
171+
if (authURL) {
172+
NSString *secAttrAccount = dict[(__bridge id)kSecAttrAccount];
173+
if (secAttrAccount.length > 0) {
174+
authURL.ease_SecAttrAccount = secAttrAccount;
175+
}
176+
}
177+
return authURL;
178+
}
179+
168180
+ (OTPAuthURL *)authURLWithKeychainDictionary:(NSDictionary *)dict {
169181
NSData *urlData = dict[(__bridge id)kSecAttrGeneric];
170182
NSData *secretData = dict[(__bridge id)kSecValueData];
@@ -258,6 +270,10 @@ - (BOOL)saveToKeychain {
258270
}
259271

260272
- (BOOL)removeFromKeychain {
273+
if (self.ease_SecAttrAccount.length > 0) {
274+
return [SSKeychain deletePasswordForService:kOTPService account:self.ease_SecAttrAccount];
275+
}
276+
261277
if (![self isInKeychain]) {
262278
return NO;
263279
}

0 commit comments

Comments
 (0)