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

/src/client/api/client-api.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 177 lines | 128 code | 26 blank | 23 comment | 19 complexity | a8b0851d7f129c57bbd479eb09590c55 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/client/api/client-api.cpp
  18. * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  19. * @author Zofia Abramowska <z.abramowska@samsung.com>
  20. * @author Oskar Świtalski <o.switalski@samsung.com>
  21. * @version 1.0
  22. * @brief Implementation of external libcynara-client API
  23. */
  24. #include <new>
  25. #include <common.h>
  26. #include <exceptions/TryCatch.h>
  27. #include <log/log.h>
  28. #include <types/string_validation.h>
  29. #include <configuration/Configuration.h>
  30. #include <cynara-client.h>
  31. #include <cynara-error.h>
  32. #include <api/ApiInterface.h>
  33. #include <logic/Logic.h>
  34. struct cynara {
  35. Cynara::ApiInterface *impl;
  36. cynara(Cynara::ApiInterface *_impl) : impl(_impl) {
  37. }
  38. ~cynara() {
  39. delete impl;
  40. }
  41. };
  42. struct cynara_configuration {
  43. Cynara::Configuration* impl;
  44. cynara_configuration(Cynara::Configuration *_impl) : impl(_impl) {}
  45. ~cynara_configuration() {
  46. delete impl;
  47. }
  48. };
  49. CYNARA_API
  50. int cynara_configuration_create(cynara_configuration **pp_conf) {
  51. if (!pp_conf)
  52. return CYNARA_API_INVALID_PARAM;
  53. return Cynara::tryCatch([&]() {
  54. Cynara::ConfigurationUniquePtr ptr(new Cynara::Configuration());
  55. *pp_conf = new cynara_configuration(ptr.get());
  56. ptr.release();
  57. LOGD("Cynara configuration initialized");
  58. return CYNARA_API_SUCCESS;
  59. });
  60. }
  61. CYNARA_API
  62. void cynara_configuration_destroy(cynara_configuration *p_conf) {
  63. delete p_conf;
  64. }
  65. CYNARA_API
  66. int cynara_configuration_set_cache_size(cynara_configuration *p_conf, size_t cache_size) {
  67. if (!p_conf || !p_conf->impl)
  68. return CYNARA_API_INVALID_PARAM;
  69. return Cynara::tryCatch([&]() {
  70. p_conf->impl->setCacheSize(cache_size);
  71. return CYNARA_API_SUCCESS;
  72. });
  73. }
  74. CYNARA_API
  75. int cynara_initialize(cynara **pp_cynara, const cynara_configuration *p_conf)
  76. {
  77. if (!pp_cynara)
  78. return CYNARA_API_INVALID_PARAM;
  79. init_log();
  80. return Cynara::tryCatch([&]() {
  81. Cynara::LogicUniquePtr ptr;
  82. if (p_conf && p_conf->impl) {
  83. ptr.reset(new Cynara::Logic(*(p_conf->impl)));
  84. } else {
  85. ptr.reset(new Cynara::Logic());
  86. }
  87. *pp_cynara = new cynara(ptr.get());
  88. ptr.release();
  89. LOGD("Cynara client initialized");
  90. return CYNARA_API_SUCCESS;
  91. });
  92. }
  93. CYNARA_API
  94. int cynara_finish(cynara *p_cynara)
  95. {
  96. delete p_cynara;
  97. return CYNARA_API_SUCCESS;
  98. }
  99. CYNARA_API
  100. int cynara_check(cynara *p_cynara, const char *client, const char *client_session, const char *user,
  101. const char *privilege)
  102. {
  103. if (!p_cynara || !p_cynara->impl)
  104. return CYNARA_API_INVALID_PARAM;
  105. if (!isStringValid(client) || !isStringValid(client_session))
  106. return CYNARA_API_INVALID_PARAM;
  107. if (!isStringValid(user) || !isStringValid(privilege))
  108. return CYNARA_API_INVALID_PARAM;
  109. return Cynara::tryCatch([&]() {
  110. std::string clientStr;
  111. std::string clientSessionStr;
  112. std::string userStr;
  113. std::string privilegeStr;
  114. try {
  115. clientStr = client;
  116. clientSessionStr = client_session;
  117. userStr = user;
  118. privilegeStr = privilege;
  119. } catch (const std::length_error &e) {
  120. LOGE("%s", e.what());
  121. return CYNARA_API_INVALID_PARAM;
  122. }
  123. return p_cynara->impl->check(clientStr, clientSessionStr, userStr, privilegeStr);
  124. });
  125. }
  126. CYNARA_API
  127. int cynara_simple_check(cynara *p_cynara, const char *client, const char *client_session,
  128. const char *user, const char *privilege) {
  129. if (!p_cynara || !p_cynara->impl)
  130. return CYNARA_API_INVALID_PARAM;
  131. if (!isStringValid(client) || !isStringValid(client_session))
  132. return CYNARA_API_INVALID_PARAM;
  133. if (!isStringValid(user) || !isStringValid(privilege))
  134. return CYNARA_API_INVALID_PARAM;
  135. return Cynara::tryCatch([&]() {
  136. std::string clientStr;
  137. std::string clientSessionStr;
  138. std::string userStr;
  139. std::string privilegeStr;
  140. try {
  141. clientStr = client;
  142. clientSessionStr = client_session;
  143. userStr = user;
  144. privilegeStr = privilege;
  145. } catch (const std::length_error &e) {
  146. LOGE("%s", e.what());
  147. return CYNARA_API_INVALID_PARAM;
  148. }
  149. return p_cynara->impl->simpleCheck(clientStr, clientSessionStr, userStr, privilegeStr);
  150. });
  151. }