Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Small improvements #79

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
14 changes: 7 additions & 7 deletions SSKeychain/SSKeychain.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ typedef NS_ENUM(OSStatus, SSKeychainErrorCode) {
extern NSString *const kSSKeychainErrorDomain;

/** Account name. */
extern NSString *const kSSKeychainAccountKey;
extern NSString *kSSKeychainAccountKey;

/**
Time the item was created.

The value will be a string.
*/
extern NSString *const kSSKeychainCreatedAtKey;
extern NSString *kSSKeychainCreationDateKey;

/** Item class. */
extern NSString *const kSSKeychainClassKey;
extern NSString *kSSKeychainClassKey;

/** Item description. */
extern NSString *const kSSKeychainDescriptionKey;
extern NSString *kSSKeychainDescriptionKey;

/** Item label. */
extern NSString *const kSSKeychainLabelKey;
extern NSString *kSSKeychainLabelKey;

/** Time the item was last modified.

The value will be a string.
*/
extern NSString *const kSSKeychainLastModifiedKey;
extern NSString *kSSKeychainModificationDateKey;

/** Where the item was created. */
extern NSString *const kSSKeychainWhereKey;
extern NSString *kSSKeychainServiceKey;

/**
Simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system
Expand Down
25 changes: 18 additions & 7 deletions SSKeychain/SSKeychain.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@
#import "SSKeychain.h"

NSString *const kSSKeychainErrorDomain = @"com.samsoffes.sskeychain";
NSString *const kSSKeychainAccountKey = @"acct";
NSString *const kSSKeychainCreatedAtKey = @"cdat";
NSString *const kSSKeychainClassKey = @"labl";
NSString *const kSSKeychainDescriptionKey = @"desc";
NSString *const kSSKeychainLabelKey = @"labl";
NSString *const kSSKeychainLastModifiedKey = @"mdat";
NSString *const kSSKeychainWhereKey = @"svce";
NSString *kSSKeychainAccountKey;
NSString *kSSKeychainCreationDateKey;
NSString *kSSKeychainClassKey;
NSString *kSSKeychainDescriptionKey;
NSString *kSSKeychainLabelKey;
NSString *kSSKeychainModificationDateKey;
NSString *kSSKeychainServiceKey;

#if __IPHONE_4_0 && TARGET_OS_IPHONE
static CFTypeRef SSKeychainAccessibilityType = NULL;
#endif

@implementation SSKeychain

+ (void)load {
kSSKeychainAccountKey = (__bridge id)kSecAttrAccount;
kSSKeychainCreationDateKey = (__bridge id)kSecAttrCreationDate;
kSSKeychainClassKey = (__bridge id)kSecClass;
kSSKeychainDescriptionKey = (__bridge id)kSecAttrDescription;
kSSKeychainLabelKey = (__bridge id)kSecAttrLabel;
kSSKeychainModificationDateKey = (__bridge id)kSecAttrModificationDate;
kSSKeychainServiceKey = (__bridge id)kSecAttrService;
}


+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account {
return [self passwordForService:serviceName account:account error:nil];
}
Expand Down
32 changes: 16 additions & 16 deletions SSKeychain/SSKeychainQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,30 @@ - (BOOL)save:(NSError *__autoreleasing *)error {
return NO;
}

[self deleteItem:nil];

NSMutableDictionary *query = [self query];
[query setObject:self.passwordData forKey:(__bridge id)kSecValueData];
[query setObject:@YES forKey:(__bridge id)kSecReturnData];
[query setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];

NSMutableDictionary *updates = [NSMutableDictionary dictionaryWithCapacity:2];
[updates setObject:self.passwordData forKey:(__bridge id)kSecValueData];
if (self.label) {
[query setObject:self.label forKey:(__bridge id)kSecAttrLabel];
[updates setObject:self.label forKey:(__bridge id)kSecAttrLabel];
}

#if __IPHONE_4_0 && TARGET_OS_IPHONE
CFTypeRef accessibilityType = [SSKeychain accessibilityType];
if (accessibilityType) {
[query setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible];
[updates setObject:(__bridge id)accessibilityType forKey:(__bridge id)kSecAttrAccessible];
}
#endif
status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);

status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
if (status == errSecSuccess) {
status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)updates);
} else if (status == errSecItemNotFound) {
[query addEntriesFromDictionary:updates];
status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
}

if (status != errSecSuccess && error != NULL) {
*error = [[self class] errorWithCode:status];
Expand All @@ -68,17 +78,7 @@ - (BOOL)deleteItem:(NSError *__autoreleasing *)error {
}

NSMutableDictionary *query = [self query];
#if TARGET_OS_IPHONE
status = SecItemDelete((__bridge CFDictionaryRef)query);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Ah, I didn't see that. I'll revert the commit.

#else
CFTypeRef result = NULL;
[query setObject:@YES forKey:(__bridge id)kSecReturnRef];
status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
if (status == errSecSuccess) {
status = SecKeychainItemDelete((SecKeychainItemRef)result);
CFRelease(result);
}
#endif

if (status != errSecSuccess && error != NULL) {
*error = [[self class] errorWithCode:status];
Expand Down