DBKeychain-iOS.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // DBKeychain.m
  3. // DropboxSDK
  4. //
  5. // Created by Brian Smith on 4/5/12.
  6. // Copyright (c) 2012 Dropbox, Inc. All rights reserved.
  7. //
  8. #import "DBKeychain.h"
  9. #import <UIKit/UIKit.h>
  10. #import "DBLog.h"
  11. static NSDictionary *kDBKeychainDict;
  12. @implementation DBKeychain
  13. //TWS modify
  14. + (void)initialize {
  15. if ([self class] != [DBKeychain class]) return;
  16. //NSString *keychainId = [NSString stringWithFormat:@"%@.dropbox.auth", [[NSBundle mainBundle] bundleIdentifier]];
  17. //kDBKeychainDict = [[NSDictionary alloc] initWithObjectsAndKeys:(id)kSecClassGenericPassword, (id)kSecClass, keychainId, (id)kSecAttrService, nil];
  18. NSString *keychainId = [NSString stringWithFormat:@"%@.dropbox.auth", [self mainBundleName]];
  19. kDBKeychainDict = [[NSDictionary alloc] initWithObjectsAndKeys:
  20. (id)kSecClassGenericPassword, (id)kSecClass,
  21. keychainId, (id)kSecAttrService,
  22. @"group.it.twsweb.Crypto-Cloud",(id)kSecAttrAccessGroup,
  23. nil];
  24. }
  25. //TWS add
  26. + (NSString *)mainBundleName
  27. {
  28. /*
  29. NSUserDefaults *sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.it.twsweb.Crypto-Cloud"];
  30. // Esiste la chiave versioneprofessional ??
  31. if ([sharedUserDefaults objectForKey:@"versioneprofessional"]) {
  32. if ([[sharedUserDefaults objectForKey:@"versioneprofessional"] boolValue] == YES)
  33. return @"it.twsweb.Crypto-Cloud";
  34. else
  35. return @"it.twsweb.Crypto-Lite";
  36. } else {
  37. */
  38. NSMutableArray *components = [NSMutableArray arrayWithArray:[[[NSBundle mainBundle] bundleIdentifier] componentsSeparatedByString:@"."]];
  39. while ([components count] > 3) {
  40. [components removeLastObject];
  41. }
  42. return [components componentsJoinedByString:@"."];
  43. //}
  44. }
  45. + (NSDictionary *)credentials {
  46. NSMutableDictionary *searchDict = [NSMutableDictionary dictionaryWithDictionary:kDBKeychainDict];
  47. [searchDict setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
  48. [searchDict setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
  49. [searchDict setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
  50. NSDictionary *attrDict = nil;
  51. OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchDict, (CFTypeRef *)&attrDict);
  52. [attrDict autorelease];
  53. NSData *foundValue = [attrDict objectForKey:(id)kSecValueData];
  54. if (status == noErr && foundValue) {
  55. return [NSKeyedUnarchiver unarchiveObjectWithData:foundValue];
  56. } else {
  57. if (status != errSecItemNotFound) {
  58. DBLogWarning(@"DropboxSDK: error reading stored credentials (%i)", (int32_t)status);
  59. }
  60. return nil;
  61. }
  62. }
  63. + (void)setCredentials:(NSDictionary *)credentials {
  64. NSData *credentialData = [NSKeyedArchiver archivedDataWithRootObject:credentials];
  65. NSMutableDictionary *attrDict = [NSMutableDictionary dictionaryWithDictionary:kDBKeychainDict];
  66. [attrDict setObject:credentialData forKey:(id)kSecValueData];
  67. NSArray *version = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
  68. if ([[version objectAtIndex:0] intValue] >= 4) {
  69. [attrDict setObject:(id)kSecAttrAccessibleAfterFirstUnlock forKey:(id)kSecAttrAccessible];
  70. }
  71. OSStatus status = noErr;
  72. if ([self credentials]) {
  73. [attrDict removeObjectForKey:(id)kSecClass];
  74. status = SecItemUpdate((CFDictionaryRef)kDBKeychainDict, (CFDictionaryRef)attrDict);
  75. } else {
  76. status = SecItemAdd((CFDictionaryRef)attrDict, NULL);
  77. }
  78. if (status != noErr) {
  79. DBLogWarning(@"DropboxSDK: error saving credentials (%i)", (int32_t)status);
  80. }
  81. }
  82. + (void)deleteCredentials {
  83. OSStatus status = SecItemDelete((CFDictionaryRef)kDBKeychainDict);
  84. if (status != noErr) {
  85. DBLogWarning(@"DropboxSDK: error deleting credentials (%i)", (int32_t)status);
  86. }
  87. }
  88. @end