-
Notifications
You must be signed in to change notification settings - Fork 51
/
SecureStoragePluginPlugin.swift
121 lines (109 loc) · 3.92 KB
/
SecureStoragePluginPlugin.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Foundation
import Capacitor
import SwiftKeychainWrapper
/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitor.ionicframework.com/docs/plugins/ios
*/
@objc(SecureStoragePlugin)
public class SecureStoragePlugin: CAPPlugin {
var keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: "cap_sec")
@objc func set(_ call: CAPPluginCall) {
let key = call.getString("key") ?? ""
let value = call.getString("value") ?? ""
let saveSuccessful: Bool = keychainwrapper.set(value, forKey: key, withAccessibility: .afterFirstUnlock)
if(saveSuccessful) {
call.resolve([
"value": saveSuccessful
])
}
else {
call.reject("error")
}
}
@objc func get(_ call: CAPPluginCall) {
let key = call.getString("key") ?? ""
let hasValueDedicated = keychainwrapper.hasValue(forKey: key)
let hasValueStandard = KeychainWrapper.standard.hasValue(forKey: key)
// copy standard value to dedicated and remove standard key
if (hasValueStandard && !hasValueDedicated) {
let syncValueSuccessful: Bool = keychainwrapper.set(
KeychainWrapper.standard.string(forKey: key) ?? "",
forKey: key,
withAccessibility: .afterFirstUnlock
)
let removeValueSuccessful: Bool = KeychainWrapper.standard.removeObject(forKey: key)
if (!syncValueSuccessful || !removeValueSuccessful) {
call.reject("error")
}
}
if(hasValueDedicated || hasValueStandard) {
call.resolve([
"value": keychainwrapper.string(forKey: key) ?? ""
])
}
else {
call.reject("Item with given key does not exist")
}
}
@objc func keys(_ call: CAPPluginCall) {
let keys = keychainwrapper.allKeys();
call.resolve([
"value": Array(keys)
])
}
@objc func remove(_ call: CAPPluginCall) {
let key = call.getString("key") ?? ""
let hasValueDedicated = keychainwrapper.hasValue(forKey: key)
let hasValueStandard = KeychainWrapper.standard.hasValue(forKey: key)
if(hasValueDedicated || hasValueStandard) {
KeychainWrapper.standard.removeObject(forKey: key);
let removeDedicatedSuccessful: Bool = keychainwrapper.removeObject(forKey: key)
if(removeDedicatedSuccessful) {
call.resolve([
"value": removeDedicatedSuccessful
])
}
else {
call.reject("Remove failed")
}
}
else {
call.reject("Item with given key does not exist")
}
}
@objc func clear(_ call: CAPPluginCall) {
let keys = keychainwrapper.allKeys();
// cleanup standard keychain wrapper keys
if(keys.count == 0) {
call.resolve([
"value": true
])
}
else {
for key in keys {
let hasValueStandard = KeychainWrapper.standard.hasValue(forKey: key)
if (hasValueStandard) {
let removeStandardSuccessful = KeychainWrapper.standard.removeObject(forKey: key)
if (!removeStandardSuccessful) {
call.reject("error")
}
}
}
let clearSuccessful: Bool = keychainwrapper.removeAllKeys()
if(clearSuccessful) {
call.resolve([
"value": clearSuccessful
])
}
else {
call.reject("error")
}
}
}
@objc func getPlatform(_ call: CAPPluginCall) {
call.resolve([
"value": "ios"
])
}
}