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

Application password support by iOS #528

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions RNKeychainManager/RNKeychainManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ CFStringRef accessibleValue(NSDictionary *options)
return nil;
}

NSString *applicationPasswordValue(NSDictionary *options)
{
if (options && options[@"applicationPassword"] != nil) {
return options[@"applicationPassword"];
}
return nil;
}

#pragma mark - Proposed functionality - Helpers

#define kAuthenticationType @"authenticationType"
Expand Down Expand Up @@ -350,12 +358,21 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *service = serviceValue(options);
NSDictionary *attributes = attributes = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecAttrAccount: username,
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
};
NSString *applicationPassword = applicationPasswordValue(options);

NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecAttrAccount: username,
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
}];

if (applicationPassword != nil) {
LAContext *context = [LAContext new];
[context setCredential: [applicationPassword dataUsingEncoding: NSUTF8StringEncoding] type: LACredentialTypeApplicationPassword];

attributes[(__bridge NSString *)kSecUseAuthenticationContext] = context;
}

[self deletePasswordsForService:service];

Expand All @@ -368,15 +385,23 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server
{
NSString *service = serviceValue(options);
NSString *authenticationPrompt = authenticationPromptValue(options);

NSDictionary *query = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne,
(__bridge NSString *)kSecUseOperationPrompt: authenticationPrompt
};
NSString *applicationPassword = applicationPasswordValue(options);

NSMutableDictionary *query = [NSMutableDictionary dictionaryWithDictionary: @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne,
(__bridge NSString *)kSecUseOperationPrompt: authenticationPrompt
}];

if (applicationPassword != nil) {
LAContext *context = [LAContext new];
[context setCredential: [applicationPassword dataUsingEncoding: NSUTF8StringEncoding] type: LACredentialTypeApplicationPassword];

query[(__bridge NSString *)kSecUseAuthenticationContext] = context;
}

// Look up service in the keychain
NSDictionary *found = nil;
Expand Down
1 change: 1 addition & 0 deletions typings/react-native-keychain.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ declare module 'react-native-keychain' {
securityLevel?: SECURITY_LEVEL;
storage?: STORAGE_TYPE;
rules?: SECURITY_RULES;
applicationPassword?: string | undefined;
}

function setGenericPassword(
Expand Down