123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // SAMKeychain.m
- // SAMKeychain
- //
- // Created by Sam Soffes on 5/19/10.
- // Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
- //
- #import "SAMKeychain.h"
- #import "SAMKeychainQuery.h"
- NSString *const kSAMKeychainErrorDomain = @"com.samsoffes.samkeychain";
- NSString *const kSAMKeychainAccountKey = @"acct";
- NSString *const kSAMKeychainCreatedAtKey = @"cdat";
- NSString *const kSAMKeychainClassKey = @"labl";
- NSString *const kSAMKeychainDescriptionKey = @"desc";
- NSString *const kSAMKeychainLabelKey = @"labl";
- NSString *const kSAMKeychainLastModifiedKey = @"mdat";
- NSString *const kSAMKeychainWhereKey = @"svce";
- #if __IPHONE_4_0 && TARGET_OS_IPHONE
- static CFTypeRef SAMKeychainAccessibilityType = NULL;
- #endif
- @implementation SAMKeychain
- + (nullable NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account {
- return [self passwordForService:serviceName account:account error:nil];
- }
- + (nullable NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- query.account = account;
- [query fetch:error];
- return query.password;
- }
- + (nullable NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account {
- return [self passwordDataForService:serviceName account:account error:nil];
- }
- + (nullable NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- query.account = account;
- [query fetch:error];
- return query.passwordData;
- }
- + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account {
- return [self deletePasswordForService:serviceName account:account error:nil];
- }
- + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- query.account = account;
- return [query deleteItem:error];
- }
- + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account {
- return [self setPassword:password forService:serviceName account:account error:nil];
- }
- + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- query.account = account;
- query.password = password;
- return [query save:error];
- }
- + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account {
- return [self setPasswordData:password forService:serviceName account:account error:nil];
- }
- + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- query.account = account;
- query.passwordData = password;
- return [query save:error];
- }
- + (nullable NSArray *)allAccounts {
- return [self allAccounts:nil];
- }
- + (nullable NSArray *)allAccounts:(NSError *__autoreleasing *)error {
- return [self accountsForService:nil error:error];
- }
- + (nullable NSArray *)accountsForService:(nullable NSString *)serviceName {
- return [self accountsForService:serviceName error:nil];
- }
- + (nullable NSArray *)accountsForService:(nullable NSString *)serviceName error:(NSError *__autoreleasing *)error {
- SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
- query.service = serviceName;
- return [query fetchAll:error];
- }
- #if __IPHONE_4_0 && TARGET_OS_IPHONE
- + (CFTypeRef)accessibilityType {
- return SAMKeychainAccessibilityType;
- }
- + (void)setAccessibilityType:(CFTypeRef)accessibilityType {
- CFRetain(accessibilityType);
- if (SAMKeychainAccessibilityType) {
- CFRelease(SAMKeychainAccessibilityType);
- }
- SAMKeychainAccessibilityType = accessibilityType;
- }
- #endif
- @end
|