File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed
Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -323,6 +323,14 @@ NS_ASSUME_NONNULL_BEGIN
323323 */
324324- (BOOL )hasValueForKey : (NSString *)key ;
325325
326+
327+ /* *
328+ * Fetches an array of NSString containing all the keys used in the keychain
329+ *
330+ * @return a NSString array with all keys from the keychain.
331+ */
332+ - (nonnull NSArray *)keys ;
333+
326334// /---------------------------------------------------
327335// / @name Create helper methods
328336// /---------------------------------------------------
Original file line number Diff line number Diff line change @@ -120,6 +120,25 @@ - (BOOL)hasValueForKey:(NSString *)key {
120120 return status == errSecSuccess;
121121}
122122
123+ - (nonnull NSArray *)keys {
124+ NSMutableArray *keys = [NSMutableArray array ];
125+ NSDictionary *query = [self queryFindAll ];
126+ CFArrayRef result = nil ;
127+ OSStatus status = SecItemCopyMatching ((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);
128+ if (status == errSecSuccess) {
129+ NSArray *items = [NSArray arrayWithArray: (__bridge NSArray *)result];
130+ CFBridgingRelease (result);
131+ for (NSDictionary *item in items) {
132+ id secAccount = item[(__bridge id )kSecAttrAccount ];
133+ if ([secAccount isKindOfClass: [NSString class ]]) {
134+ NSString *key = (NSString *)secAccount;
135+ [keys addObject: key];
136+ }
137+ }
138+ }
139+ return keys;
140+ }
141+
123142- (BOOL )setString : (NSString *)string forKey : (NSString *)key {
124143 return [self setString: string forKey: key promptMessage: nil ];
125144}
Original file line number Diff line number Diff line change @@ -168,6 +168,48 @@ class A0SimpleKeychainSpec: QuickSpec {
168168 expect ( keychain. data ( forKey: key) ) . notTo ( beNil ( ) )
169169 }
170170 }
171+
172+ describe ( " retrieving keys " ) {
173+
174+ var keys = [ String] ( )
175+
176+ beforeEach {
177+
178+ keychain. clearAll ( )
179+
180+ keychain = A0SimpleKeychain ( service: kKeychainService)
181+ keys. append ( UUID ( ) . uuidString)
182+ keys. append ( UUID ( ) . uuidString)
183+ keys. append ( UUID ( ) . uuidString)
184+ for (i, key) in keys. enumerated ( ) {
185+ keychain. setString ( " value \( i) " , forKey: key)
186+ }
187+ }
188+
189+ afterEach {
190+ keychain. clearAll ( )
191+ }
192+
193+ it ( " should return all the keys " ) {
194+ expect ( keychain. keys ( ) as? [ String ] ) . to ( equal ( keys) )
195+ }
196+
197+ it ( " should clear all " ) {
198+
199+ for key in keys {
200+ expect ( keychain. data ( forKey: key) ) . notTo ( beNil ( ) )
201+ }
202+ expect ( keychain. keys ( ) . count) . to ( equal ( keys. count) )
203+
204+ keychain. clearAll ( )
205+
206+ for key in keys {
207+ expect ( keychain. data ( forKey: key) ) . to ( beNil ( ) )
208+ }
209+ expect ( keychain. keys ( ) . count) . to ( equal ( 0 ) )
210+ }
211+ }
212+
171213
172214 describe ( " generate key pair " ) {
173215
You can’t perform that action at this time.
0 commit comments