Skip to content

Commit 377fa3a

Browse files
error codes and error message for ios plugin
1 parent 60ff098 commit 377fa3a

File tree

3 files changed

+146
-115
lines changed

3 files changed

+146
-115
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-secure-key-store",
3-
"version": "1.5.3",
3+
"version": "1.5.4",
44
"description": "Cordova plugin for securely saving keys, passwords or strings on devices.",
55
"cordova": {
66
"id": "cordova-plugin-secure-key-store",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version='1.0' encoding='utf-8'?>
22
<plugin id="cordova-plugin-secure-key-store"
3-
version="1.5.3"
3+
version="1.5.4"
44
xmlns="http://apache.org/cordova/ns/plugins/1.0"
55
xmlns:android="http://schemas.android.com/apk/res/android">
66

src/ios/SecureKeyStore.m

Lines changed: 144 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Licensed to the Apache Software Foundation (ASF) under one
66
to you under the Apache License, Version 2.0 (the
77
"License"); you may not use this file except in compliance
88
with the License. You may obtain a copy of the License at
9-
9+
1010
http://www.apache.org/licenses/LICENSE-2.0
11-
11+
1212
Unless required by applicable law or agreed to in writing,
1313
software distributed under the License is distributed on an
1414
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -24,49 +24,62 @@ @implementation SecureKeyStore
2424

2525
- (void) writeToSecureKeyStore:(NSMutableDictionary*) dict
2626
{
27-
// get keychain
28-
KeychainItemWrapper * keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil];
29-
NSString *error;
30-
31-
// Serialize dictionary and store in keychain
32-
NSData *serializedDict = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
33-
[keychain setObject:serializedDict forKey:(__bridge id)(kSecValueData)];
34-
if (error) {
35-
NSLog(@"%@", error);
36-
}
27+
// get keychain
28+
KeychainItemWrapper * keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil];
29+
NSString *error;
30+
// Serialize dictionary and store in keychain
31+
NSData *serializedDict = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
32+
[keychain setObject:serializedDict forKey:(__bridge id)(kSecValueData)];
33+
if (error) {
34+
NSLog(@"%@", error);
35+
}
3736
}
3837

3938
- (NSMutableDictionary *) readFromSecureKeyStore
4039
{
41-
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
42-
// get keychain
43-
KeychainItemWrapper * keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil];
44-
NSError *error;
45-
@try
46-
{
47-
NSData *serializedDict = [keychain objectForKey:(__bridge id)(kSecValueData)];
48-
NSUInteger dictLength = [serializedDict length];
49-
if (dictLength) {
50-
// de-serialize dictionary
51-
dict = [NSPropertyListSerialization propertyListFromData:serializedDict mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];
52-
if (error) {
53-
NSLog(@"Read process Exception: %@", error);
54-
}
55-
}
56-
}
57-
@catch (NSException * exception)
58-
{
59-
NSLog(@"Read exception: %@", exception);
60-
}
61-
return dict;
40+
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
41+
// get keychain
42+
KeychainItemWrapper * keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil];
43+
NSError *error;
44+
@try
45+
{
46+
NSData *serializedDict = [keychain objectForKey:(__bridge id)(kSecValueData)];
47+
NSUInteger dictLength = [serializedDict length];
48+
if (dictLength) {
49+
// de-serialize dictionary
50+
dict = [NSPropertyListSerialization propertyListFromData:serializedDict mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];
51+
if (error) {
52+
NSLog(@"Read process Exception: %@", error);
53+
}
54+
}
55+
}
56+
@catch (NSException * exception)
57+
{
58+
NSLog(@"Read exception: %@", exception);
59+
}
60+
return dict;
6261
}
6362

64-
- (void) removeKeyFromSecureKeyStore:(NSString*) key
63+
- (BOOL) removeKeyFromSecureKeyStore:(NSString*) key
6564
{
66-
// get mutable dictionary and remove key from store
67-
NSMutableDictionary *dict = [self readFromSecureKeyStore];
68-
[dict removeObjectForKey:key];
69-
[self writeToSecureKeyStore:dict];
65+
@try
66+
{
67+
// get mutable dictionary and remove key from store
68+
NSMutableDictionary *dict = [self readFromSecureKeyStore];
69+
[dict removeObjectForKey:key];
70+
[self writeToSecureKeyStore:dict];
71+
return YES;
72+
}
73+
@catch (NSException * exception)
74+
{
75+
NSLog(@"Remove exception: %@", exception.reason);
76+
return NO;
77+
}
78+
}
79+
80+
- (void) resetSecureKeyStore
81+
{
82+
[[[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil] resetKeychainItem];
7083
}
7184

7285
- (void)handleAppUninstallation
@@ -78,89 +91,107 @@ - (void)handleAppUninstallation
7891
}
7992
}
8093

81-
- (void) set:(CDVInvokedUrlCommand*)command
82-
{
83-
CDVPluginResult* pluginResult = nil;
84-
NSString* key = [command.arguments objectAtIndex:0];
85-
NSString* value = [command.arguments objectAtIndex:1];
86-
87-
@try {
88-
// handle app uninstallation
89-
[self handleAppUninstallation];
90-
91-
// get mutable dictionary and store data
92-
[self.commandDelegate runInBackground:^{
93-
@synchronized(self) {
94-
NSMutableDictionary *dict = [self readFromSecureKeyStore];
95-
[dict setValue: value forKey: key];
96-
[self writeToSecureKeyStore:dict];
97-
98-
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Key saved to keychain successfully"];
99-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
100-
}
101-
}];
102-
}
103-
@catch (NSException* exception)
104-
{
105-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:exception];
106-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
107-
}
108-
}
109-
110-
- (void) get:(CDVInvokedUrlCommand*)command
94+
- (void) set:(CDVInvokedUrlCommand*)command
11195
{
112-
CDVPluginResult* pluginResult = nil;
113-
NSString* key = [command.arguments objectAtIndex:0];
114-
115-
@try {
116-
// handle app uninstallation
117-
[self handleAppUninstallation];
118-
[self.commandDelegate runInBackground:^{
119-
@synchronized(self) {
120-
// get mutable dictionaly and retrieve store data
121-
NSMutableDictionary *dict = [self readFromSecureKeyStore];
122-
NSString *value = nil;
123-
124-
if (dict != nil) {
125-
value =[dict valueForKey:key];
96+
CDVPluginResult* pluginResult = nil;
97+
NSString* key = [command.arguments objectAtIndex:0];
98+
NSString* value = [command.arguments objectAtIndex:1];
99+
100+
@try {
101+
// handle app uninstallation
102+
[self handleAppUninstallation];
103+
// get mutable dictionary and store data
104+
[self.commandDelegate runInBackground:^{
105+
@synchronized(self) {
106+
@try {
107+
NSMutableDictionary *dict = [self readFromSecureKeyStore];
108+
[dict setValue: value forKey: key];
109+
[self writeToSecureKeyStore:dict];
110+
111+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"key saved successfully"];
112+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
113+
}
114+
@catch (NSException* exception)
115+
{
116+
NSString* errorMessage = [NSString stringWithFormat:@"{\"code\":9,\"message\":\"error saving key, please try to un-install and re-install app again\",\"actual-error\":%@}", exception];
117+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
118+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
119+
}
126120
}
127-
128-
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
129-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
130-
}
131-
}];
132-
}
133-
@catch (NSException* exception)
134-
{
135-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Exception: fetching key from keychain"];
136-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
137-
}
121+
}];
122+
}
123+
@catch (NSException* exception)
124+
{
125+
NSString* errorMessage = [NSString stringWithFormat:@"{\"code\":9,\"message\":\"error saving key\",\"actual-error\":%@}", exception];
126+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
127+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
128+
}
138129
}
139130

140-
- (void) remove:(CDVInvokedUrlCommand*)command
131+
- (void) get:(CDVInvokedUrlCommand*)command
141132
{
142-
CDVPluginResult* pluginResult = nil;
143-
NSString* key = (NSString*)[command.arguments objectAtIndex:0];
144-
@try {
145-
// handle app uninstallation
146-
[self handleAppUninstallation];
147-
[self.commandDelegate runInBackground:^{
148-
@synchronized(self) {
149-
[self removeKeyFromSecureKeyStore:key];
150-
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Key removed successfully"];
151-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
152-
}
153-
}];
154-
}
155-
@catch(NSException *exception) {
156-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Exception: Could not delete key from keychain"];
157-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
158-
}
133+
CDVPluginResult* pluginResult = nil;
134+
NSString* key = [command.arguments objectAtIndex:0];
135+
136+
@try {
137+
// handle app uninstallation
138+
[self handleAppUninstallation];
139+
[self.commandDelegate runInBackground:^{
140+
@synchronized(self) {
141+
// get mutable dictionaly and retrieve store data
142+
NSMutableDictionary *dict = [self readFromSecureKeyStore];
143+
NSString *value = nil;
144+
145+
if (dict != nil) {
146+
value =[dict valueForKey:key];
147+
}
148+
149+
if (value != nil) {
150+
value =[dict valueForKey:key];
151+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
152+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
153+
} else {
154+
NSString* errorMessage = @"{\"code\":1,\"message\":\"key does not present\"}";
155+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
156+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
157+
}
158+
}
159+
}];
160+
}
161+
@catch (NSException* exception)
162+
{
163+
NSString* errorMessage = [NSString stringWithFormat:@"{\"code\":1,\"message\":\"key does not present\",\"actual-error\":%@}", exception];
164+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
165+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
166+
}
159167
}
160168

161-
- (void) resetSecureKeyStore
169+
- (void) remove:(CDVInvokedUrlCommand*)command
162170
{
163-
[[[KeychainItemWrapper alloc] initWithIdentifier:@"cordova.plugins.SecureKeyStore" accessGroup:nil] resetKeychainItem];
171+
CDVPluginResult* pluginResult = nil;
172+
NSString* key = (NSString*)[command.arguments objectAtIndex:0];
173+
@try {
174+
// handle app uninstallation
175+
[self handleAppUninstallation];
176+
[self.commandDelegate runInBackground:^{
177+
@synchronized(self) {
178+
BOOL status = [self removeKeyFromSecureKeyStore:key];
179+
if (status) {
180+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Key removed successfully"];
181+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
182+
} else {
183+
NSString* errorMessage = @"{\"code\":6,\"message\":\"could not delete key\"}";
184+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
185+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
186+
}
187+
}
188+
}];
189+
}
190+
@catch(NSException *exception) {
191+
NSString* errorMessage = [NSString stringWithFormat:@"{\"code\":6,\"message\":\"could not delete key\",\"actual-error\":%@}", exception];
192+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
193+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
194+
}
164195
}
165196

166197
@end

0 commit comments

Comments
 (0)