Skip to content

Commit 908f5f1

Browse files
committed
accountsForService 里面的账号密码还需要在取一下
1 parent 4017fac commit 908f5f1

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

Coding_iOS/Ease_2FA/Controllers/OTPListViewController.m

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,43 @@ @interface OTPListViewController ()<UITableViewDataSource, UITableViewDelegate>
3232

3333
@implementation OTPListViewController
3434

35+
+ (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account{
36+
if (serviceName.length <= 0 || account.length <= 0) {
37+
return nil;
38+
}
39+
SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
40+
query.service = serviceName;
41+
query.account = account;
42+
[query fetch:nil];
43+
return query.passwordData;
44+
}
45+
46+
+ (NSMutableArray *)loadKeychainAuthURLs{
47+
NSArray *otpAccountList = [SSKeychain accountsForService:kOTPService];
48+
NSMutableArray *authURLs = [NSMutableArray arrayWithCapacity:otpAccountList.count];
49+
for (NSDictionary *otpAccount in otpAccountList) {
50+
NSData *passwordData = [self passwordDataForService:kOTPService account:otpAccount[(__bridge id)kSecAttrAccount]];
51+
if (passwordData) {
52+
NSMutableDictionary *tempDict = [otpAccount mutableCopy];
53+
tempDict[(__bridge id)kSecValueData] = passwordData;
54+
OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary:tempDict];
55+
if (authURL) {
56+
[authURLs addObject:authURL];
57+
}
58+
}
59+
}
60+
return authURLs;
61+
}
62+
3563
+(NSString *)otpCodeWithGK:(NSString *)global_key{
3664
NSString *otpCode = nil;
3765
if (global_key.length > 0) {
38-
NSArray *otpAccountDictList = [SSKeychain accountsForService:kOTPService];
39-
for (NSDictionary *obj in otpAccountDictList) {
40-
NSString *name = obj[(__bridge id)kSecAttrAccount];
41-
name = [[name componentsSeparatedByString:@"@"] firstObject];
42-
if ([name isEqualToString:global_key]) {
43-
OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary:obj];
66+
NSMutableArray *authURLs = [self loadKeychainAuthURLs];
67+
for (OTPAuthURL *authURL in authURLs) {
68+
NSString *cur_issure = authURL.issuer;
69+
NSString *cur_global_key = [[authURL.name componentsSeparatedByString:@"@"] firstObject];
70+
if ([cur_issure isEqualToString:@"Coding"] &&
71+
[cur_global_key isEqualToString:global_key]) {
4472
otpCode = authURL.otpCode;
4573
break;
4674
}
@@ -52,18 +80,7 @@ +(NSString *)otpCodeWithGK:(NSString *)global_key{
5280
- (void)viewDidLoad{
5381
[super viewDidLoad];
5482
self.title = @"身份验证器";
55-
[self loadKeychainArray];
56-
}
57-
58-
- (void)loadKeychainArray{
59-
NSArray *otpAccountDictList = [SSKeychain accountsForService:kOTPService];
60-
self.authURLs = [NSMutableArray arrayWithCapacity:[otpAccountDictList count]];
61-
for (NSDictionary *otpAccountDict in otpAccountDictList) {
62-
OTPAuthURL *authURL = [OTPAuthURL ease_authURLWithKeychainDictionary:otpAccountDict];
63-
if (authURL) {
64-
[self.authURLs addObject:authURL];
65-
}
66-
}
83+
self.authURLs = [[self class] loadKeychainAuthURLs];
6784
}
6885

6986
- (void)viewWillAppear:(BOOL)animated{

Coding_iOS/Ease_2FA/Views/Cell/OTPTableViewCell.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ - (void)updateUI{
8383
}];
8484
}
8585
_issuerLabel.text = _authURL.issuer;
86-
_passwordLabel.text = _authURL.otpCode;
86+
_passwordLabel.text = _authURL.otpCode.length < 6? _authURL.otpCode: [_authURL.otpCode stringByReplacingCharactersInRange:NSMakeRange(3, 0) withString:@" "];
8787
_nameLabel.text = _authURL.name;
8888
}
8989
- (void)otpAuthURLDidGenerateNewOTP:(NSNotification *)notification{

0 commit comments

Comments
 (0)