Skip to content

Commit be63ee0

Browse files
committed
Added basic auth. Added static settings file
1 parent cac11d9 commit be63ee0

File tree

8 files changed

+81
-13
lines changed

8 files changed

+81
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
55
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
66
# Serial port
7-
PORT=/dev/cu.usbserial-A602HRAZ
7+
PORT=/dev/ttyUSB0
88
SPEED=9600
99

1010
######################################################################
1111
# End of user config
1212
######################################################################
1313
HTTP_FILES := $(wildcard http/*)
14-
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
14+
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-basicauth.lua httpserver-conf.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
1515

1616
# Print usage
1717
usage:

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
99
* Server-side execution of Lua scripts
1010
* Query string argument parsing
1111
* Serving .gz compressed files
12+
* HTTP basic authentication
1213

1314
## How to use
1415

@@ -49,6 +50,10 @@ A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
4950
then index.html is served. By the way, unlike most HTTP servers, nodemcu_httpserver treats the URLs in a
5051
case-sensitive manner.
5152

53+
4. Enable http basic authentication.
54+
55+
Enable and configure http basic authentication in "httpserver-conf.lua" file.
56+
5257
## How to create dynamic Lua scripts
5358

5459
Similar to static files, upload a Lua script called "http/[name].lua where you replace [name] with your script's name.
@@ -123,7 +128,6 @@ A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
123128
## Not supported
124129

125130
* Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
126-
* HTTP authentication
127131
* Encryption
128132

129133
## Notes on memory usage.

httpserver-basicauth.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- httpserver-basicauth.lua
2+
-- Part of nodemcu-httpserver, authenticates a user using http basic auth.
3+
-- Author: Sam Dieck
4+
5+
basicAuth = {}
6+
7+
function basicAuth.authenticate(header)
8+
conf = dofile("httpserver-conf.lc")
9+
-- Parse basic auth http header.
10+
-- Returns the username if header contains valid credentials,
11+
-- nil otherwise.
12+
local credentials_enc = header:match("Authorization: Basic ([A-Za-z0-9+/=]+)")
13+
if not credentials_enc then
14+
return nil
15+
end
16+
local credentials = dofile("b64.lc").decode(credentials_enc)
17+
local user, pwd = credentials:match("^(.*):(.*)$")
18+
if user ~= conf.auth.user or pwd ~= conf.auth.password then
19+
return nil
20+
end
21+
print("httpserver-basicauth: User " .. user .. " authenticated.")
22+
return user
23+
end
24+
25+
function basicAuth.authErrorHeader()
26+
return "WWW-Authenticate: Basic realm=\"nodemcu-httpserver\""
27+
end
28+
29+
return basicAuth

httpserver-conf.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- httpserver-conf.lua
2+
-- Part of nodemcu-httpserver, contains static configuration for httpserver.
3+
-- Author: Sam Dieck
4+
5+
conf = {}
6+
7+
-- WIFI
8+
-- FIXME use these
9+
--wifi = {}
10+
--wifi.essid = "Internet"
11+
--wifi.password = ""
12+
13+
-- Basic Authentication Conf
14+
auth = {}
15+
auth.enabled = false
16+
auth.user = "user"
17+
auth.password = "password"
18+
conf.auth = auth
19+
20+
return conf

httpserver-error.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
return function (connection, args)
66

7-
local function sendHeader(connection, code, errorString, mimeType)
8-
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
7+
local function sendHeader(connection, code, errorString, extraHeaders, mimeType)
8+
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
9+
for i, header in ipairs(extraHeaders) do
10+
connection:send(header .. "\r\n")
11+
end
12+
connection:send("connection: close\r\n\r\n")
913
end
1014

1115
print("Error " .. args.code .. ": " .. args.errorString)
12-
sendHeader(connection, args.code, args.errorString, "text/html")
16+
args.headers = args.headers or {}
17+
sendHeader(connection, args.code, args.errorString, args.headers, "text/html")
1318
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
1419
end

httpserver.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,25 @@ return function (port)
4040

4141
local function onReceive(connection, payload)
4242
collectgarbage()
43-
-- print(payload) -- for debugging
43+
local conf = dofile("httpserver-conf.lc")
44+
local auth
45+
local user = "Anonymous"
46+
4447
-- parse payload and decide what to serve.
4548
local req = dofile("httpserver-request.lc")(payload)
4649
print("Requested URI: " .. req.request)
47-
if req.methodIsValid and req.method == "GET" then
50+
if conf.auth.enabled then
51+
auth = dofile("httpserver-basicauth.lc")
52+
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
53+
end
54+
if user and req.methodIsValid and req.method == "GET" then
4855
onGet(connection, req.uri)
4956
else
5057
local args = {}
5158
local fileServeFunction = dofile("httpserver-error.lc")
52-
if req.methodIsValid then
59+
if not user then
60+
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
61+
elseif req.methodIsValid then
5362
args = {code = 501, errorString = "Not Implemented"}
5463
else
5564
args = {code = 400, errorString = "Bad Request"}

init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID
1212
wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters
1313

1414
wifiConfig.stationPointConfig = {}
15-
wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
16-
wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
15+
wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
16+
wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
1717

1818
-- Tell the chip to connect to the access point
1919

@@ -43,7 +43,7 @@ local compileAndRemoveIfNeeded = function(f)
4343
end
4444
end
4545

46-
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
46+
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'b64.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
4747
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
4848

4949
compileAndRemoveIfNeeded = nil
@@ -66,7 +66,7 @@ tmr.alarm(0, 3000, 1, function()
6666
else
6767
print('IP: ',ip)
6868
-- Uncomment to automatically start the server in port 80
69-
--dofile("httpserver.lc")(80)
69+
dofile("httpserver.lc")(80)
7070
end
7171
tmr.stop(0)
7272
joinCounter = nil

0 commit comments

Comments
 (0)