Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds getAllInternetPasswordServers method for iOS #473

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions RNKeychainManager/RNKeychainManager.m
Expand Up @@ -301,6 +301,37 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server
return services;
}

-(NSArray<NSString*>*)getAllServersForInternetPasswords
{
NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
(__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
nil];
NSMutableArray<NSString*> *servers = [NSMutableArray<NSString*> new];

[query setObject:(__bridge id)kSecClassInternetPassword forKey:(__bridge id)kSecClass];
NSArray *result = nil;
CFTypeRef resultRef = NULL;
OSStatus osStatus = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef*)&resultRef);
if (osStatus != noErr && osStatus != errSecItemNotFound) {
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
@throw error;
} else if (osStatus != errSecItemNotFound) {
result = (__bridge NSArray*)(resultRef);
if (result != NULL) {
for (id entry in result) {
NSMutableData *serverData = [entry objectForKey:(__bridge NSString *)kSecAttrServer];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NSMutableData *serverData = [entry objectForKey:(__bridge NSString *)kSecAttrServer];
NSString *serverData = [entry objectForKey:(__bridge NSString *)kSecAttrServer];

if (serverData != NULL) {
NSString *server = [[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is not needed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NSString *server = [[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding];

[servers addObject:server];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[servers addObject:server];
[servers addObject:serverData];

}
}
}
}

return servers;
}

#pragma mark - RNKeychain

#if TARGET_OS_IOS
Expand Down Expand Up @@ -594,4 +625,14 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server
}
}

RCT_EXPORT_METHOD(getAllInternetPasswordServers:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
@try {
NSArray *servers = [self getAllServersForInternetPasswords];
return resolve(servers);
} @catch (NSError *nsError) {
return rejectWithError(reject, nsError);
}
}

@end
15 changes: 15 additions & 0 deletions index.js
Expand Up @@ -348,6 +348,21 @@ export function canImplyAuthentication(options?: Options): Promise<boolean> {
return RNKeychainManager.canCheckAuthentication(options);
}

/**
* Gets all `kSecAttrServer` values used with internet credentials for iOS.
* @return {Promise} Resolves to an array of strings
*/
export async function getAllInternetPasswordServers(): Promise<string[]> {
if (Platform.OS !== 'ios') {
return Promise.reject(
new Error(
`getAllInternetPasswordServers() is not supported on ${Platform.OS}`
)
);
}
return RNKeychainManager.getAllInternetPasswordServers();
}

//* ANDROID ONLY */

/**
Expand Down