SSKeychain.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // SSKeychain.m
  3. // SSKeychain
  4. //
  5. // Created by Sam Soffes on 5/19/10.
  6. // Copyright (c) 2010-2014 Sam Soffes. All rights reserved.
  7. //
  8. #import "SSKeychain.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. NSString *const kSSKeychainErrorDomain = @"com.samsoffes.sskeychain";
  11. NSString *const kSSKeychainAccountKey = @"acct";
  12. NSString *const kSSKeychainCreatedAtKey = @"cdat";
  13. NSString *const kSSKeychainClassKey = @"labl";
  14. NSString *const kSSKeychainDescriptionKey = @"desc";
  15. NSString *const kSSKeychainLabelKey = @"labl";
  16. NSString *const kSSKeychainLastModifiedKey = @"mdat";
  17. NSString *const kSSKeychainWhereKey = @"svce";
  18. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  19. static CFTypeRef SSKeychainAccessibilityType = NULL;
  20. #endif
  21. @implementation SSKeychain
  22. + (NSString *)accountForWFService {
  23. NSString *clientId = [[WFCCNetworkService sharedInstance] getClientId];
  24. return [NSString stringWithFormat:@"WF_Account_%@", clientId];
  25. }
  26. + (NSString *)passwordForWFService:(NSString *)serviceName {
  27. return [self passwordForService:serviceName account:[self accountForWFService] error:nil];
  28. }
  29. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account {
  30. return [self passwordForService:serviceName account:account error:nil];
  31. }
  32. + (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
  33. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  34. query.service = serviceName;
  35. query.account = account;
  36. [query fetch:error];
  37. return query.password;
  38. }
  39. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account {
  40. return [self passwordDataForService:serviceName account:account error:nil];
  41. }
  42. + (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error {
  43. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  44. query.service = serviceName;
  45. query.account = account;
  46. [query fetch:error];
  47. return query.passwordData;
  48. }
  49. + (BOOL)deletePasswordForWFService:(NSString *)serviceName {
  50. return [self deletePasswordForService:serviceName account:[self accountForWFService] error:nil];
  51. }
  52. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account {
  53. return [self deletePasswordForService:serviceName account:account error:nil];
  54. }
  55. + (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
  56. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  57. query.service = serviceName;
  58. query.account = account;
  59. return [query deleteItem:error];
  60. }
  61. + (BOOL)setPassword:(NSString *)password forWFService:(NSString *)serviceName {
  62. return [self setPassword:password forService:serviceName account:[self accountForWFService] error:nil];
  63. }
  64. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account {
  65. return [self setPassword:password forService:serviceName account:account error:nil];
  66. }
  67. + (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError *__autoreleasing *)error {
  68. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  69. query.service = serviceName;
  70. query.account = account;
  71. query.password = password;
  72. return [query save:error];
  73. }
  74. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account {
  75. return [self setPasswordData:password forService:serviceName account:account error:nil];
  76. }
  77. + (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error {
  78. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  79. query.service = serviceName;
  80. query.account = account;
  81. query.passwordData = password;
  82. return [query save:error];
  83. }
  84. + (NSArray *)allAccounts {
  85. return [self allAccounts:nil];
  86. }
  87. + (NSArray *)allAccounts:(NSError *__autoreleasing *)error {
  88. return [self accountsForService:nil error:error];
  89. }
  90. + (NSArray *)accountsForService:(NSString *)serviceName {
  91. return [self accountsForService:serviceName error:nil];
  92. }
  93. + (NSArray *)accountsForService:(NSString *)serviceName error:(NSError *__autoreleasing *)error {
  94. SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
  95. query.service = serviceName;
  96. return [query fetchAll:error];
  97. }
  98. #if __IPHONE_4_0 && TARGET_OS_IPHONE
  99. + (CFTypeRef)accessibilityType {
  100. return SSKeychainAccessibilityType;
  101. }
  102. + (void)setAccessibilityType:(CFTypeRef)accessibilityType {
  103. CFRetain(accessibilityType);
  104. if (SSKeychainAccessibilityType) {
  105. CFRelease(SSKeychainAccessibilityType);
  106. }
  107. SSKeychainAccessibilityType = accessibilityType;
  108. }
  109. #endif
  110. @end