6ca7def2563e0106967f22c0bd543d74636ea8b3
[platform/core/security/cynara.git] / src / client / api / client-api.cpp
1 /*
2  *  Copyright (c) 2014 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  * @version     1.0
20  * @brief       Implementation of external libcynara-client API
21  */
22
23 #include <new>
24
25 #include <common.h>
26
27 #include <exceptions/TryCatch.h>
28 #include <log/log.h>
29
30 #include <cynara-client.h>
31 #include <cynara-client-error.h>
32 #include <api/ApiInterface.h>
33 #include <logic/Logic.h>
34
35 struct cynara {
36     Cynara::ApiInterface *impl;
37
38     cynara(Cynara::ApiInterface *_impl) : impl(_impl) {
39     }
40     ~cynara() {
41         delete impl;
42     }
43 };
44
45 CYNARA_API
46 int cynara_initialize(cynara **pp_cynara, const cynara_configuration *p_conf UNUSED)
47 {
48     if (!pp_cynara)
49         return CYNARA_API_INVALID_PARAM;
50
51     return Cynara::tryCatch([&]() {
52         *pp_cynara = new cynara(new Cynara::Logic);
53
54         init_log();
55
56         LOGD("Cynara client initialized");
57
58         return CYNARA_API_SUCCESS;
59     });
60 }
61
62 CYNARA_API
63 int cynara_finish(cynara *p_cynara)
64 {
65     delete p_cynara;
66
67     return CYNARA_API_SUCCESS;
68 }
69
70 CYNARA_API
71 int cynara_check(cynara *p_cynara, const char *client, const char *client_session, const char *user,
72     const char *privilege)
73 {
74     if(!p_cynara || !p_cynara->impl)
75         return CYNARA_API_INVALID_PARAM;
76     if(!client || !client_session || !user || !privilege)
77         return CYNARA_API_INVALID_PARAM;
78
79     return Cynara::tryCatch([&]() {
80         std::string clientStr;
81         std::string clientSessionStr;
82         std::string userStr;
83         std::string privilegeStr;
84
85         try {
86             clientStr = client;
87             clientSessionStr = client_session;
88             userStr = user;
89             privilegeStr = privilege;
90         } catch (const std::length_error &e) {
91             LOGE(e.what());
92             return CYNARA_API_INVALID_PARAM;
93         }
94         return p_cynara->impl->check(clientStr, clientSessionStr, userStr, privilegeStr);
95     });
96 }