mirror of
https://github.com/zhigang1992/UICKeyChainStore.git
synced 2026-01-12 22:53:17 +08:00
@@ -14,11 +14,89 @@ typedef NS_ENUM(NSInteger, UICKeyChainStoreErrorCode) {
|
||||
UICKeyChainStoreErrorInvalidArguments = 1,
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, UICKeyChainStoreItemClass) {
|
||||
UICKeyChainStoreItemClassGenericPassword = 1,
|
||||
UICKeyChainStoreItemClassInternetPassword,
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, UICKeyChainStoreProtocolType) {
|
||||
UICKeyChainStoreProtocolTypeFTP = 1,
|
||||
UICKeyChainStoreProtocolTypeFTPAccount,
|
||||
UICKeyChainStoreProtocolTypeHTTP,
|
||||
UICKeyChainStoreProtocolTypeIRC,
|
||||
UICKeyChainStoreProtocolTypeNNTP,
|
||||
UICKeyChainStoreProtocolTypePOP3,
|
||||
UICKeyChainStoreProtocolTypeSMTP,
|
||||
UICKeyChainStoreProtocolTypeSOCKS,
|
||||
UICKeyChainStoreProtocolTypeIMAP,
|
||||
UICKeyChainStoreProtocolTypeLDAP,
|
||||
UICKeyChainStoreProtocolTypeAppleTalk,
|
||||
UICKeyChainStoreProtocolTypeAFP,
|
||||
UICKeyChainStoreProtocolTypeTelnet,
|
||||
UICKeyChainStoreProtocolTypeSSH,
|
||||
UICKeyChainStoreProtocolTypeFTPS,
|
||||
UICKeyChainStoreProtocolTypeHTTPS,
|
||||
UICKeyChainStoreProtocolTypeHTTPProxy,
|
||||
UICKeyChainStoreProtocolTypeHTTPSProxy,
|
||||
UICKeyChainStoreProtocolTypeFTPProxy,
|
||||
UICKeyChainStoreProtocolTypeSMB,
|
||||
UICKeyChainStoreProtocolTypeRTSP,
|
||||
UICKeyChainStoreProtocolTypeRTSPProxy,
|
||||
UICKeyChainStoreProtocolTypeDAAP,
|
||||
UICKeyChainStoreProtocolTypeEPPC,
|
||||
UICKeyChainStoreProtocolTypeNNTPS,
|
||||
UICKeyChainStoreProtocolTypeLDAPS,
|
||||
UICKeyChainStoreProtocolTypeTelnetS,
|
||||
UICKeyChainStoreProtocolTypeIRCS,
|
||||
UICKeyChainStoreProtocolTypePOP3S,
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, UICKeyChainStoreAuthenticationType) {
|
||||
UICKeyChainStoreAuthenticationTypeNTLM = 1,
|
||||
UICKeyChainStoreAuthenticationTypeMSN,
|
||||
UICKeyChainStoreAuthenticationTypeDPA,
|
||||
UICKeyChainStoreAuthenticationTypeRPA,
|
||||
UICKeyChainStoreAuthenticationTypeHTTPBasic,
|
||||
UICKeyChainStoreAuthenticationTypeHTTPDigest,
|
||||
UICKeyChainStoreAuthenticationTypeHTMLForm,
|
||||
UICKeyChainStoreAuthenticationTypeDefault,
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, UICKeyChainStoreAccessibility) {
|
||||
UICKeyChainStoreAccessibilityWhenUnlocked = 1,
|
||||
UICKeyChainStoreAccessibilityAfterFirstUnlock,
|
||||
UICKeyChainStoreAccessibilityAlways,
|
||||
UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
|
||||
__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0),
|
||||
UICKeyChainStoreAccessibilityWhenUnlockedThisDeviceOnly,
|
||||
UICKeyChainStoreAccessibilityAfterFirstUnlockThisDeviceOnly,
|
||||
UICKeyChainStoreAccessibilityAlwaysThisDeviceOnly,
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, UICKeyChainStoreAuthenticationPolicy) {
|
||||
UICKeyChainStoreAuthenticationPolicyUserPresence = kSecAccessControlUserPresence,
|
||||
};
|
||||
|
||||
@interface UICKeyChainStore : NSObject
|
||||
|
||||
@property (nonatomic, readonly) UICKeyChainStoreItemClass itemClass;
|
||||
|
||||
@property (nonatomic, readonly) NSString *service;
|
||||
@property (nonatomic, readonly) NSString *accessGroup;
|
||||
|
||||
@property (nonatomic, readonly) NSURL *server;
|
||||
@property (nonatomic, readonly) UICKeyChainStoreProtocolType protocolType;
|
||||
@property (nonatomic, readonly) UICKeyChainStoreAuthenticationType authenticationType;
|
||||
|
||||
@property (nonatomic) UICKeyChainStoreAccessibility accessibility;
|
||||
@property (nonatomic, readonly) UICKeyChainStoreAuthenticationPolicy authenticationPolicy
|
||||
__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
|
||||
|
||||
@property (nonatomic) BOOL synchronizable;
|
||||
|
||||
@property (nonatomic) NSString *authenticationPrompt
|
||||
__OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
|
||||
|
||||
+ (NSString *)defaultService;
|
||||
+ (void)setDefaultService:(NSString *)defaultService;
|
||||
|
||||
@@ -26,10 +104,16 @@ typedef NS_ENUM(NSInteger, UICKeyChainStoreErrorCode) {
|
||||
+ (UICKeyChainStore *)keyChainStoreWithService:(NSString *)service;
|
||||
+ (UICKeyChainStore *)keyChainStoreWithService:(NSString *)service accessGroup:(NSString *)accessGroup;
|
||||
|
||||
+ (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
|
||||
+ (UICKeyChainStore *)keyChainStoreWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
|
||||
|
||||
- (instancetype)init;
|
||||
- (instancetype)initWithService:(NSString *)service;
|
||||
- (instancetype)initWithService:(NSString *)service accessGroup:(NSString *)accessGroup;
|
||||
|
||||
- (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType;
|
||||
- (instancetype)initWithServer:(NSURL *)server protocolType:(UICKeyChainStoreProtocolType)protocolType authenticationType:(UICKeyChainStoreAuthenticationType)authenticationType;
|
||||
|
||||
+ (NSString *)stringForKey:(NSString *)key;
|
||||
+ (NSString *)stringForKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
+ (NSString *)stringForKey:(NSString *)key service:(NSString *)service;
|
||||
@@ -56,13 +140,19 @@ typedef NS_ENUM(NSInteger, UICKeyChainStoreErrorCode) {
|
||||
+ (BOOL)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup;
|
||||
+ (BOOL)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError * __autoreleasing *)error;
|
||||
|
||||
- (void)setString:(NSString *)string forKey:(NSString *)key;
|
||||
- (BOOL)contains:(NSString *)key;
|
||||
|
||||
- (BOOL)setString:(NSString *)string forKey:(NSString *)key;
|
||||
- (BOOL)setString:(NSString *)string forKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
- (BOOL)setString:(NSString *)string forKey:(NSString *)key label:(NSString *)label comment:(NSString *)comment;
|
||||
- (BOOL)setString:(NSString *)string forKey:(NSString *)key label:(NSString *)label comment:(NSString *)comment error:(NSError * __autoreleasing *)error;
|
||||
- (NSString *)stringForKey:(NSString *)key;
|
||||
- (NSString *)stringForKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
|
||||
- (void)setData:(NSData *)data forKey:(NSString *)key;
|
||||
- (BOOL)setData:(NSData *)data forKey:(NSString *)key;
|
||||
- (BOOL)setData:(NSData *)data forKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
- (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label comment:(NSString *)comment;
|
||||
- (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label comment:(NSString *)comment error:(NSError * __autoreleasing *)error;
|
||||
- (NSData *)dataForKey:(NSString *)key;
|
||||
- (NSData *)dataForKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
|
||||
@@ -79,17 +169,24 @@ typedef NS_ENUM(NSInteger, UICKeyChainStoreErrorCode) {
|
||||
+ (BOOL)removeAllItemsForService:(NSString *)service accessGroup:(NSString *)accessGroup;
|
||||
+ (BOOL)removeAllItemsForService:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError * __autoreleasing *)error;
|
||||
|
||||
- (void)removeItemForKey:(NSString *)key;
|
||||
- (BOOL)removeItemForKey:(NSString *)key;
|
||||
- (BOOL)removeItemForKey:(NSString *)key error:(NSError * __autoreleasing *)error;
|
||||
- (void)removeAllItems;
|
||||
- (BOOL)removeAllItems;
|
||||
- (BOOL)removeAllItemsWithError:(NSError * __autoreleasing *)error;
|
||||
|
||||
- (void)synchronize;
|
||||
- (BOOL)synchronizeWithError:(NSError *__autoreleasing *)error;
|
||||
|
||||
// object subscripting
|
||||
|
||||
- (NSString *)objectForKeyedSubscript:(NSString <NSCopying> *)key;
|
||||
- (void)setObject:(NSString *)obj forKeyedSubscript:(NSString <NSCopying> *)key;
|
||||
|
||||
+ (NSArray *)allKeysWithItemClass:(CFTypeRef)itemClass;
|
||||
- (NSArray *)allKeys;
|
||||
|
||||
+ (NSArray *)allItemsWithItemClass:(CFTypeRef)itemClass;
|
||||
- (NSArray *)allItems;
|
||||
|
||||
- (void)setAccessibility:(UICKeyChainStoreAccessibility)accessibility authenticationPolicy:(UICKeyChainStoreAuthenticationPolicy)authenticationPolicy
|
||||
__OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
|
||||
|
||||
- (void)synchronize __attribute__((deprecated("calling this method is no longer required")));
|
||||
- (BOOL)synchronizeWithError:(NSError *__autoreleasing *)error __attribute__((deprecated("calling this method is no longer required")));
|
||||
|
||||
@end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -82,6 +82,7 @@
|
||||
XCTAssertNil([UICKeyChainStore dataForKey:passwordKey], @"removed password");
|
||||
}
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
- (void)testSetDataWithNoError
|
||||
{
|
||||
NSString *usernameKey = @"username";
|
||||
@@ -117,6 +118,7 @@
|
||||
XCTAssertNil([UICKeyChainStore dataForKey:passwordKey error:&error], @"removed password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)testSetNilData
|
||||
{
|
||||
@@ -183,6 +185,7 @@
|
||||
XCTAssertNil([store stringForKey:passwordKey], @"removed password");
|
||||
}
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
- (void)testSetUsernameAndPasswordWithNoError
|
||||
{
|
||||
NSString *usernameKey = @"username";
|
||||
@@ -231,6 +234,7 @@
|
||||
XCTAssertNil([store stringForKey:passwordKey error:&error], @"removed password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)testSetNilUsernameAndNilPassword
|
||||
{
|
||||
@@ -266,142 +270,6 @@
|
||||
XCTAssertNil([store stringForKey:passwordKey], @"removed password");
|
||||
}
|
||||
|
||||
- (void)testSynchronize1
|
||||
{
|
||||
NSString *usernameKey = @"username";
|
||||
NSString *passwordKey = @"password";
|
||||
NSString *username = @"kishikawakatsumi";
|
||||
NSString *password = @"password1234";
|
||||
|
||||
NSString *serviceName = @"com.example.UICKeyChainStore";
|
||||
[UICKeyChainStore removeAllItemsForService:serviceName];
|
||||
|
||||
UICKeyChainStore *store = [UICKeyChainStore keyChainStoreWithService:serviceName];
|
||||
[store removeAllItems];
|
||||
|
||||
[store setString:username forKey:usernameKey];
|
||||
[store setString:password forKey:passwordKey];
|
||||
XCTAssertEqualObjects([store stringForKey:usernameKey], username, @"stored username");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey], password, @"stored password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName], @"not synchronized yet");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"not synchronized yet");
|
||||
|
||||
[store synchronize];
|
||||
XCTAssertEqualObjects([store stringForKey:usernameKey], username, @"stored username");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey], password, @"stored password");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:usernameKey service:serviceName], username, @"stored username");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:passwordKey service:serviceName], password, @"stored password");
|
||||
|
||||
[store removeItemForKey:usernameKey];
|
||||
XCTAssertNil([store stringForKey:usernameKey], @"removed username");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey], password, @"left password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName], @"removed username");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:passwordKey service:serviceName], password, @"left password");
|
||||
|
||||
[store removeItemForKey:passwordKey];
|
||||
XCTAssertNil([store stringForKey:passwordKey], @"removed password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"removed password");
|
||||
}
|
||||
|
||||
- (void)testSynchronize2
|
||||
{
|
||||
NSString *usernameKey = @"username";
|
||||
NSString *passwordKey = @"password";
|
||||
NSString *username = @"kishikawakatsumi";
|
||||
NSString *password = @"password1234";
|
||||
|
||||
NSString *serviceName = @"com.example.UICKeyChainStore";
|
||||
[UICKeyChainStore removeAllItemsForService:serviceName];
|
||||
|
||||
UICKeyChainStore *store = [UICKeyChainStore keyChainStoreWithService:serviceName];
|
||||
[store removeAllItems];
|
||||
|
||||
[store setString:username forKey:usernameKey];
|
||||
[store setString:password forKey:passwordKey];
|
||||
XCTAssertEqualObjects([store stringForKey:usernameKey], username, @"stored username");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey], password, @"stored password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName], @"not synchronized yet");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"not synchronized yet");
|
||||
|
||||
[store removeItemForKey:usernameKey];
|
||||
XCTAssertNil([store stringForKey:usernameKey], @"removed username");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey], password, @"left password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName], @"not synchronized yet");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"not synchronized yet");
|
||||
|
||||
[store removeItemForKey:passwordKey];
|
||||
XCTAssertNil([store stringForKey:passwordKey], @"removed password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"not synchronized yet");
|
||||
|
||||
[store synchronize];
|
||||
|
||||
XCTAssertNil([store stringForKey:usernameKey], @"removed username");
|
||||
XCTAssertNil([store stringForKey:passwordKey], @"removed password");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName], @"removed username");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName], @"removed password");
|
||||
}
|
||||
|
||||
- (void)testSynchronizeWithNoError
|
||||
{
|
||||
NSString *usernameKey = @"username";
|
||||
NSString *passwordKey = @"password";
|
||||
NSString *username = @"kishikawakatsumi";
|
||||
NSString *password = @"password1234";
|
||||
|
||||
NSString *serviceName = @"com.example.UICKeyChainStore";
|
||||
|
||||
NSError *error;
|
||||
|
||||
[UICKeyChainStore removeAllItemsForService:serviceName error:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
|
||||
UICKeyChainStore *store = [UICKeyChainStore keyChainStoreWithService:serviceName];
|
||||
[store removeAllItemsWithError:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
|
||||
[store setString:username forKey:usernameKey error:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
[store setString:password forKey:passwordKey error:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([store stringForKey:usernameKey error:&error], username, @"stored username");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey error:&error], password, @"stored password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName error:&error], @"not synchronized yet");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName error:&error], @"not synchronized yet");
|
||||
XCTAssertNil(error, @"no error");
|
||||
|
||||
[store synchronizeWithError:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([store stringForKey:usernameKey error:&error], username, @"stored username");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey error:&error], password, @"stored password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:usernameKey service:serviceName error:&error], username, @"stored username");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:passwordKey service:serviceName error:&error], password, @"stored password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
|
||||
[store removeItemForKey:usernameKey error:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([store stringForKey:usernameKey error:&error], @"removed username");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([store stringForKey:passwordKey error:&error], password, @"left password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:usernameKey service:serviceName error:&error], @"removed username");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertEqualObjects([UICKeyChainStore stringForKey:passwordKey service:serviceName error:&error], password, @"left password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
|
||||
[store removeItemForKey:passwordKey error:&error];
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([store stringForKey:passwordKey error:&error], @"removed password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
XCTAssertNil([UICKeyChainStore stringForKey:passwordKey service:serviceName error:&error], @"removed password");
|
||||
XCTAssertNil(error, @"no error");
|
||||
}
|
||||
|
||||
- (void)testClassMethodsSetAndRemoveItem
|
||||
{
|
||||
// write to keychain
|
||||
@@ -424,6 +292,7 @@
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
}
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
- (void)testClassMethodsSetAndRemoveItemWithNoError
|
||||
{
|
||||
NSError *error;
|
||||
@@ -449,6 +318,7 @@
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)testInstanceMethodsSetAndRemoveItem
|
||||
{
|
||||
@@ -473,13 +343,9 @@
|
||||
expectedString = NULL;
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
|
||||
actualString = [store description];
|
||||
expectedString = @"(\n)";
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
}
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
- (void)testInstanceMethodsSetAndRemoveItemWithNoError
|
||||
{
|
||||
NSError *error;
|
||||
@@ -507,12 +373,8 @@
|
||||
expectedString = NULL;
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
|
||||
actualString = [store description];
|
||||
expectedString = @"(\n)";
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)testInstanceMethodsSetAndRemoveWithNilValue
|
||||
{
|
||||
@@ -533,11 +395,6 @@
|
||||
expectedString = NULL;
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
|
||||
actualString = [store description];
|
||||
expectedString = @"(\n)";
|
||||
XCTAssertEqualObjects(expectedString, actualString,
|
||||
@"expected %@ but got %@", expectedString, actualString);
|
||||
}
|
||||
|
||||
- (void)testObjectSubscripting
|
||||
|
||||
Reference in New Issue
Block a user