Skip to content

Commit

Permalink
Allow accessibility options to be set. Fixes TheLevelUp#2
Browse files Browse the repository at this point in the history
  • Loading branch information
cwalcott committed Sep 16, 2013
1 parent 3ccdc89 commit af9390f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
11 changes: 11 additions & 0 deletions LUKeychainAccess/LUKeychainAccess.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
typedef NS_ENUM(NSInteger, LUKeychainAccessAccessibility) {
LUKeychainAccessAttrAccessibleAfterFirstUnlock,
LUKeychainAccessAttrAccessibleAfterFirstUnlockThisDeviceOnly,
LUKeychainAccessAttrAccessibleAlways,
LUKeychainAccessAttrAccessibleAlwaysThisDeviceOnly,
LUKeychainAccessAttrAccessibleWhenUnlocked,
LUKeychainAccessAttrAccessibleWhenUnlockedThisDeviceOnly
};

@interface LUKeychainAccess : NSObject

@property (nonatomic, assign) LUKeychainAccessAccessibility accessibilityState;

// Public Methods
+ (LUKeychainAccess *)standardKeychainAccess;

Expand Down
35 changes: 35 additions & 0 deletions LUKeychainAccess/LUKeychainAccess.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ + (LUKeychainAccess *)standardKeychainAccess {
return [[self alloc] init];
}

- (id)init {
self = [super init];
if (!self) return nil;

_accessibilityState = LUKeychainAccessAttrAccessibleWhenUnlocked;

return self;
}

#pragma mark - Getters

- (BOOL)boolForKey:(NSString *)key {
Expand Down Expand Up @@ -128,6 +137,31 @@ - (void)setObject:(id)value forKey:(NSString *)key {

#pragma mark - Private Methods

- (CFTypeRef)accessibilityStateCFType {
switch (self.accessibilityState) {
case LUKeychainAccessAttrAccessibleAfterFirstUnlock:
return kSecAttrAccessibleAfterFirstUnlock;

case LUKeychainAccessAttrAccessibleAfterFirstUnlockThisDeviceOnly:
return kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;

case LUKeychainAccessAttrAccessibleAlways:
return kSecAttrAccessibleAlways;

case LUKeychainAccessAttrAccessibleAlwaysThisDeviceOnly:
return kSecAttrAccessibleAlwaysThisDeviceOnly;

case LUKeychainAccessAttrAccessibleWhenUnlocked:
return kSecAttrAccessibleWhenUnlocked;

case LUKeychainAccessAttrAccessibleWhenUnlockedThisDeviceOnly:
return kSecAttrAccessibleWhenUnlockedThisDeviceOnly;

default:
return kSecAttrAccessibleWhenUnlocked;
}
}

- (void)deleteObjectForKey:(NSString *)key {
if (key == nil) {
return;
Expand All @@ -140,6 +174,7 @@ - (void)deleteObjectForKey:(NSString *)key {
- (NSMutableDictionary *)queryDictionaryForKey:(NSString *)key {
NSMutableDictionary *query = [NSMutableDictionary dictionary];
query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
query[(__bridge id)kSecAttrAccessible] = (__bridge id)[self accessibilityStateCFType];

NSData *encodedIdentifier = [key dataUsingEncoding:NSUTF8StringEncoding];
query[(__bridge id)kSecAttrAccount] = encodedIdentifier;
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ Use `LUKeychainAccess` just as you would use `NSUserDefaults`:

There are methods for getting and setting BOOL, double, float and NSInteger types, as well as `NSString`, `NSData` and generic `NSObject` objects. You may use any object that can be encoded with `NSKeyedArchiver`.

## Accessibility

Keychain Services has several options for when a keychain item can be readable.

This option can be set in `LUKeychainAccess` through the `accessibilityState` parameter. Its possible values match those [provided by Apple](https://developer.apple.com/library/ios/DOCUMENTATION/Security/Reference/keychainservices/Reference/reference.html#//apple_ref/doc/constant_group/Keychain_Item_Accessibility_Constants):

- `LUKeychainAccessAttrAccessibleAfterFirstUnlock`
- `LUKeychainAccessAttrAccessibleAfterFirstUnlockThisDeviceOnly`
- `LUKeychainAccessAttrAccessibleAlways`
- `LUKeychainAccessAttrAccessibleAlwaysThisDeviceOnly`
- `LUKeychainAccessAttrAccessibleWhenUnlocked`
- `LUKeychainAccessAttrAccessibleWhenUnlockedThisDeviceOnly`

The default value is `LUKeychainAccessAttrAccessibleWhenUnlocked`.

## Requirements

`LUKeychainAccess` requires iOS 5.0+. The tests are written using [Kiwi](https://github.com/allending/Kiwi).

## License

`LUKeychainAccess` is written by Costa Walcott, and is Copyright 2012 SCVNGR, Inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
`LUKeychainAccess` is written by Costa Walcott, and is Copyright 2012-2013 SCVNGR, Inc D.B.A. LevelUp. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

0 comments on commit af9390f

Please sign in to comment.