2015-05-20 16:23:04 +00:00
|
|
|
//
|
|
|
|
// RNKeychainManager.m
|
|
|
|
// RNKeychainManager
|
|
|
|
//
|
|
|
|
// Created by Joel Arvidsson on 2015-05-20.
|
|
|
|
// Copyright (c) 2015 Joel Arvidsson. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Security/Security.h>
|
|
|
|
#import "RNKeychainManager.h"
|
2017-01-10 19:35:04 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-05-20 16:23:04 +00:00
|
|
|
|
2018-02-25 14:55:17 +00:00
|
|
|
#import <LocalAuthentication/LAContext.h>
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2015-05-20 16:23:04 +00:00
|
|
|
@implementation RNKeychainManager
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
2015-05-29 16:02:15 +00:00
|
|
|
// Messages from the comments in <Security/SecBase.h>
|
|
|
|
NSString *messageForError(NSError *error)
|
|
|
|
{
|
|
|
|
switch (error.code) {
|
|
|
|
case errSecUnimplemented:
|
|
|
|
return @"Function or operation not implemented.";
|
|
|
|
|
|
|
|
case errSecIO:
|
|
|
|
return @"I/O error.";
|
|
|
|
|
|
|
|
case errSecOpWr:
|
|
|
|
return @"File already open with with write permission.";
|
|
|
|
|
|
|
|
case errSecParam:
|
|
|
|
return @"One or more parameters passed to a function where not valid.";
|
|
|
|
|
|
|
|
case errSecAllocate:
|
|
|
|
return @"Failed to allocate memory.";
|
|
|
|
|
|
|
|
case errSecUserCanceled:
|
|
|
|
return @"User canceled the operation.";
|
|
|
|
|
|
|
|
case errSecBadReq:
|
|
|
|
return @"Bad parameter or invalid state for operation.";
|
|
|
|
|
|
|
|
case errSecNotAvailable:
|
|
|
|
return @"No keychain is available. You may need to restart your computer.";
|
|
|
|
|
|
|
|
case errSecDuplicateItem:
|
|
|
|
return @"The specified item already exists in the keychain.";
|
|
|
|
|
|
|
|
case errSecItemNotFound:
|
|
|
|
return @"The specified item could not be found in the keychain.";
|
|
|
|
|
|
|
|
case errSecInteractionNotAllowed:
|
|
|
|
return @"User interaction is not allowed.";
|
|
|
|
|
|
|
|
case errSecDecode:
|
|
|
|
return @"Unable to decode the provided data.";
|
|
|
|
|
|
|
|
case errSecAuthFailed:
|
|
|
|
return @"The user name or passphrase you entered is not correct.";
|
|
|
|
|
2018-02-25 22:42:53 +00:00
|
|
|
case errSecMissingEntitlement:
|
|
|
|
return @"Internal error when a required entitlement isn't present.";
|
|
|
|
|
2015-05-29 16:02:15 +00:00
|
|
|
default:
|
|
|
|
return error.localizedDescription;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
NSString *codeForError(NSError *error)
|
2015-05-28 17:08:18 +00:00
|
|
|
{
|
2017-01-10 22:00:51 +00:00
|
|
|
return [NSString stringWithFormat:@"%li", (long)error.code];
|
2015-05-28 17:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
void rejectWithError(RCTPromiseRejectBlock reject, NSError *error)
|
|
|
|
{
|
|
|
|
return reject(codeForError(error), messageForError(error), nil);
|
|
|
|
}
|
2015-05-20 16:23:04 +00:00
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
CFStringRef accessibleValue(NSDictionary *options)
|
2017-02-10 08:34:58 +00:00
|
|
|
{
|
2017-02-10 09:58:39 +00:00
|
|
|
if (options && options[@"accessible"] != nil) {
|
|
|
|
NSDictionary *keyMap = @{
|
|
|
|
@"AccessibleWhenUnlocked": (__bridge NSString *)kSecAttrAccessibleWhenUnlocked,
|
|
|
|
@"AccessibleAfterFirstUnlock": (__bridge NSString *)kSecAttrAccessibleAfterFirstUnlock,
|
|
|
|
@"AccessibleAlways": (__bridge NSString *)kSecAttrAccessibleAlways,
|
|
|
|
@"AccessibleWhenPasscodeSetThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
|
|
|
|
@"AccessibleWhenUnlockedThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
|
|
|
@"AccessibleAfterFirstUnlockThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
|
|
|
|
@"AccessibleAlwaysThisDeviceOnly": (__bridge NSString *)kSecAttrAccessibleAlwaysThisDeviceOnly
|
|
|
|
};
|
|
|
|
NSString *result = keyMap[options[@"accessible"]];
|
|
|
|
if (result) {
|
|
|
|
return (__bridge CFStringRef)result;
|
2017-02-10 08:34:58 +00:00
|
|
|
}
|
2017-02-10 09:58:39 +00:00
|
|
|
}
|
|
|
|
return kSecAttrAccessibleAfterFirstUnlock;
|
2017-02-10 08:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
NSString *serviceValue(NSDictionary *options)
|
|
|
|
{
|
|
|
|
if (options && options[@"service"] != nil) {
|
|
|
|
return options[@"service"];
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
2017-02-10 09:58:39 +00:00
|
|
|
return [[NSBundle mainBundle] bundleIdentifier];
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
NSString *accessGroupValue(NSDictionary *options)
|
|
|
|
{
|
|
|
|
if (options && options[@"accessGroup"] != nil) {
|
|
|
|
return options[@"accessGroup"];
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:55:17 +00:00
|
|
|
#pragma mark - Proposed functionality - Helpers
|
|
|
|
|
|
|
|
#define kAuthenticationType @"authenticationType"
|
2018-02-25 22:42:15 +00:00
|
|
|
#define kAuthenticationTypeBiometrics @"AuthenticationWithBiometrics"
|
2018-02-25 14:55:17 +00:00
|
|
|
|
|
|
|
#define kAccessControlType @"accessControl"
|
|
|
|
#define kAccessControlUserPresence @"UserPresence"
|
2018-02-25 23:14:48 +00:00
|
|
|
#define kAccessControlBiometryAny @"BiometryAny"
|
|
|
|
#define kAccessControlBiometryCurrentSet @"BiometryCurrentSet"
|
2018-02-25 14:55:17 +00:00
|
|
|
#define kAccessControlDevicePasscode @"DevicePasscode"
|
2018-02-26 09:49:33 +00:00
|
|
|
#define kAccessControlApplicationPassword @"ApplicationPassword"
|
2018-02-25 23:14:48 +00:00
|
|
|
#define kAccessControlBiometryAnyOrDevicePasscode @"BiometryAnyOrDevicePasscode"
|
|
|
|
#define kAccessControlBiometryCurrentSetOrDevicePasscode @"BiometryCurrentSetOrDevicePasscode"
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-25 16:21:12 +00:00
|
|
|
#define kBiometryTypeTouchID @"TouchID"
|
|
|
|
#define kBiometryTypeFaceID @"FaceID"
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-25 22:42:15 +00:00
|
|
|
#define kAuthenticationPromptMessage @"authenticationPrompt"
|
2018-02-25 14:55:17 +00:00
|
|
|
|
|
|
|
LAPolicy authPolicy(NSDictionary *options)
|
|
|
|
{
|
2018-02-25 16:21:12 +00:00
|
|
|
if (options && options[kAuthenticationType]) {
|
2018-02-25 22:42:15 +00:00
|
|
|
if ([ options[kAuthenticationType] isEqualToString:kAuthenticationTypeBiometrics ]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return LAPolicyDeviceOwnerAuthenticationWithBiometrics;
|
2018-02-25 14:55:17 +00:00
|
|
|
}
|
2018-02-25 16:21:12 +00:00
|
|
|
}
|
|
|
|
return LAPolicyDeviceOwnerAuthentication;
|
2018-02-25 14:55:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
SecAccessControlCreateFlags accessControlValue(NSDictionary *options)
|
2018-02-25 14:55:17 +00:00
|
|
|
{
|
2018-02-26 11:31:06 +00:00
|
|
|
if (options && options[kAccessControlType] && [options[kAccessControlType] isKindOfClass:[NSString class]]) {
|
|
|
|
if ([options[kAccessControlType] isEqualToString: kAccessControlUserPresence]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlUserPresence;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlBiometryAny]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlTouchIDAny;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlBiometryCurrentSet]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlTouchIDCurrentSet;
|
2018-02-25 14:55:17 +00:00
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlDevicePasscode]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlDevicePasscode;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlBiometryAnyOrDevicePasscode]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlTouchIDAny|kSecAccessControlOr|kSecAccessControlDevicePasscode;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlBiometryCurrentSetOrDevicePasscode]) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return kSecAccessControlTouchIDCurrentSet|kSecAccessControlOr|kSecAccessControlDevicePasscode;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
else if ([options[kAccessControlType] isEqualToString: kAccessControlApplicationPassword]) {
|
2018-02-26 09:49:33 +00:00
|
|
|
return kSecAccessControlApplicationPassword;
|
|
|
|
}
|
2018-02-25 16:21:12 +00:00
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
return 0;
|
2018-02-25 14:55:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
- (void)insertKeychainEntry:(NSDictionary *)attributes withOptions:(NSDictionary * __nullable)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
|
2018-02-25 22:53:42 +00:00
|
|
|
{
|
2018-02-26 11:31:06 +00:00
|
|
|
NSString *accessGroup = accessGroupValue(options);
|
|
|
|
CFStringRef accessible = accessibleValue(options);
|
|
|
|
SecAccessControlCreateFlags accessControl = accessControlValue(options);
|
|
|
|
|
|
|
|
NSMutableDictionary *mAttributes = attributes.mutableCopy;
|
|
|
|
|
|
|
|
if (accessControl) {
|
|
|
|
NSError *aerr = nil;
|
|
|
|
BOOL canAuthenticate = [[LAContext new] canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&aerr];
|
|
|
|
if (aerr || !canAuthenticate) {
|
|
|
|
return rejectWithError(reject, aerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFErrorRef error = NULL;
|
|
|
|
SecAccessControlRef sacRef = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
|
|
|
|
accessible,
|
|
|
|
accessControl,
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return rejectWithError(reject, aerr);
|
|
|
|
}
|
|
|
|
mAttributes[(__bridge NSString *)kSecAttrAccessControl] = (__bridge id)sacRef;
|
|
|
|
} else {
|
|
|
|
mAttributes[(__bridge NSString *)kSecAttrAccessible] = (__bridge id)accessible;
|
|
|
|
}
|
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
if (accessGroup != nil) {
|
|
|
|
mAttributes[(__bridge NSString *)kSecAttrAccessGroup] = accessGroup;
|
|
|
|
}
|
2018-02-26 11:31:06 +00:00
|
|
|
|
|
|
|
attributes = [NSDictionary dictionaryWithDictionary:mAttributes];
|
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
OSStatus osStatus = SecItemAdd((__bridge CFDictionaryRef) attributes, NULL);
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (osStatus != noErr && osStatus != errSecItemNotFound) {
|
|
|
|
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
|
|
|
|
return rejectWithError(reject, error);
|
|
|
|
} else {
|
|
|
|
return resolve(@(YES));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OSStatus)deletePasswordsForService:(NSString *)service
|
|
|
|
{
|
|
|
|
NSDictionary *query = @{
|
|
|
|
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
|
|
|
|
(__bridge NSString *)kSecAttrService: service,
|
|
|
|
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
|
|
|
|
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanFalse
|
|
|
|
};
|
|
|
|
|
|
|
|
return SecItemDelete((__bridge CFDictionaryRef) query);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OSStatus)deleteCredentialsForServer:(NSString *)server
|
|
|
|
{
|
|
|
|
NSDictionary *query = @{
|
|
|
|
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
|
|
|
|
(__bridge NSString *)kSecAttrServer: server,
|
|
|
|
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
|
|
|
|
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanFalse
|
|
|
|
};
|
|
|
|
|
|
|
|
return SecItemDelete((__bridge CFDictionaryRef) query);
|
|
|
|
}
|
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
#pragma mark - RNKeychain
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-25 19:21:01 +00:00
|
|
|
#if TARGET_OS_IOS
|
2018-02-25 14:55:17 +00:00
|
|
|
RCT_EXPORT_METHOD(canCheckAuthentication:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
2018-02-25 16:21:12 +00:00
|
|
|
LAPolicy policyToEvaluate = authPolicy(options);
|
|
|
|
|
|
|
|
NSError *aerr = nil;
|
2018-02-25 22:53:42 +00:00
|
|
|
BOOL canBeProtected = [[LAContext new] canEvaluatePolicy:policyToEvaluate error:&aerr];
|
2018-02-25 16:21:12 +00:00
|
|
|
|
|
|
|
if (aerr || !canBeProtected) {
|
|
|
|
return resolve(@(NO));
|
|
|
|
} else {
|
|
|
|
return resolve(@(YES));
|
|
|
|
}
|
2018-02-25 14:55:17 +00:00
|
|
|
}
|
2018-02-25 19:21:01 +00:00
|
|
|
#endif
|
2018-02-25 14:55:17 +00:00
|
|
|
|
2018-02-25 19:21:01 +00:00
|
|
|
#if TARGET_OS_IOS
|
2018-02-25 16:05:33 +00:00
|
|
|
RCT_EXPORT_METHOD(getSupportedBiometryType:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
NSError *aerr = nil;
|
|
|
|
LAContext *context = [LAContext new];
|
|
|
|
BOOL canBeProtected = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&aerr];
|
|
|
|
|
|
|
|
if (!aerr && canBeProtected) {
|
|
|
|
if (@available(iOS 11, *)) {
|
|
|
|
if (context.biometryType == LABiometryTypeFaceID) {
|
2018-02-25 16:21:12 +00:00
|
|
|
return resolve(kBiometryTypeFaceID);
|
2018-02-25 16:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-25 16:21:12 +00:00
|
|
|
return resolve(kBiometryTypeTouchID);
|
2018-02-25 16:05:33 +00:00
|
|
|
}
|
2018-02-25 19:21:01 +00:00
|
|
|
|
2018-02-25 16:05:33 +00:00
|
|
|
return resolve([NSNull null]);
|
|
|
|
}
|
2018-02-25 19:21:01 +00:00
|
|
|
#endif
|
2018-02-25 16:05:33 +00:00
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
RCT_EXPORT_METHOD(setGenericPasswordForOptions:(NSDictionary *)options withUsername:(NSString *)username withPassword:(NSString *)password resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
2018-02-25 14:55:17 +00:00
|
|
|
{
|
2018-02-25 18:20:49 +00:00
|
|
|
NSString *service = serviceValue(options);
|
2018-02-26 11:31:06 +00:00
|
|
|
NSDictionary *attributes = attributes = @{
|
2018-02-25 22:53:42 +00:00
|
|
|
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
|
|
|
|
(__bridge NSString *)kSecAttrService: service,
|
|
|
|
(__bridge NSString *)kSecAttrAccount: username,
|
2018-02-26 11:31:06 +00:00
|
|
|
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
|
2018-02-25 22:53:42 +00:00
|
|
|
};
|
2018-02-25 16:21:12 +00:00
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
[self deletePasswordsForService:service];
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
[self insertKeychainEntry:attributes withOptions:options resolver:resolve rejecter:reject];
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(getGenericPasswordForOptions:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
NSString *service = serviceValue(options);
|
2018-02-26 11:31:06 +00:00
|
|
|
NSString *authenticationPrompt = @"Authenticate to retrieve secret";
|
|
|
|
if (options && options[kAuthenticationPromptMessage]) {
|
|
|
|
authenticationPrompt = options[kAuthenticationPromptMessage];
|
|
|
|
}
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
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,
|
2018-02-26 11:31:06 +00:00
|
|
|
(__bridge NSString *)kSecUseOperationPrompt: authenticationPrompt
|
2018-02-25 22:53:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Look up service in the keychain
|
|
|
|
NSDictionary *found = nil;
|
2015-05-29 16:25:56 +00:00
|
|
|
CFTypeRef foundTypeRef = NULL;
|
2018-02-25 22:53:42 +00:00
|
|
|
OSStatus osStatus = SecItemCopyMatching((__bridge CFDictionaryRef) query, (CFTypeRef*)&foundTypeRef);
|
2015-05-29 16:25:56 +00:00
|
|
|
|
|
|
|
if (osStatus != noErr && osStatus != errSecItemNotFound) {
|
|
|
|
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
|
2017-01-10 22:00:51 +00:00
|
|
|
return rejectWithError(reject, error);
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
found = (__bridge NSDictionary*)(foundTypeRef);
|
|
|
|
if (!found) {
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@(NO));
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Found
|
2018-02-25 22:53:42 +00:00
|
|
|
NSString *username = (NSString *) [found objectForKey:(__bridge id)(kSecAttrAccount)];
|
|
|
|
NSString *password = [[NSString alloc] initWithData:[found objectForKey:(__bridge id)(kSecValueData)] encoding:NSUTF8StringEncoding];
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@{
|
|
|
|
@"service": service,
|
|
|
|
@"username": username,
|
|
|
|
@"password": password
|
|
|
|
});
|
2015-05-29 16:25:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(resetGenericPasswordForOptions:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
NSString *service = serviceValue(options);
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2018-02-25 22:53:42 +00:00
|
|
|
OSStatus osStatus = [self deletePasswordsForService:service];
|
2018-02-25 18:16:07 +00:00
|
|
|
|
2015-05-29 16:25:56 +00:00
|
|
|
if (osStatus != noErr && osStatus != errSecItemNotFound) {
|
|
|
|
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
|
2017-01-10 22:00:51 +00:00
|
|
|
return rejectWithError(reject, error);
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@(YES));
|
2015-05-29 16:25:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(setInternetCredentialsForServer:(NSString *)server withUsername:(NSString*)username withPassword:(NSString*)password withOptions:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
2018-02-25 22:53:42 +00:00
|
|
|
[self deleteCredentialsForServer:server];
|
|
|
|
|
|
|
|
NSDictionary *attributes = @{
|
|
|
|
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
|
|
|
|
(__bridge NSString *)kSecAttrServer: server,
|
|
|
|
(__bridge NSString *)kSecAttrAccount: username,
|
2018-02-26 11:31:06 +00:00
|
|
|
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
|
2018-02-25 22:53:42 +00:00
|
|
|
};
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2018-02-26 11:31:06 +00:00
|
|
|
[self insertKeychainEntry:attributes withOptions:options resolver:resolve rejecter:reject];
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(getInternetCredentialsForServer:(NSString *)server withOptions:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
2018-02-25 22:53:42 +00:00
|
|
|
NSDictionary *query = @{
|
|
|
|
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
|
|
|
|
(__bridge NSString *)kSecAttrServer: server,
|
|
|
|
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
|
|
|
|
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanTrue,
|
2018-02-26 11:31:06 +00:00
|
|
|
(__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne
|
2018-02-25 22:53:42 +00:00
|
|
|
};
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2015-05-20 18:39:52 +00:00
|
|
|
// Look up server in the keychain
|
2017-02-10 09:58:39 +00:00
|
|
|
NSDictionary *found = nil;
|
2015-05-28 17:10:41 +00:00
|
|
|
CFTypeRef foundTypeRef = NULL;
|
2018-02-25 22:53:42 +00:00
|
|
|
OSStatus osStatus = SecItemCopyMatching((__bridge CFDictionaryRef) query, (CFTypeRef*)&foundTypeRef);
|
2015-05-20 18:39:52 +00:00
|
|
|
|
2015-05-29 16:03:19 +00:00
|
|
|
if (osStatus != noErr && osStatus != errSecItemNotFound) {
|
2015-05-20 18:39:52 +00:00
|
|
|
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
|
2017-01-10 22:00:51 +00:00
|
|
|
return rejectWithError(reject, error);
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
2015-05-28 17:10:41 +00:00
|
|
|
|
|
|
|
found = (__bridge NSDictionary*)(foundTypeRef);
|
|
|
|
if (!found) {
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@(NO));
|
2015-05-28 17:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Found
|
2017-02-10 09:58:39 +00:00
|
|
|
NSString *username = (NSString *) [found objectForKey:(__bridge id)(kSecAttrAccount)];
|
|
|
|
NSString *password = [[NSString alloc] initWithData:[found objectForKey:(__bridge id)(kSecValueData)] encoding:NSUTF8StringEncoding];
|
2015-05-28 17:10:41 +00:00
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@{
|
|
|
|
@"server": server,
|
|
|
|
@"username": username,
|
|
|
|
@"password": password
|
|
|
|
});
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(resetInternetCredentialsForServer:(NSString *)server withOptions:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
2018-02-25 22:53:42 +00:00
|
|
|
OSStatus osStatus = [self deleteCredentialsForServer:server];
|
2015-05-20 18:39:52 +00:00
|
|
|
|
2015-05-29 16:03:19 +00:00
|
|
|
if (osStatus != noErr && osStatus != errSecItemNotFound) {
|
2015-05-20 18:39:52 +00:00
|
|
|
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
|
2017-01-10 22:00:51 +00:00
|
|
|
return rejectWithError(reject, error);
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
2015-05-29 16:25:56 +00:00
|
|
|
|
2017-01-10 22:00:51 +00:00
|
|
|
return resolve(@(YES));
|
2015-05-20 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:21:36 +00:00
|
|
|
#if TARGET_OS_IOS
|
2017-01-10 21:25:04 +00:00
|
|
|
RCT_EXPORT_METHOD(requestSharedWebCredentials:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
SecRequestSharedWebCredential(NULL, NULL, ^(CFArrayRef credentials, CFErrorRef error) {
|
|
|
|
if (error != NULL) {
|
|
|
|
NSError *nsError = (__bridge NSError *)error;
|
|
|
|
return reject([NSString stringWithFormat:@"%li", (long)nsError.code], nsError.description, nil);
|
|
|
|
}
|
2017-01-10 22:00:51 +00:00
|
|
|
|
2017-01-10 21:25:04 +00:00
|
|
|
if (CFArrayGetCount(credentials) > 0) {
|
|
|
|
CFDictionaryRef credentialDict = CFArrayGetValueAtIndex(credentials, 0);
|
|
|
|
NSString *server = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecAttrServer);
|
|
|
|
NSString *username = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecAttrAccount);
|
|
|
|
NSString *password = (__bridge NSString *)CFDictionaryGetValue(credentialDict, kSecSharedPassword);
|
2017-01-10 22:00:51 +00:00
|
|
|
|
2017-01-10 21:25:04 +00:00
|
|
|
return resolve(@{
|
|
|
|
@"server": server,
|
|
|
|
@"username": username,
|
|
|
|
@"password": password
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return resolve(@(NO));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-10 09:58:39 +00:00
|
|
|
RCT_EXPORT_METHOD(setSharedWebCredentialsForServer:(NSString *)server withUsername:(NSString *)username withPassword:(NSString *)password resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
|
2017-01-10 21:25:04 +00:00
|
|
|
{
|
|
|
|
SecAddSharedWebCredential(
|
|
|
|
(__bridge CFStringRef)server,
|
|
|
|
(__bridge CFStringRef)username,
|
|
|
|
(__bridge CFStringRef)password,
|
|
|
|
^(CFErrorRef error)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (error != NULL) {
|
|
|
|
NSError *nsError = (__bridge NSError *)error;
|
|
|
|
return reject([NSString stringWithFormat:@"%li", (long)nsError.code], nsError.description, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(@(YES));
|
|
|
|
});
|
|
|
|
}
|
2017-08-24 21:21:36 +00:00
|
|
|
#endif
|
2017-01-10 21:25:04 +00:00
|
|
|
|
2015-05-20 16:23:04 +00:00
|
|
|
@end
|