Skip to content

Commit

Permalink
chore: rename synchronized to cloudSync
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianMazur committed Oct 9, 2024
1 parent c99bc20 commit 79771b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
51 changes: 29 additions & 22 deletions ios/RNKeychainManager/RNKeychainManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ CFStringRef accessibleValue(NSDictionary *options)
return [[NSBundle mainBundle] bundleIdentifier];
}

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

NSString *accessGroupValue(NSDictionary *options)
{
if (options && options[@"accessGroup"] != nil) {
Expand All @@ -127,9 +135,9 @@ CFStringRef accessibleValue(NSDictionary *options)
return nil;
}

CFBooleanRef synchronizedValue(NSDictionary *options)
CFBooleanRef cloudSyncValue(NSDictionary *options)
{
if (options && options[@"synchronized"]) {
if (options && options[@"cloudSync"]) {
return kCFBooleanTrue;
}
return kCFBooleanFalse;
Expand Down Expand Up @@ -262,11 +270,11 @@ - (void)insertKeychainEntry:(NSDictionary *)attributes
- (OSStatus)deletePasswordsForOptions:(NSDictionary *)options
{
NSString *service = serviceValue(options);
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);
NSDictionary *query = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)synchronized,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)cloudSync,
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanFalse
};
Expand All @@ -276,11 +284,11 @@ - (OSStatus)deletePasswordsForOptions:(NSDictionary *)options

- (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDictionary * __nullable)options
{
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);
NSDictionary *query = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
(__bridge NSString *)kSecAttrServer: server,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(synchronized),
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(cloudSync),
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanFalse
};
Expand Down Expand Up @@ -372,12 +380,12 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDiction
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *service = serviceValue(options);
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);
NSDictionary *attributes = attributes = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecAttrAccount: username,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(synchronized),
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(cloudSync),
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
};

Expand All @@ -392,12 +400,12 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDiction
{
NSString *service = serviceValue(options);
NSString *authenticationPrompt = authenticationPromptValue(options);
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);

NSDictionary *query = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassGenericPassword),
(__bridge NSString *)kSecAttrService: service,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(options[@"synchronized"] ? kCFBooleanTrue : kCFBooleanFalse),
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(cloudSync),
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne,
Expand Down Expand Up @@ -460,31 +468,31 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDiction
rejecter:(RCTPromiseRejectBlock)reject)
{
[self deleteCredentialsForServer:server withOptions: options];
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);

NSDictionary *attributes = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
(__bridge NSString *)kSecAttrServer: server,
(__bridge NSString *)kSecAttrAccount: username,
(__bridge NSString *)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding],
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(synchronized),
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(cloudSync),
};

[self insertKeychainEntry:attributes withOptions:options resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(hasInternetCredentialsForServer:(NSString *)server
withOptions:(NSDictionary * __nullable)options
RCT_EXPORT_METHOD(hasInternetCredentialsForServer:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
CFBooleanRef synchronized = synchronizedValue(options);

CFBooleanRef cloudSync = cloudSyncValue(options);
NSString *server = serverValue(options);
NSMutableDictionary *queryParts = [[NSMutableDictionary alloc] init];

queryParts[(__bridge NSString *)kSecClass] = (__bridge id)(kSecClassInternetPassword);
queryParts[(__bridge NSString *)kSecAttrServer] = server;
queryParts[(__bridge NSString *)kSecMatchLimit] = (__bridge NSString *)kSecMatchLimitOne;
queryParts[(__bridge NSString *)kSecAttrSynchronizable] = (__bridge id)(synchronized);
queryParts[(__bridge NSString *)kSecAttrSynchronizable] = (__bridge id)(cloudSync);

if (@available(iOS 9, *)) {
queryParts[(__bridge NSString *)kSecUseAuthenticationUI] = (__bridge NSString *)kSecUseAuthenticationUIFail;
Expand Down Expand Up @@ -546,13 +554,13 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDiction
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
CFBooleanRef synchronized = synchronizedValue(options);
CFBooleanRef cloudSync = cloudSyncValue(options);
NSString *authenticationPrompt = authenticationPromptValue(options);
NSDictionary *query = @{
(__bridge NSString *)kSecClass: (__bridge id)(kSecClassInternetPassword),
(__bridge NSString *)kSecAttrServer: server,
(__bridge NSString *)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(synchronized),
(__bridge NSString *)kSecAttrSynchronizable: (__bridge id)(cloudSync),
(__bridge NSString *)kSecReturnData: (__bridge id)kCFBooleanTrue,
(__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne,
(__bridge NSString *)kSecUseOperationPrompt: authenticationPrompt
Expand Down Expand Up @@ -587,13 +595,12 @@ - (OSStatus)deleteCredentialsForServer:(NSString *)server withOptions:(NSDiction

}

RCT_EXPORT_METHOD(resetInternetCredentialsForServer:(NSString *)server
withOptions:(NSDictionary * __nullable)options
RCT_EXPORT_METHOD(resetInternetCredentialsForServer:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *server = serverValue(options);
OSStatus osStatus = [self deleteCredentialsForServer:server withOptions:options];

if (osStatus != noErr && osStatus != errSecItemNotFound) {
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
return rejectWithError(reject, error);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export type Options = {
/** The security rules to apply when storing the keychain item (Android only). */
rules?: SECURITY_RULES;
/** Whether to synchronize the keychain item to iCloud (iOS only). */
synchronized?: boolean;
cloudSync?: boolean;
/** Authentication prompt details or a title string. */
authenticationPrompt?: string | AuthenticationPrompt;
};
Expand Down

0 comments on commit 79771b4

Please sign in to comment.