-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathYYKeychain.h
More file actions
343 lines (262 loc) · 13.2 KB
/
Copy pathYYKeychain.h
File metadata and controls
343 lines (262 loc) · 13.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// YYKeychain.h
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 14/10/15.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import <Foundation/Foundation.h>
@class YYKeychainItem;
NS_ASSUME_NONNULL_BEGIN
/**
A wrapper for system keychain API.
Inspired by [SSKeychain](https://github.com/soffes/sskeychain) 😜
*/
@interface YYKeychain : NSObject
#pragma mark - Convenience method for keychain
///=============================================================================
/// @name Convenience method for keychain
///=============================================================================
/**
Returns the password for a given account and service, or `nil` if not found or
an error occurs.
@param serviceName The service for which to return the corresponding password.
This value must not be nil.
@param account The account for which to return the corresponding password.
This value must not be nil.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Password string, or nil when not found or error occurs.
*/
+ (nullable NSString *)getPasswordForService:(NSString *)serviceName
account:(NSString *)account
error:(NSError **)error;
+ (nullable NSString *)getPasswordForService:(NSString *)serviceName
account:(NSString *)account;
+ (nullable id)getPasswordObjectForService:(NSString *)serviceName
account:(NSString *)account
error:(NSError **)error;
/**
Deletes a password from the Keychain.
@param serviceName The service for which to return the corresponding password.
This value must not be nil.
@param account The account for which to return the corresponding password.
This value must not be nil.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Whether succeed.
*/
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
/**
Insert or update the password for a given account and service.
@param password The new password.
@param serviceName The service for which to return the corresponding password.
This value must not be nil.
@param account The account for which to return the corresponding password.
This value must not be nil.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Whether succeed.
*/
+ (BOOL)setPassword:(NSString *)password
forService:(NSString *)serviceName
account:(NSString *)account
error:(NSError **)error;
+ (BOOL)setPassword:(NSString *)password
forService:(NSString *)serviceName
account:(NSString *)account;
#pragma mark - Full query for keychain (SQL-like)
///=============================================================================
/// @name Full query for keychain (SQL-like)
///=============================================================================
/**
Insert an item into keychain.
@discussion The service,account,password is required. If there's item exist
already, an error occurs and insert fail.
@param item The item to insert.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Whether succeed.
*/
+ (BOOL)insertItem:(YYKeychainItem *)item error:(NSError **)error;
+ (BOOL)insertItem:(YYKeychainItem *)item;
/**
Update item in keychain.
@discussion The service,account,password is required. If there's no item exist
already, an error occurs and insert fail.
@param item The item to insert.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Whether succeed.
*/
+ (BOOL)updateItem:(YYKeychainItem *)item error:(NSError **)error;
+ (BOOL)updateItem:(YYKeychainItem *)item;
/**
Delete items from keychain.
@discussion The service,account,password is required. If there's item exist
already, an error occurs and insert fail.
@param item The item to update.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return Whether succeed.
*/
+ (BOOL)deleteItem:(YYKeychainItem *)item error:(NSError **)error;
+ (BOOL)deleteItem:(YYKeychainItem *)item;
/**
Find an item from keychain.
@discussion The service,account is optinal. It returns only one item if there
exist multi.
@param item The item for query.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return An item or nil.
*/
+ (nullable YYKeychainItem *)selectOneItem:(YYKeychainItem *)item error:(NSError **)error;
+ (nullable YYKeychainItem *)selectOneItem:(YYKeychainItem *)item;
/**
Find all items matches the query.
@discussion The service,account is optinal. It returns all item matches by the
query.
@param item The item for query.
@param error On input, a pointer to an error object. If an error occurs,
this pointer is set to an actual error object containing the error information.
You may specify nil for this parameter if you do not want the error information.
See `YYKeychainErrorCode`.
@return An array of YYKeychainItem.
*/
+ (nullable NSArray<YYKeychainItem *> *)selectItems:(YYKeychainItem *)item error:(NSError **)error;
+ (nullable NSArray<YYKeychainItem *> *)selectItems:(YYKeychainItem *)item;
+ (nullable NSArray<NSDictionary *> *)exportItemsWithError:(NSError **)error;
@end
#pragma mark - Const
/**
Error code in YYKeychain API.
*/
typedef NS_ENUM (NSUInteger, YYKeychainErrorCode) {
YYKeychainErrorUnimplemented = 1, ///< Function or operation not implemented.
YYKeychainErrorIO, ///< I/O error (bummers)
YYKeychainErrorOpWr, ///< File already open with with write permission.
YYKeychainErrorParam, ///< One or more parameters passed to a function where not valid.
YYKeychainErrorAllocate, ///< Failed to allocate memory.
YYKeychainErrorUserCancelled, ///< User cancelled the operation.
YYKeychainErrorBadReq, ///< Bad parameter or invalid state for operation.
YYKeychainErrorInternalComponent, ///< Internal...
YYKeychainErrorNotAvailable, ///< No keychain is available. You may need to restart your computer.
YYKeychainErrorDuplicateItem, ///< The specified item already exists in the keychain.
YYKeychainErrorItemNotFound, ///< The specified item could not be found in the keychain.
YYKeychainErrorInteractionNotAllowed, ///< User interaction is not allowed.
YYKeychainErrorDecode, ///< Unable to decode the provided data.
YYKeychainErrorAuthFailed, ///< The user name or passphrase you entered is not.
};
/**
When query to return the item's data, the error
errSecInteractionNotAllowed will be returned if the item's data is not
available until a device unlock occurs.
*/
typedef NS_ENUM (NSUInteger, YYKeychainAccessible) {
YYKeychainAccessibleNone = 0, ///< no value
/** Item data can only be accessed
while the device is unlocked. This is recommended for items that only
need be accesible while the application is in the foreground. Items
with this attribute will migrate to a new device when using encrypted
backups. */
YYKeychainAccessibleWhenUnlocked,
/** Item data can only be
accessed once the device has been unlocked after a restart. This is
recommended for items that need to be accesible by background
applications. Items with this attribute will migrate to a new device
when using encrypted backups.*/
YYKeychainAccessibleAfterFirstUnlock,
/** Item data can always be accessed
regardless of the lock state of the device. This is not recommended
for anything except system use. Items with this attribute will migrate
to a new device when using encrypted backups.*/
YYKeychainAccessibleAlways,
/** Item data can
only be accessed while the device is unlocked. This class is only
available if a passcode is set on the device. This is recommended for
items that only need to be accessible while the application is in the
foreground. Items with this attribute will never migrate to a new
device, so after a backup is restored to a new device, these items
will be missing. No items can be stored in this class on devices
without a passcode. Disabling the device passcode will cause all
items in this class to be deleted.*/
YYKeychainAccessibleWhenPasscodeSetThisDeviceOnly,
/** Item data can only
be accessed while the device is unlocked. This is recommended for items
that only need be accesible while the application is in the foreground.
Items with this attribute will never migrate to a new device, so after
a backup is restored to a new device, these items will be missing. */
YYKeychainAccessibleWhenUnlockedThisDeviceOnly,
/** Item data can
only be accessed once the device has been unlocked after a restart.
This is recommended for items that need to be accessible by background
applications. Items with this attribute will never migrate to a new
device, so after a backup is restored to a new device these items will
be missing.*/
YYKeychainAccessibleAfterFirstUnlockThisDeviceOnly,
/** Item data can always
be accessed regardless of the lock state of the device. This option
is not recommended for anything except system use. Items with this
attribute will never migrate to a new device, so after a backup is
restored to a new device, these items will be missing.*/
YYKeychainAccessibleAlwaysThisDeviceOnly,
};
/**
Whether the item in question can be synchronized.
*/
typedef NS_ENUM (NSUInteger, YYKeychainQuerySynchronizationMode) {
/** Default, Don't care for synchronization */
YYKeychainQuerySynchronizationModeAny = 0,
/** Is not synchronized */
YYKeychainQuerySynchronizationModeNo,
/** To add a new item which can be synced to other devices, or to obtain
synchronized results from a query*/
YYKeychainQuerySynchronizationModeYes,
} NS_AVAILABLE_IOS (7_0);
#pragma mark - Item
/**
Wrapper for keychain item/query.
*/
@interface YYKeychainItem : NSObject <NSCopying>
@property (nullable, nonatomic, copy) NSString *service; ///< kSecAttrService
@property (nullable, nonatomic, copy) NSString *account; ///< kSecAttrAccount
@property (nullable, nonatomic, copy) NSData *passwordData; ///< kSecValueData
@property (nullable, nonatomic, copy) NSString *password; ///< shortcut for `passwordData`
@property (nullable, nonatomic, copy) id <NSCoding> passwordObject; ///< shortcut for `passwordData`
@property (nullable, nonatomic, copy) NSString *label; ///< kSecAttrLabel
@property (nullable, nonatomic, copy) NSNumber *type; ///< kSecAttrType (FourCC)
@property (nullable, nonatomic, copy) NSNumber *creater; ///< kSecAttrCreator (FourCC)
@property (nullable, nonatomic, copy) NSString *comment; ///< kSecAttrComment
@property (nullable, nonatomic, copy) NSString *descr; ///< kSecAttrDescription
@property (nullable, nonatomic, readonly, strong) NSDate *modificationDate; ///< kSecAttrModificationDate
@property (nullable, nonatomic, readonly, strong) NSDate *creationDate; ///< kSecAttrCreationDate
@property (nullable, nonatomic, copy) NSString *accessGroup; ///< kSecAttrAccessGroup
@property (nonatomic) YYKeychainAccessible accessible; ///< kSecAttrAccessible
@property (nonatomic) YYKeychainQuerySynchronizationMode synchronizable NS_AVAILABLE_IOS(7_0); ///< kSecAttrSynchronizable
@end
#pragma mark - export function
/// 重置所有 keychain 数据
FOUNDATION_EXPORT void resetKeyChainAllPassword(void);
/// 在 keychain 中,指定账户名,获取密码
/// @param accout 账户名
FOUNDATION_EXPORT NSString* getKeychainPassword(NSString *accout);
NS_ASSUME_NONNULL_END