-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleKeychain.m
More file actions
148 lines (121 loc) · 4.22 KB
/
Copy pathSimpleKeychain.m
File metadata and controls
148 lines (121 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// SimpleKeychain.m
// Runner
//
// Created by Alex on 21.07.2020.
// Copyright © 2020 Alex Moiseenko. All rights reserved.
//
#import "SimpleKeychain.h"
#import <Security/Security.h>
@interface KeychainResult ()
+(instancetype) success: ( NSString * _Nonnull)value;
+(instancetype) error: ( NSError * _Nonnull)error;
@end
@implementation KeychainResult
+ (instancetype)success:(NSString *)value{
return [[KeychainResult alloc] initWithValue:value andError:nil];
}
+ (instancetype)error:(NSError *)error {
return [[KeychainResult alloc] initWithValue:nil andError:error];
}
- (instancetype)initWithValue:(NSString *) value andError: (NSError *) error
{
self = [super init];
if (self) {
self->_value = value;
self->_error = error;
}
return self;
}
@end
static NSError* errorWithOSStatus(OSStatus status){
return [NSError errorWithDomain:@"keychain" code:status userInfo:@{NSLocalizedDescriptionKey: @"OSStatus error"}];
}
@implementation SimpleKeychain
{
NSString * sharedGroup;
dispatch_queue_t syncQueue;
}
-(instancetype)initWithSharedGroup: (nullable NSString *)sharedGroup
{
self = [super init];
if (self) {
self->sharedGroup = sharedGroup;
self->syncQueue = dispatch_queue_create("keychain.sync", DISPATCH_QUEUE_SERIAL);
}
return self;
}
-(instancetype)init
{
return [self initWithSharedGroup:nil];
}
-(NSMutableDictionary *) makeBaseQuery: (NSString *) key
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity: 5];
dict[(__bridge NSString*) kSecClass] = (__bridge NSString*) kSecClassGenericPassword;
dict[(__bridge NSString*) kSecAttrAccount] = key;
if (sharedGroup)
dict[(__bridge NSString*) kSecAttrAccessGroup] = sharedGroup;
return dict;
}
-(KeychainResult *) get: (NSString *) key
{
__block KeychainResult* result;
dispatch_sync(syncQueue, ^{
CFTypeRef dataTypeRef = NULL;
__auto_type query = [self makeBaseQuery:key];
query[(id)kSecReturnData] = (__bridge NSNumber*) kCFBooleanTrue;
query[(__bridge NSString*)kSecMatchLimit] = (__bridge NSString*) kSecMatchLimitOne;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) query, &dataTypeRef);
if (status == errSecSuccess){
NSData *data = (__bridge NSData *)dataTypeRef;
result = [KeychainResult success:[NSString stringWithUTF8String: [data bytes]]];
} else{
result = [KeychainResult error:errorWithOSStatus(status)];
}
});
return result;
}
-(NSError *) set:(NSString *) value forKey: (NSString *) key;
{
__block NSError* error = nil;
dispatch_sync(syncQueue, ^{
NSMutableDictionary * query = [self makeBaseQuery:key];
query[(__bridge NSString*)kSecMatchLimit] = (__bridge NSString*) kSecMatchLimitOne;
OSStatus status;
status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
switch (status) {
case errSecSuccess:
query[(__bridge NSString*)kSecMatchLimit] = nil;
status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef) @{(__bridge id)kSecValueData: [value dataUsingEncoding:NSUTF8StringEncoding]});
if (status != errSecSuccess){
error = errorWithOSStatus(status);
}
break;
case errSecItemNotFound:
query[(__bridge NSString*)kSecMatchLimit] = nil;
query[(id)kSecValueData] = [value dataUsingEncoding:NSUTF8StringEncoding];
status = SecItemAdd((__bridge CFDictionaryRef)query, nil);
if (status != errSecSuccess){
error = errorWithOSStatus(status);
}
break;
default:
error = errorWithOSStatus(status);
break;
}
});
return error;
}
-(NSError *) remove: (NSString *) key
{
__block NSError* error = nil;
dispatch_sync(syncQueue, ^{
OSStatus status = SecItemDelete((__bridge CFDictionaryRef) [self makeBaseQuery:key]);
if (status != errSecSuccess){
error = errorWithOSStatus(status);
}
});
return error;
}
@end