mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-07 16:01:30 +00:00
Disable login
This commit is contained in:
1 parent
b3b52b5eba
commit
e449c1b3f0
4 files changed
+25
-22
No files matched your search
+3
-4
@@ -508,9 +508,10 @@ static struct mg_dash_user *mg_dash_authenticate(struct mg_http_message *hm,
|
|||||||
struct mg_dash *dash) {
|
struct mg_dash *dash) {
|
||||||
static struct mg_dash_user *s_users; // List of authenticated users
|
static struct mg_dash_user *s_users; // List of authenticated users
|
||||||
char user[100], pass[100];
|
char user[100], pass[100];
|
||||||
|
static struct mg_dash_user admin = {NULL, "admin", "admin", 9, (uint64_t) -1};
|
||||||
struct mg_dash_user *u, *tmp, *result = NULL;
|
struct mg_dash_user *u, *tmp, *result = NULL;
|
||||||
|
|
||||||
if (dash->authenticate == NULL) return NULL;
|
if (dash->authenticate == NULL) return &admin;
|
||||||
mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass));
|
mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass));
|
||||||
// MG_DEBUG(("user [%s], pass: [%s], h: %.*s", user, pass, hm->head.len,
|
// MG_DEBUG(("user [%s], pass: [%s], h: %.*s", user, pass, hm->head.len,
|
||||||
// hm->head.buf));
|
// hm->head.buf));
|
||||||
@@ -599,9 +600,7 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
if (mg_match(hm->uri, mg_str("/api/hi"), NULL) ||
|
if (mg_match(hm->uri, mg_str("/api/hi"), NULL) ||
|
||||||
mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
||||||
// Do nothing, handle them MG_EV_HTTP_MSG. We bypass auth for those
|
// Do nothing, handle them MG_EV_HTTP_MSG. We bypass auth for those
|
||||||
} else if (dash->authenticate && u == NULL &&
|
} else if (u == NULL && mg_match(hm->uri, mg_str("/api/#"), NULL)) {
|
||||||
mg_match(hm->uri, mg_str("/api/#"), NULL)) {
|
|
||||||
// Auth check fail
|
|
||||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||||
c->data[0] = CONN_HANDLED;
|
c->data[0] = CONN_HANDLED;
|
||||||
} else if (mg_match(hm->uri, mg_str("/api/login"), NULL) && u != NULL) {
|
} else if (mg_match(hm->uri, mg_str("/api/login"), NULL) && u != NULL) {
|
||||||
|
|||||||
+3
-4
@@ -387,9 +387,10 @@ static struct mg_dash_user *mg_dash_authenticate(struct mg_http_message *hm,
|
|||||||
struct mg_dash *dash) {
|
struct mg_dash *dash) {
|
||||||
static struct mg_dash_user *s_users; // List of authenticated users
|
static struct mg_dash_user *s_users; // List of authenticated users
|
||||||
char user[100], pass[100];
|
char user[100], pass[100];
|
||||||
|
static struct mg_dash_user admin = {NULL, "admin", "admin", 9, (uint64_t) -1};
|
||||||
struct mg_dash_user *u, *tmp, *result = NULL;
|
struct mg_dash_user *u, *tmp, *result = NULL;
|
||||||
|
|
||||||
if (dash->authenticate == NULL) return NULL;
|
if (dash->authenticate == NULL) return &admin;
|
||||||
mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass));
|
mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass));
|
||||||
// MG_DEBUG(("user [%s], pass: [%s], h: %.*s", user, pass, hm->head.len,
|
// MG_DEBUG(("user [%s], pass: [%s], h: %.*s", user, pass, hm->head.len,
|
||||||
// hm->head.buf));
|
// hm->head.buf));
|
||||||
@@ -478,9 +479,7 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
if (mg_match(hm->uri, mg_str("/api/hi"), NULL) ||
|
if (mg_match(hm->uri, mg_str("/api/hi"), NULL) ||
|
||||||
mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
||||||
// Do nothing, handle them MG_EV_HTTP_MSG. We bypass auth for those
|
// Do nothing, handle them MG_EV_HTTP_MSG. We bypass auth for those
|
||||||
} else if (dash->authenticate && u == NULL &&
|
} else if (u == NULL && mg_match(hm->uri, mg_str("/api/#"), NULL)) {
|
||||||
mg_match(hm->uri, mg_str("/api/#"), NULL)) {
|
|
||||||
// Auth check fail
|
|
||||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||||
c->data[0] = CONN_HANDLED;
|
c->data[0] = CONN_HANDLED;
|
||||||
} else if (mg_match(hm->uri, mg_str("/api/login"), NULL) && u != NULL) {
|
} else if (mg_match(hm->uri, mg_str("/api/login"), NULL) && u != NULL) {
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
static struct mg_dash s_dash;
|
||||||
|
static int authenticate(const char *user, const char *pass);
|
||||||
|
|
||||||
// Action buttons
|
// Action buttons
|
||||||
static bool s_action1, s_action2;
|
static bool s_action1, s_action2;
|
||||||
static uint64_t s_action2_timeout;
|
static uint64_t s_action2_timeout;
|
||||||
@@ -66,6 +69,7 @@ static struct mg_field_set set_metrics = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Read-write device settings
|
// Read-write device settings
|
||||||
|
static bool s_enable_login = false;
|
||||||
static double s_volume = 17.2;
|
static double s_volume = 17.2;
|
||||||
static int s_log_level = MG_LL_DEBUG;
|
static int s_log_level = MG_LL_DEBUG;
|
||||||
static char s_name[20] = "Dublin";
|
static char s_name[20] = "Dublin";
|
||||||
@@ -76,6 +80,7 @@ static char s_ota_url[100] = "https://my-product.com/ota.json";
|
|||||||
|
|
||||||
static void write_settings(void) {
|
static void write_settings(void) {
|
||||||
mg_log_level = s_log_level;
|
mg_log_level = s_log_level;
|
||||||
|
s_dash.authenticate = s_enable_login ? authenticate : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_settings(void) {
|
static void read_settings(void) {
|
||||||
@@ -86,6 +91,7 @@ static struct mg_field fields_settings[] = {
|
|||||||
{"volume", MG_VAL_DBL, &s_volume, sizeof(s_volume)},
|
{"volume", MG_VAL_DBL, &s_volume, sizeof(s_volume)},
|
||||||
{"name", MG_VAL_STR, &s_name, sizeof(s_name)},
|
{"name", MG_VAL_STR, &s_name, sizeof(s_name)},
|
||||||
{"log_level", MG_VAL_INT, &s_log_level, sizeof(s_log_level)},
|
{"log_level", MG_VAL_INT, &s_log_level, sizeof(s_log_level)},
|
||||||
|
{"enable_login", MG_VAL_BOOL, &s_enable_login, sizeof(s_enable_login)},
|
||||||
{"ota_version", MG_VAL_STR, &s_ota_version, sizeof(s_ota_version)},
|
{"ota_version", MG_VAL_STR, &s_ota_version, sizeof(s_ota_version)},
|
||||||
{"ota_status", MG_VAL_STR, &s_ota_status, sizeof(s_ota_status)},
|
{"ota_status", MG_VAL_STR, &s_ota_status, sizeof(s_ota_status)},
|
||||||
{"ota_url", MG_VAL_STR, &s_ota_url, sizeof(s_ota_url)},
|
{"ota_url", MG_VAL_STR, &s_ota_url, sizeof(s_ota_url)},
|
||||||
@@ -182,24 +188,19 @@ static int authenticate(const char *user, const char *pass) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mg_dash_init(struct mg_mgr *mgr) {
|
void mg_dash_init(struct mg_mgr *mgr) {
|
||||||
static struct mg_dash dash; // Important: keep it static!
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_leds);
|
||||||
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_metrics);
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_leds);
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_settings);
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_metrics);
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph1);
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_settings);
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph2);
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_graph1);
|
MG_DASH_ADD_FIELD_SET(&s_dash, &set_actions);
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_graph2);
|
|
||||||
MG_DASH_ADD_FIELD_SET(&dash, &set_actions);
|
|
||||||
|
|
||||||
// Add two fake files - for demonstration
|
// Add two fake files - for demonstration
|
||||||
mg_dash_file_add(mg_str("device-config.json"), 1234);
|
mg_dash_file_add(mg_str("device-config.json"), 1234);
|
||||||
mg_dash_file_add(mg_str("device-log-2026-04-25.txt"), 1327854);
|
mg_dash_file_add(mg_str("device-log-2026-04-25.txt"), 1327854);
|
||||||
|
|
||||||
// Require authentication
|
|
||||||
dash.authenticate = authenticate;
|
|
||||||
|
|
||||||
mg_mem_files = mg_packed_files;
|
mg_mem_files = mg_packed_files;
|
||||||
mg_http_listen(mgr, MG_HTTP_ADDR, mg_dash_ev_handler, &dash);
|
mg_http_listen(mgr, MG_HTTP_ADDR, mg_dash_ev_handler, &s_dash);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mg_dash_poll(struct mg_mgr *mgr) {
|
void mg_dash_poll(struct mg_mgr *mgr) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<title>My Dashboard</title>
|
<title>My Dashboard</title>
|
||||||
|
|
||||||
<!-- Mongoose device dashboard -->
|
<!-- Mongoose device dashboard -->
|
||||||
<script src="https://mongoose.ws/resources/dashboard.js?2"></script>
|
<script src="dashboard.js"></script>
|
||||||
<script>
|
<script>
|
||||||
Dashboard.init({
|
Dashboard.init({
|
||||||
debug: true,
|
debug: true,
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
graph2: {data: [], report: true},
|
graph2: {data: [], report: true},
|
||||||
actions: { action1: false, action2: false },
|
actions: { action1: false, action2: false },
|
||||||
leds: { led1: true, led2: false, led3: false },
|
leds: { led1: true, led2: false, led3: false },
|
||||||
settings: { volume: 12, log_level: 3, name: 'Galway', ota_version: '1.2.3', ota_status: 'No checks yet', ota_url: 'http://my-product.com/ota.json', ota_interval: 30 },
|
settings: { volume: 12, log_level: 3, name: 'Galway', enable_login: true, ota_version: '1.2.3', ota_status: 'No checks yet', ota_url: 'http://my-product.com/ota.json', ota_interval: 30 },
|
||||||
metrics: { temperature: 1.2, cpu: 4, ram: 5, version: '1.2.3' },
|
metrics: { temperature: 1.2, cpu: 4, ram: 5, version: '1.2.3' },
|
||||||
files: {data: [{name: 'device-log-2026-04-25.txt', size: 8625563}]},
|
files: {data: [{name: 'device-log-2026-04-25.txt', size: 8625563}]},
|
||||||
},
|
},
|
||||||
@@ -561,6 +561,10 @@ a, a:visited, a:link { text-decoration: none; }
|
|||||||
<option value="4">Verbose (very noisy)</option>
|
<option value="4">Verbose (very noisy)</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="field-label">Enable login</label>
|
||||||
|
<input type="checkbox" data-bind="settings.enable_login" class="toggle" />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-footer">
|
<div class="panel-footer">
|
||||||
<button class="btn btn-secondary" type="button" data-cancel="settings">
|
<button class="btn btn-secondary" type="button" data-cancel="settings">
|
||||||
|
|||||||
Reference in new issue
Block a user