Cache default method types for obtaining peer credentials
[platform/core/security/cynara.git] / src / helpers / creds-commons / creds-commons.cpp
index 390f2ea..30a24e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  *  See the License for the specific language governing permissions and
  *  limitations under the License
  */
-/*
- * @file        creds-commons.cpp
+/**
+ * @file        src/helpers/creds-commons/creds-commons.cpp
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
- * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @author      Jacek Bukarewicz <j.bukarewicz@samsung.com>
  * @version     1.0
  * @brief       Implementation of external libcynara-creds-commons API
  */
 
 #include <attributes/attributes.h>
+#include <exceptions/TryCatch.h>
 
-#include <cynara-client-error.h>
 #include <cynara-creds-commons.h>
+#include <cynara-error.h>
+
+#include "CredsCommonsInner.h"
 
 CYNARA_API
 int cynara_creds_get_default_client_method(enum cynara_client_creds *method) {
-    //todo read from proper file and parse
+    static int cachedMethodCode = -1;
+    static const Cynara::CredentialsMap clientCredsMap{{"smack", CLIENT_METHOD_SMACK},
+                                                       {"pid", CLIENT_METHOD_PID}};
 
-    *method = CLIENT_METHOD_SMACK;
+    if (cachedMethodCode == -1) {
+        int ret = Cynara::tryCatch([&] () {
+            return Cynara::CredsCommonsInnerBackend::
+                      getMethodFromConfigurationFile(clientCredsMap, "client_default",
+                                                     cachedMethodCode);
+        });
+        if (ret != CYNARA_API_SUCCESS)
+            return ret;
+    }
+    *method = static_cast<enum cynara_client_creds>(cachedMethodCode);
     return CYNARA_API_SUCCESS;
+
 }
 
 CYNARA_API
 int cynara_creds_get_default_user_method(enum cynara_user_creds *method) {
-    //todo read from proper file and parse
+    static int cachedMethodCode = -1;
+    static const Cynara::CredentialsMap userCredsMap{{"uid", USER_METHOD_UID},
+                                                     {"gid", USER_METHOD_GID}};
+
+    if (cachedMethodCode == -1) {
+        int ret = Cynara::tryCatch([&] () {
+            return Cynara::CredsCommonsInnerBackend::
+                                       getMethodFromConfigurationFile(userCredsMap,
+                                                                      "user_default",
+                                                                      cachedMethodCode);
+        });
+        if (ret != CYNARA_API_SUCCESS)
+            return ret;
+    }
 
-    *method = USER_METHOD_UID;
+    *method = static_cast<enum cynara_user_creds>(cachedMethodCode);
     return CYNARA_API_SUCCESS;
 }