/
AccountManager.m
217 lines (162 loc) · 5.94 KB
/
AccountManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// AccountManager.m
// ASiST
//
// Created by Oliver on 07.09.09.
// Copyright 2009 Drobnik.com. All rights reserved.
//
#import "AccountManager.h"
#import <Security/Security.h>
#import "GenericAccount.h"
#import "GenericAccount+MyAppSales.h"
@interface AccountManager ()
- (void) loadAllGenericAccounts;
@end
@implementation AccountManager
@synthesize accounts;
static const UInt8 kKeychainIdentifier[] = "com.drobnik.asist.KeychainUI\0";
static AccountManager *_sharedInstance = nil;
+ (AccountManager *) sharedAccountManager
{
if (!_sharedInstance)
{
_sharedInstance = [[AccountManager alloc] init];
}
return _sharedInstance;
}
- (id) init
{
if ((self = [super init]))
{
[self loadAllGenericAccounts];
// migrate old version primary account
if ([accounts count]==1)
{
GenericAccount *singleAccount = [accounts objectAtIndex:0];
if ([singleAccount.service isEqualToString:@"HomeDir"])
{
NSLog(@"Migrating old account %@ to new ITC Service", singleAccount.account);
/*
GenericAccount *newAccount = [[GenericAccount alloc] initWithService:@"iTunes Connect" user:singleAccount.account];
newAccount.description = singleAccount.description;
newAccount.label = singleAccount.label;
newAccount.comment = singleAccount.account;
newAccount.password = singleAccount.password;
*/
singleAccount.service = @"iTunes Connect";
singleAccount.description = singleAccount.account;
}
}
}
return self;
}
- (void) dealloc
{
[accounts release];
[super dealloc];
}
/*
// this takes the result from an attributes query, queries for the data (= password) and adds it in readable format
- (NSMutableDictionary *)secItemFormatToDictionary:(NSDictionary *)dictionaryToConvert
{
// The assumption is that this method will be called with a properly populated dictionary
// containing all the right key/value pairs for the UI element.
// Remove the generic attribute which distinguishes this Keychain Item with this
// application.
// Create returning dictionary populated with the attributes.
NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionaryToConvert];
// Add the proper search key and class attribute.
[returnDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[returnDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
// Acquire the password data from the attributes.
NSData *passwordData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)returnDictionary, (CFTypeRef *)&passwordData) == noErr)
{
// Remove the search, class, and identifier key/value, we don't need them anymore.
[returnDictionary removeObjectForKey:(id)kSecReturnData];
// Add the password to the dictionary.
NSString *passwordString = [[[NSString alloc] initWithBytes:[passwordData bytes] length:[passwordData length]
encoding:NSUTF8StringEncoding] autorelease];
[returnDictionary setObject:passwordString forKey:(id)kSecValueData];
}
else
{
// Don't do anything if nothing is found.
// NSAssert(NO, @"Serious error, nothing is found in the Keychain.\n");
// no password in keychain
// Remove the search, class, and identifier key/value, we don't need them anymore.
[returnDictionary removeObjectForKey:(id)kSecReturnData];
}
[passwordData release];
return returnDictionary;
}
*/
// this loads all generic accounts from the keychain
- (void) loadAllGenericAccounts
{
accounts = [[NSMutableArray alloc] init];
NSMutableDictionary *genericPasswordQuery; // A placeholder for a generic Keychain Item query.
genericPasswordQuery = [[[NSMutableDictionary alloc] init] autorelease];
[genericPasswordQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
NSData *keychainType = [NSData dataWithBytes:kKeychainIdentifier length:strlen((const char *)kKeychainIdentifier)];
[genericPasswordQuery setObject:keychainType forKey:(id)kSecAttrGeneric];
// We want all generic accounts and all attributes
[genericPasswordQuery setObject:(id)kSecMatchLimitAll forKey:(id)kSecMatchLimit];
[genericPasswordQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
[genericPasswordQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData]; // so password is also returned
NSDictionary *tempQuery = [NSDictionary dictionaryWithDictionary:genericPasswordQuery];
id result = nil;
if (SecItemCopyMatching((CFDictionaryRef)tempQuery, (CFTypeRef *)&result) == noErr)
{
if ([result isKindOfClass:[NSDictionary class]])
{
NSDictionary *resultAsDictionary = (NSDictionary *)result;
GenericAccount *tmpAcct = [[GenericAccount alloc] initFromKeychainDictionary:resultAsDictionary];
[accounts addObject:tmpAcct];
[tmpAcct release];
}
else if ([result isKindOfClass:[NSArray class]])
{
NSArray *resultsAsArray = (NSArray *)result;
for (NSDictionary *oneAccount in resultsAsArray)
{
GenericAccount *tmpAcct = [[GenericAccount alloc] initFromKeychainDictionary:oneAccount];
[accounts addObject:tmpAcct];
[tmpAcct release];
}
}
}
}
#pragma mark Adding/Removing Accounts
- (GenericAccount *) addAccountForService:(NSString*)aService user:(NSString *)aUser
{
GenericAccount *tmpAccount = [[GenericAccount alloc] initService:aService forUser:aUser];
[accounts addObject:tmpAccount];
return [tmpAccount autorelease];
}
- (void) removeAccount:(GenericAccount *)accountToRemove
{
[accountToRemove removeFromKeychain];
[self.accounts removeObject:accountToRemove];
}
#pragma mark Retrieving Accounts
- (NSArray *)accountsOfType:(NSString *)type
{
NSMutableArray *tmpArray = [NSMutableArray array];
for (GenericAccount *oneAccount in accounts)
{
if ([oneAccount.service isEqualToString:type])
{
[tmpArray addObject:oneAccount];
}
}
if ([tmpArray count])
{
return [NSArray arrayWithArray:tmpArray];
}
else
{
return nil;
}
}
@end