/src/helpers/creds-commons/creds-commons.cpp

https://gitlab.com/github-cloud-corporation/cynara · C++ · 81 lines · 46 code · 11 blank · 24 comment · 12 complexity · 227151ac615b04f473e5e1ddf448329b MD5 · raw file

  1. /*
  2. * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/helpers/creds-commons/creds-commons.cpp
  18. * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  19. * @author Radoslaw Bartosiak <r.bartosiak@samsung.com>
  20. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  21. * @author Jacek Bukarewicz <j.bukarewicz@samsung.com>
  22. * @version 1.1
  23. * @brief Implementation of external libcynara-creds-commons API
  24. */
  25. #include <fstream>
  26. #include <attributes/attributes.h>
  27. #include <exceptions/TryCatch.h>
  28. #include <cynara-creds-commons.h>
  29. #include <cynara-error.h>
  30. #include "CredsCommonsInner.h"
  31. CYNARA_API
  32. int cynara_creds_get_default_client_method(enum cynara_client_creds *method) {
  33. static int cachedMethodCode = -1;
  34. static const Cynara::CredentialsMap clientCredsMap{{"smack", CLIENT_METHOD_SMACK},
  35. {"pid", CLIENT_METHOD_PID}};
  36. if (cachedMethodCode == -1) {
  37. int ret = Cynara::tryCatch([&] () {
  38. std::ifstream f;
  39. int r = Cynara::CredsCommonsInnerBackend::credsConfigurationFile(f);
  40. if (r != CYNARA_API_SUCCESS)
  41. return r;
  42. return Cynara::CredsCommonsInnerBackend::getMethodFromConfigurationFile(
  43. f, clientCredsMap, "client_default", cachedMethodCode);
  44. });
  45. if (ret != CYNARA_API_SUCCESS)
  46. return ret;
  47. }
  48. *method = static_cast<enum cynara_client_creds>(cachedMethodCode);
  49. return CYNARA_API_SUCCESS;
  50. }
  51. CYNARA_API
  52. int cynara_creds_get_default_user_method(enum cynara_user_creds *method) {
  53. static int cachedMethodCode = -1;
  54. static const Cynara::CredentialsMap userCredsMap{{"uid", USER_METHOD_UID},
  55. {"gid", USER_METHOD_GID}};
  56. if (cachedMethodCode == -1) {
  57. int ret = Cynara::tryCatch([&] () {
  58. std::ifstream f;
  59. int r = Cynara::CredsCommonsInnerBackend::credsConfigurationFile(f);
  60. if (r != CYNARA_API_SUCCESS)
  61. return r;
  62. return Cynara::CredsCommonsInnerBackend::getMethodFromConfigurationFile(
  63. f, userCredsMap, "user_default", cachedMethodCode);
  64. });
  65. if (ret != CYNARA_API_SUCCESS)
  66. return ret;
  67. }
  68. *method = static_cast<enum cynara_user_creds>(cachedMethodCode);
  69. return CYNARA_API_SUCCESS;
  70. }