Add response deserialization
[platform/core/security/cynara.git] / src / service / logic / Logic.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        Logic.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @version     1.0
20  * @brief       This file implements main class of logic layer in cynara service
21  */
22
23 #include <log/log.h>
24 #include <common.h>
25 #include <exceptions/PluginNotFoundException.h>
26
27 #include <main/Cynara.h>
28 #include <request/CheckRequest.h>
29 #include <request/RequestContext.h>
30 #include <response/CheckResponse.h>
31 #include <storage/Storage.h>
32
33 #include "Logic.h"
34
35 namespace Cynara {
36 Logic::Logic() {
37 }
38
39 Logic::~Logic() {
40 }
41
42 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
43     PolicyResult result(PredefinedPolicyType::DENY);
44     if (check(context, request->key(), result)) {
45         context->returnResponse(context, std::make_shared<CheckResponse>(result, request->sequenceNumber()));
46     }
47 }
48
49 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
50                   PolicyResult& result) {
51     result = m_storage->checkPolicy(key);
52
53     switch (result.policyType()) {
54         case PredefinedPolicyType::ALLOW :
55             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
56             return true;
57         case PredefinedPolicyType::DENY :
58             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
59             return true;
60     }
61     //todo pass question to proper plugin that:
62     //  1) returns false when answer has to be waited for (UI)
63     //  2) returns true if Response is to be generated
64     // In case 1) context should be saved in plugin in order to return answer when ready.
65
66     //in case no proper plugin is found
67     throw PluginNotFoundException(result);
68     return false;
69 }
70
71 } // namespace Cynara