-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecureStorage.swift
105 lines (81 loc) · 3.2 KB
/
SecureStorage.swift
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
//
// SecureStorage.swift
// GoogleServices
//
// Created by Julian Parkfy on 16/05/2018.
// Copyright © 2018 Julian. All rights reserved.
//
import Foundation
protocol SecureStorable: Codable {
}
protocol SecureStorage {
@discardableResult func set<T>(value: T, key: String) -> Bool where T: SecureStorable
func get<T>(_ key: String) -> T? where T: SecureStorable
@discardableResult func clear() -> Bool
}
final class SecureStorageImpl: SecureStorage {
@discardableResult func set<T>(value: T, key: String) -> Bool where T: SecureStorable {
let data = try! JSONEncoder().encode(value)
return self.set(data, key: key)
}
func get<T>(_ key: String) -> T? where T: SecureStorable {
let data = self.getData(key)
if let data = data {
let object = try! JSONDecoder().decode(T.self, from: data)
return object
}
return nil
}
@discardableResult func clear() -> Bool {
let query: [String: Any] = [
SecureStorageOptions.SecureClass : kSecClassGenericPassword,
]
let result = SecItemDelete(query as CFDictionary)
return result == noErr
}
//MARK: - Private
private func set(_ value: Data, key: String, access: SecureStorageAccess = .accessibleWhenUnlockedThisDeviceOnly) -> Bool {
let _ = self.delete(key)
let query: [String: Any] = [
SecureStorageOptions.SecureClass : kSecClassGenericPassword,
SecureStorageOptions.ValueKey : key,
SecureStorageOptions.ValueData : value,
SecureStorageOptions.Accesible : access.value,
]
let result = SecItemAdd(query as CFDictionary, nil)
return result == noErr
}
private func getData(_ key: String) -> Data? {
let query: [String: Any] = [
SecureStorageOptions.SecureClass : kSecClassGenericPassword,
SecureStorageOptions.ValueKey : key,
SecureStorageOptions.ReturnData : kCFBooleanTrue,
SecureStorageOptions.MatchLimit : kSecMatchLimitOne,
]
var result: AnyObject?
let resultCode = withUnsafePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer(mutating: $0))
}
if resultCode == noErr {
return result as? Data
}
return nil
}
private func delete(_ key: String) -> Bool {
let query: [String: Any] = [
SecureStorageOptions.SecureClass : kSecClassGenericPassword,
SecureStorageOptions.ValueKey : key,
]
let result = SecItemDelete(query as CFDictionary)
return result == noErr
}
//MARK: - Constants
private struct SecureStorageOptions {
static let SecureClass = kSecClass as String
static let ValueData = kSecValueData as String
static let ValueKey = kSecAttrAccount as String
static let Accesible = kSecAttrAccessible as String
static let ReturnData = kSecReturnData as String
static let MatchLimit = kSecMatchLimit as String
}
}