diff --git a/README.md b/README.md index d6cb1c6..566880c 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ public class MainActivity extends BridgeActivity { ```ts -get(options: { key: string }): Promise<{ value: string }> +get(options: { serviceName?: string, key: string }): Promise<{ value: string }> ``` @@ -92,7 +92,7 @@ get(options: { key: string }): Promise<{ value: string }> ```ts -set(options: { key: string; value: string }): Promise<{ value: boolean }> +set(options: { serviceName?: string, key: string; value: string }): Promise<{ value: boolean }> ``` @@ -103,7 +103,7 @@ set(options: { key: string; value: string }): Promise<{ value: boolean }> ```ts -remove(options: { key: string }): Promise<{ value: boolean }> +remove(options: { serviceName?: string, key: string }): Promise<{ value: boolean }> ``` @@ -113,14 +113,16 @@ remove(options: { key: string }): Promise<{ value: boolean }> --- ```ts -keys(): Promise<{ value: string[] }> + +keys(options: { serviceName?: string }): Promise<{ value: string[] }> + ``` --- ```ts - clear(): Promise<{ value: boolean }> +clear(options: { serviceName?: string }): Promise<{ value: boolean }> ``` diff --git a/ios/Plugin/SecureStoragePluginPlugin.swift b/ios/Plugin/SecureStoragePluginPlugin.swift index a0eb0fe..d7c3ffd 100644 --- a/ios/Plugin/SecureStoragePluginPlugin.swift +++ b/ios/Plugin/SecureStoragePluginPlugin.swift @@ -7,14 +7,20 @@ import SwiftKeychainWrapper * here: https://capacitor.ionicframework.com/docs/plugins/ios */ @objc(SecureStoragePlugin) -public class SecureStoragePlugin: CAPPlugin { - var keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: "cap_sec") - +public class SecureStoragePlugin: CAPPlugin { + + struct Constants { + static let serviceName = "cap_sec" + } + @objc func set(_ call: CAPPluginCall) { + let serviceName = call.getString("serviceName") ?? Constants.serviceName + let keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: serviceName) + let key = call.getString("key") ?? "" let value = call.getString("value") ?? "" let saveSuccessful: Bool = keychainwrapper.set(value, forKey: key, withAccessibility: .afterFirstUnlock) - if(saveSuccessful) { + if(saveSuccessful) { call.resolve([ "value": saveSuccessful ]) @@ -25,6 +31,9 @@ public class SecureStoragePlugin: CAPPlugin { } @objc func get(_ call: CAPPluginCall) { + let serviceName = call.getString("serviceName") ?? Constants.serviceName + let keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: serviceName) + let key = call.getString("key") ?? "" let hasValueDedicated = keychainwrapper.hasValue(forKey: key) let hasValueStandard = KeychainWrapper.standard.hasValue(forKey: key) @@ -53,6 +62,9 @@ public class SecureStoragePlugin: CAPPlugin { } @objc func keys(_ call: CAPPluginCall) { + let serviceName = call.getString("serviceName") ?? Constants.serviceName + let keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: serviceName) + let keys = keychainwrapper.allKeys(); call.resolve([ "value": Array(keys) @@ -60,6 +72,9 @@ public class SecureStoragePlugin: CAPPlugin { } @objc func remove(_ call: CAPPluginCall) { + let serviceName = call.getString("serviceName") ?? Constants.serviceName + let keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: serviceName) + let key = call.getString("key") ?? "" let hasValueDedicated = keychainwrapper.hasValue(forKey: key) let hasValueStandard = KeychainWrapper.standard.hasValue(forKey: key) @@ -82,6 +97,9 @@ public class SecureStoragePlugin: CAPPlugin { } @objc func clear(_ call: CAPPluginCall) { + let serviceName = call.getString("serviceName") ?? Constants.serviceName + let keychainwrapper: KeychainWrapper = KeychainWrapper.init(serviceName: serviceName) + let keys = keychainwrapper.allKeys(); // cleanup standard keychain wrapper keys diff --git a/src/definitions.ts b/src/definitions.ts index 7a675f7..edc016e 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -1,8 +1,8 @@ export interface SecureStoragePluginPlugin { - get(options: { key: string }): Promise<{ value: string }>; - set(options: { key: string; value: string }): Promise<{ value: boolean }>; - remove(options: { key: string }): Promise<{ value: boolean }>; - clear(): Promise<{ value: boolean }>; - keys(): Promise<{ value: string[] }>; + get(options: { serviceName?: string, key: string }): Promise<{ value: string }>; + set(options: { serviceName?: string, key: string; value: string }): Promise<{ value: boolean }>; + remove(options: { serviceName?: string, key: string }): Promise<{ value: boolean }>; + clear(options: { serviceName?: string }): Promise<{ value: boolean }>; + keys(options: { serviceName?: string }): Promise<{ value: string[] }>; getPlatform(): Promise<{ value: string }>; }