esp32: map basic auth to nvs

This commit is contained in:
Andy Green 2018-02-24 08:14:17 +08:00
parent 27115c3258
commit a0581a926b
5 changed files with 55 additions and 1 deletions

View file

@ -23,3 +23,15 @@ Factory Reset or Uninitialized|Factory|AP: ESP_012345|80|http://192.168.4.1|fact
User configuration|Factory|AP: config-model-serial|443|https://192.168.4.1|index.html - user set up his AP information
Operation|OTA|Station only|443|https://model-serial.local|OTA application
## Basic Auth
The lws-esp32-test-server-demos app also demos basic auth.
On a normal platform this is done by binding a mount to a text file somewhere in the filesystem, which
contains user:password information one per line.
On ESP32 there is not necessarily any generic VFS in use. So instead, the basic auth lookup is bound to
a given nvs domain, where the username is the key and the password the value. main/main.c in the test
demos app shows how to both make the mount use basic auth, and how to set a user:password combination
using nvs.

View file

@ -142,6 +142,9 @@ romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
cp++;
}
while (*p && *p == '/' && p[1] == '/')
p++;
if (!*cp && (!*p || *p == '/') &&
(ntohl(next_be) & 7) == RFST_HARDLINK) {
set_cache(i, sizeof(*i));
@ -162,6 +165,9 @@ romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
if (!*p && *cp == '/')
return NULL;
while (*p && *p == '/' && p[1] == '/')
p++;
if (*p == '/' && !*cp) {
set_cache(i, sizeof(*i));
switch (ntohl(ci->next) & 7) {

View file

@ -633,6 +633,36 @@ LWS_VISIBLE void esp32_uvtimer_cb(TimerHandle_t t)
p->cb(p->t);
}
int
lws_find_string_in_file(const char *filename, const char *string, int stringlen)
{
nvs_handle nvh;
size_t s;
int n;
char buf[64], result[64];
const char *p = strchr(string, ':'), *q;
if (!p)
return 1;
q = string;
n = 0;
while (n < sizeof(buf) - 1 && q != p)
buf[n++] = *q++;
buf[n] = '\0';
ESP_ERROR_CHECK(nvs_open(filename, NVS_READWRITE, &nvh));
s = sizeof(result) - 1;
n = nvs_get_str(nvh, buf, result, &s);
nvs_close(nvh);
if (n != ESP_OK)
return 2;
return !strcmp(p + 1, result);
}
/* helper functionality */
#include "misc/romfs.h"

View file

@ -1305,6 +1305,11 @@ LWS_EXTERN void lws_feature_status_libevent(struct lws_context_creation_info *in
#endif
#if defined(LWS_WITH_ESP32)
LWS_EXTERN int
lws_find_string_in_file(const char *filename, const char *string, int stringlen);
#endif
#ifdef LWS_WITH_IPV6
#define LWS_IPV6_ENABLED(vh) \
(!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \

View file

@ -607,7 +607,7 @@ lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
}
#if LWS_POSIX
#if !defined(LWS_WITH_ESP32)
static int
lws_find_string_in_file(const char *filename, const char *string, int stringlen)
{
@ -651,6 +651,7 @@ lws_find_string_in_file(const char *filename, const char *string, int stringlen)
return hit;
}
#endif
static int
lws_unauthorised_basic_auth(struct lws *wsi)