forked from rdkcentral/RDKShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.cpp
201 lines (181 loc) · 7.47 KB
/
permissions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "permissions.h"
#include "rdkshelljson.h"
#include "logger.h"
#include <sstream>
#include <map>
#include <algorithm>
namespace
{
std::map<std::string, std::vector<std::string>> gClientsPermittedExtensions;
std::vector<std::string> gClientsDefaultPermittedExtensions;
std::string gDefaultRenderer;
}
namespace RdkShell
{
void readPermissionsConfiguration()
{
const char* configFilePath = getenv("RDKSHELL_CLIENT_PERMISSIONS_CONFIG");
if (configFilePath == nullptr)
{
Logger::log(LogLevel::Warn, "%s: RDKSHELL_CLIENT_PERMISSIONS_CONFIG not specified\n", __func__);
return;
}
Logger::log(LogLevel::Information, "Reading Permissions config from: %s", configFilePath);
rapidjson::Document document;
bool ret = RdkShellJson::readJsonFile(configFilePath, document);
if (ret == false)
{
Logger::log(LogLevel::Error, "%s: unabled to read permission configuration file: '%s'\n", __func__, configFilePath);
return;
}
if (document.HasMember("clients"))
{
const rapidjson::Value& clients = document["clients"];
if (clients.IsArray())
{
for (rapidjson::SizeType i = 0; i < clients.Size(); ++i)
{
const rapidjson::Value& entry = clients[i];
if (entry.IsObject() &&
entry.HasMember("client") &&
entry.HasMember("extensions"))
{
const rapidjson::Value& extensionsValue = entry["extensions"];
if (extensionsValue.IsArray())
{
std::vector<std::string> entryExtensions;
for (rapidjson::SizeType j = 0; j < extensionsValue.Size(); ++j)
{
const rapidjson::Value& extesionValue = extensionsValue[j];
if (extesionValue.IsString())
{
entryExtensions.push_back(extesionValue.GetString());
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect extensions\n", __func__);
continue;
}
}
const rapidjson::Value& clientValue = entry["client"];
if (clientValue.IsString())
{
std::string entryClient = clientValue.GetString();
std::transform(entryClient.begin(), entryClient.end(), entryClient.begin(),
[](unsigned char c){ return std::tolower(c); });
if (gClientsPermittedExtensions.count(entryClient) == 0)
{
gClientsPermittedExtensions[entryClient] = entryExtensions;
}
else
{
Logger::log(LogLevel::Error, "%s: multiple entries for the same client\n", __func__);
continue;
}
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect client\n", __func__);
continue;
}
}
else
{
Logger::log(LogLevel::Error, "%s: extensions entry not an array\n", __func__);
continue;
}
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect config structure\n", __func__);
continue;
}
}
}
else
{
Logger::log(LogLevel::Error, "%s: \"clients\" not an array\n", __func__);
}
}
if (document.HasMember("default"))
{
const rapidjson::Value& defaultValue = document["default"];
if (defaultValue.IsObject())
{
if (defaultValue.HasMember("extensions"))
{
const rapidjson::Value& extensionsValue = defaultValue["extensions"];
if (extensionsValue.IsArray())
{
for (rapidjson::SizeType j = 0; j < extensionsValue.Size(); ++j)
{
const rapidjson::Value& extesionValue = extensionsValue[j];
if (extesionValue.IsString())
{
gClientsDefaultPermittedExtensions.push_back(extesionValue.GetString());
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect extensions\n", __func__);
continue;
}
}
}
else
{
Logger::log(LogLevel::Error, "%s: extensions entry not an array\n", __func__);
}
}
if (defaultValue.HasMember("renderer"))
{
const rapidjson::Value& defaultRenderer = defaultValue["renderer"];
if (defaultRenderer.IsString())
{
gDefaultRenderer = defaultRenderer.GetString();
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect default renderer\n", __func__);
}
}
}
else
{
Logger::log(LogLevel::Error, "%s: incorrect config structure\n", __func__);
}
}
}
void getAllowedExtensions(const std::string& clientName, std::vector<std::string>& extensions)
{
if (gClientsPermittedExtensions.count(clientName) > 0)
{
extensions = gClientsPermittedExtensions[clientName];
}
else
{
extensions = gClientsDefaultPermittedExtensions;
}
}
std::string getRenderer()
{
return gDefaultRenderer;
}
}