PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/github-cloud-corporation/cynara
C++ | 128 lines | 88 code | 14 blank | 26 comment | 14 complexity | 601b0ac295a3319c2a36cf1c8b485aa9 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/CredsCommonsInner.cpp
  18. * @author Radoslaw Bartosiak <r.bartosiak@samsung.com>
  19. * @version 1.1
  20. * @brief Implementation of internal credential commons functions
  21. */
  22. #include <algorithm>
  23. #include <cctype>
  24. #include <fstream>
  25. #include <functional>
  26. #include <fstream>
  27. #include <sstream>
  28. #include <string>
  29. #include <log/log.h>
  30. #include <config/PathConfig.h>
  31. #include <cynara-creds-commons.h>
  32. #include <cynara-error.h>
  33. #include <CredsCommonsInner.h>
  34. namespace Cynara {
  35. int CredsCommonsInnerBackend::credsConfigurationFile(std::ifstream &f) {
  36. f.open(Cynara::PathConfig::confPath + "/creds.conf", std::fstream::in);
  37. if (!f.is_open())
  38. return CYNARA_API_CONFIGURATION_ERROR;
  39. return CYNARA_API_SUCCESS;
  40. }
  41. // parses stream with configuration skipping comments (from # till the end of line)
  42. // untill a line of form <non empty key>=<empty or no empty value>
  43. // is found. Returns true when key and value FOUND, false when no more lines
  44. bool CredsCommonsInnerBackend::getKeyAndValue(std::istream &f, const std::locale &loc,
  45. std::string &key, std::string &value) {
  46. std::string rawLine;
  47. while (std::getline(f, rawLine)) {
  48. std::istringstream rawLineStream(rawLine);
  49. std::string uncommentedLine;
  50. if (std::getline(rawLineStream, uncommentedLine, '#')) {
  51. size_t found = uncommentedLine.find_first_of("=");
  52. if (found != std::string::npos) {
  53. std::string tempKey = uncommentedLine.substr(0, found);
  54. std::string tempValue = uncommentedLine.substr(found + 1);
  55. if (!tempKey.empty()) {
  56. key = normalizeString(tempKey, loc);
  57. value = normalizeString(tempValue, loc);
  58. return true;
  59. }
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. bool CredsCommonsInnerBackend::interpretValue(const CredentialsMap &methodCodeMap, int &method,
  66. const std::string &value, bool &occurred) {
  67. if (occurred) //two entries in conf file are treated as error
  68. return false;
  69. occurred = true;
  70. auto it = methodCodeMap.find(value);
  71. if (it == methodCodeMap.end()) //value is not valid
  72. return false;
  73. method = it->second;
  74. return true;
  75. }
  76. int CredsCommonsInnerBackend::getMethodFromConfigurationFile(std::istream &f,
  77. const CredentialsMap &methodCodeMap,
  78. const std::string &methodName,
  79. int &method) {
  80. std::locale loc = f.getloc();
  81. bool occurred = false;
  82. std::string key, value;
  83. int tmpMethod;
  84. while (getKeyAndValue(f, loc, key, value))
  85. if (key == methodName && !interpretValue(methodCodeMap, tmpMethod, value, occurred))
  86. return CYNARA_API_CONFIGURATION_ERROR;
  87. if (occurred) {
  88. method = tmpMethod;
  89. return CYNARA_API_SUCCESS;
  90. }
  91. return CYNARA_API_CONFIGURATION_ERROR;
  92. }
  93. // trim from the start
  94. inline std::string &CredsCommonsInnerBackend::ltrim(std::string &s, const std::locale &loc) {
  95. s.erase(s.begin(), std::find_if(s.begin(), s.end(),
  96. [&loc](char c) { return not(std::isspace(c, loc)); }));
  97. return s;
  98. }
  99. // trim from the end
  100. inline std::string &CredsCommonsInnerBackend::rtrim(std::string &s, const std::locale &loc) {
  101. s.erase(std::find_if(s.rbegin(), s.rend(),
  102. [&loc](char c) { return not(std::isspace(c, loc)); }).base(),
  103. s.end());
  104. return s;
  105. }
  106. inline std::string &CredsCommonsInnerBackend::toLower(std::string &s, const std::locale &loc) {
  107. std::transform(s.begin(), s.end(), s.begin(), [&loc](char c) { return std::tolower(c, loc); });
  108. return s;
  109. }
  110. inline std::string &CredsCommonsInnerBackend::normalizeString(std::string &s,
  111. const std::locale &loc) {
  112. return toLower(ltrim(rtrim(s, loc), loc), loc);
  113. }
  114. } // namespace Cynara