CDVKeychain.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVKeychain.h"
  18. #import "SFHFKeychainUtils.h"
  19. @implementation CDVKeychain
  20. - (CDVPlugin*) initWithWebView:(UIWebView*)theWebView
  21. {
  22. self = (CDVKeychain*)[super initWithWebView:(UIWebView*)theWebView];
  23. if (self) {
  24. // initialization here
  25. }
  26. return self;
  27. }
  28. - (void) getForKey:(CDVInvokedUrlCommand*)command
  29. {
  30. [self.commandDelegate runInBackground:^{
  31. NSArray* arguments = command.arguments;
  32. CDVPluginResult* pluginResult = nil;
  33. if ([arguments count] >= 2)
  34. {
  35. NSString* key = [arguments objectAtIndex:0];
  36. NSString* serviceName = [arguments objectAtIndex:1];
  37. NSError* error = nil;
  38. NSString* value = [SFHFKeychainUtils getPasswordForUsername:key andServiceName:serviceName error:&error];
  39. if (error == nil && value != nil) {
  40. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
  41. } else {
  42. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  43. messageAsString:[NSString stringWithFormat:@"error retrieving value for key '%@' : %@", key, [error localizedDescription]]];
  44. }
  45. }
  46. else
  47. {
  48. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  49. messageAsString:@"incorrect number of arguments for getForkey"];
  50. }
  51. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  52. }];
  53. }
  54. - (void) setForKey:(CDVInvokedUrlCommand*)command
  55. {
  56. [self.commandDelegate runInBackground:^{
  57. NSArray* arguments = command.arguments;
  58. CDVPluginResult* pluginResult = nil;
  59. if ([arguments count] >= 3)
  60. {
  61. NSString* key = [arguments objectAtIndex:0];
  62. NSString* serviceName = [arguments objectAtIndex:1];
  63. NSString* value = [arguments objectAtIndex:2];
  64. NSError* error = nil;
  65. BOOL stored = [SFHFKeychainUtils storeUsername:key andPassword:value forServiceName:serviceName updateExisting:YES error:&error];
  66. if (stored && error == nil) {
  67. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  68. } else {
  69. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
  70. }
  71. }
  72. else
  73. {
  74. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  75. messageAsString:@"incorrect number of arguments for setForKey"];
  76. }
  77. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  78. }];
  79. }
  80. - (void) removeForKey:(CDVInvokedUrlCommand*)command
  81. {
  82. [self.commandDelegate runInBackground:^{
  83. NSArray* arguments = command.arguments;
  84. CDVPluginResult* pluginResult = nil;
  85. if ([arguments count] >= 2)
  86. {
  87. NSString* key = [arguments objectAtIndex:0];
  88. NSString* serviceName = [arguments objectAtIndex:1];
  89. NSError* error = nil;
  90. BOOL deleted = [SFHFKeychainUtils deleteItemForUsername:key andServiceName:serviceName error:&error];
  91. if (deleted && error == nil) {
  92. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  93. } else {
  94. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
  95. }
  96. }
  97. else
  98. {
  99. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
  100. messageAsString:@"incorrect number of arguments for removeForKey"];
  101. }
  102. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  103. }];
  104. }
  105. @end