Skip to content

Commit 664585f

Browse files
committed
add security policies for public access to API
1 parent 1ed8e20 commit 664585f

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ else()
9898
mbedx509
9999
)
100100
target_compile_definitions(mo_simulator PUBLIC
101-
MG_ENABLE_MBEDTLS=1
101+
MG_TLS=MG_TLS_MBED
102102
)
103103

104104
endif()

src/main.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <MicroOcpp.h>
1111
#include <MicroOcpp/Core/Context.h>
12+
#include <MicroOcpp/Core/FilesystemUtils.h>
1213
#include "evse.h"
1314
#include "api.h"
1415

@@ -128,6 +129,10 @@ void app_loop() {
128129

129130
#if MO_NETLIB == MO_NETLIB_MONGOOSE
130131

132+
#ifndef MO_SIM_ENDPOINT_URL
133+
#define MO_SIM_ENDPOINT_URL "http://0.0.0.0:8000" //URL to forward to mg_http_listen(). Will be ignored if the URL field exists in api.jsn
134+
#endif
135+
131136
int main() {
132137

133138
#if MBEDTLS_PLATFORM_MEMORY
@@ -143,12 +148,20 @@ int main() {
143148
mg_log_set(MG_LL_INFO);
144149
mg_mgr_init(&mgr);
145150

146-
mg_http_listen(&mgr, "0.0.0.0:8000", http_serve, NULL); // Create listening connection
147-
148151
auto filesystem = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail);
149152

150153
load_ocpp_version(filesystem);
151154

155+
auto api_settings_doc = MicroOcpp::FilesystemUtils::loadJson(filesystem, MO_FILENAME_PREFIX "api.jsn", "Simulator");
156+
if (!api_settings_doc) {
157+
api_settings_doc = MicroOcpp::makeJsonDoc("Simulator", 0);
158+
}
159+
JsonObject api_settings = api_settings_doc->as<JsonObject>();
160+
161+
const char *api_url = api_settings["url"] | MO_SIM_ENDPOINT_URL;
162+
163+
mg_http_listen(&mgr, api_url, http_serve, (void*)api_url); // Create listening connection
164+
152165
osock = new MicroOcpp::MOcppMongooseClient(&mgr,
153166
"ws://echo.websocket.events",
154167
"charger-01",
@@ -160,7 +173,7 @@ int main() {
160173
MicroOcpp::ProtocolVersion{1,6}
161174
);
162175

163-
server_initialize(osock);
176+
server_initialize(osock, api_settings["cert"] | "", api_settings["key"] | "", api_settings["user"] | "", api_settings["pass"] | "");
164177
app_setup(*osock, filesystem);
165178

166179
setOnResetExecute([] (bool isHard) {

src/net_mongoose.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,49 @@
1616
#define CORS_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers\r\nAccess-Control-Allow-Methods: GET,HEAD,OPTIONS,POST,PUT\r\n"
1717

1818
MicroOcpp::MOcppMongooseClient *ao_sock = nullptr;
19+
const char *api_cert = "";
20+
const char *api_key = "";
21+
const char *api_user = "";
22+
const char *api_pass = "";
1923

20-
void server_initialize(MicroOcpp::MOcppMongooseClient *osock) {
21-
ao_sock = osock;
24+
void server_initialize(MicroOcpp::MOcppMongooseClient *osock, const char *cert, const char *key, const char *user, const char *pass) {
25+
ao_sock = osock;
26+
api_cert = cert;
27+
api_key = key;
28+
api_user = user;
29+
api_pass = pass;
2230
}
2331

24-
char* toStringPtr(std::string cppString){
25-
char *cstr = new char[cppString.length() + 1];
26-
strcpy(cstr, cppString.c_str());
27-
return cstr;
32+
bool api_check_basic_auth(const char *user, const char *pass) {
33+
if (strcmp(api_user, user)) {
34+
return false;
35+
}
36+
if (strcmp(api_pass, pass)) {
37+
return false;
38+
}
39+
return true;
2840
}
2941

3042
void http_serve(struct mg_connection *c, int ev, void *ev_data) {
31-
if (ev == MG_EV_HTTP_MSG) {
43+
if (ev == MG_EV_ACCEPT) {
44+
if (mg_url_is_ssl((const char*)c->fn_data)) { // TLS listener!
45+
struct mg_tls_opts opts = {0};
46+
opts.cert = mg_str(api_cert);
47+
opts.key = mg_str(api_key);
48+
mg_tls_init(c, &opts);
49+
}
50+
} else if (ev == MG_EV_HTTP_MSG) {
3251
//struct mg_http_message *message_data = (struct mg_http_message *) ev_data;
3352
struct mg_http_message *message_data = reinterpret_cast<struct mg_http_message *>(ev_data);
3453
const char *final_headers = DEFAULT_HEADER CORS_HEADERS;
54+
55+
char user[64], pass[64];
56+
mg_http_creds(message_data, user, sizeof(user), pass, sizeof(pass));
57+
if (!api_check_basic_auth(user, pass)) {
58+
mg_http_reply(c, 403, final_headers, "Not Authorised\n");
59+
return;
60+
}
61+
3562
struct mg_str json = message_data->body;
3663

3764
MO_DBG_VERBOSE("%.*s", 20, message_data->uri.buf);

src/net_mongoose.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace MicroOcpp {
1313
class MOcppMongooseClient;
1414
}
1515

16-
void server_initialize(MicroOcpp::MOcppMongooseClient *osock);
16+
void server_initialize(MicroOcpp::MOcppMongooseClient *osock, const char *cert = "", const char *key = "", const char *user = "", const char *pass = "");
1717

1818
void http_serve(struct mg_connection *c, int ev, void *ev_data);
1919

0 commit comments

Comments
 (0)