Added basic auth. Added static settings file
This commit is contained in:
parent
cac11d9a1a
commit
be63ee0093
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.swp
|
||||||
4
Makefile
4
Makefile
@ -4,14 +4,14 @@
|
|||||||
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
||||||
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
|
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
|
||||||
# Serial port
|
# Serial port
|
||||||
PORT=/dev/cu.usbserial-A602HRAZ
|
PORT=/dev/ttyUSB0
|
||||||
SPEED=9600
|
SPEED=9600
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# End of user config
|
# End of user config
|
||||||
######################################################################
|
######################################################################
|
||||||
HTTP_FILES := $(wildcard http/*)
|
HTTP_FILES := $(wildcard http/*)
|
||||||
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
|
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-basicauth.lua httpserver-conf.lua httpserver-static.lua httpserver-header.lua httpserver-error.lua
|
||||||
|
|
||||||
# Print usage
|
# Print usage
|
||||||
usage:
|
usage:
|
||||||
|
|||||||
@ -9,6 +9,7 @@ A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
|
|||||||
* Server-side execution of Lua scripts
|
* Server-side execution of Lua scripts
|
||||||
* Query string argument parsing
|
* Query string argument parsing
|
||||||
* Serving .gz compressed files
|
* Serving .gz compressed files
|
||||||
|
* HTTP basic authentication
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
@ -49,6 +50,10 @@ A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.
|
|||||||
then index.html is served. By the way, unlike most HTTP servers, nodemcu_httpserver treats the URLs in a
|
then index.html is served. By the way, unlike most HTTP servers, nodemcu_httpserver treats the URLs in a
|
||||||
case-sensitive manner.
|
case-sensitive manner.
|
||||||
|
|
||||||
|
4. Enable http basic authentication.
|
||||||
|
|
||||||
|
Enable and configure http basic authentication in "httpserver-conf.lua" file.
|
||||||
|
|
||||||
## How to create dynamic Lua scripts
|
## How to create dynamic Lua scripts
|
||||||
|
|
||||||
Similar to static files, upload a Lua script called "http/[name].lua where you replace [name] with your script's name.
|
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.
|
|||||||
## Not supported
|
## Not supported
|
||||||
|
|
||||||
* Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
|
* Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
|
||||||
* HTTP authentication
|
|
||||||
* Encryption
|
* Encryption
|
||||||
|
|
||||||
## Notes on memory usage.
|
## Notes on memory usage.
|
||||||
|
|||||||
29
httpserver-basicauth.lua
Normal file
29
httpserver-basicauth.lua
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
-- httpserver-basicauth.lua
|
||||||
|
-- Part of nodemcu-httpserver, authenticates a user using http basic auth.
|
||||||
|
-- Author: Sam Dieck
|
||||||
|
|
||||||
|
basicAuth = {}
|
||||||
|
|
||||||
|
function basicAuth.authenticate(header)
|
||||||
|
conf = dofile("httpserver-conf.lc")
|
||||||
|
-- Parse basic auth http header.
|
||||||
|
-- Returns the username if header contains valid credentials,
|
||||||
|
-- nil otherwise.
|
||||||
|
local credentials_enc = header:match("Authorization: Basic ([A-Za-z0-9+/=]+)")
|
||||||
|
if not credentials_enc then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local credentials = dofile("b64.lc").decode(credentials_enc)
|
||||||
|
local user, pwd = credentials:match("^(.*):(.*)$")
|
||||||
|
if user ~= conf.auth.user or pwd ~= conf.auth.password then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
print("httpserver-basicauth: User " .. user .. " authenticated.")
|
||||||
|
return user
|
||||||
|
end
|
||||||
|
|
||||||
|
function basicAuth.authErrorHeader()
|
||||||
|
return "WWW-Authenticate: Basic realm=\"nodemcu-httpserver\""
|
||||||
|
end
|
||||||
|
|
||||||
|
return basicAuth
|
||||||
20
httpserver-conf.lua
Normal file
20
httpserver-conf.lua
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
-- httpserver-conf.lua
|
||||||
|
-- Part of nodemcu-httpserver, contains static configuration for httpserver.
|
||||||
|
-- Author: Sam Dieck
|
||||||
|
|
||||||
|
conf = {}
|
||||||
|
|
||||||
|
-- WIFI
|
||||||
|
-- FIXME use these
|
||||||
|
--wifi = {}
|
||||||
|
--wifi.essid = "Internet"
|
||||||
|
--wifi.password = ""
|
||||||
|
|
||||||
|
-- Basic Authentication Conf
|
||||||
|
auth = {}
|
||||||
|
auth.enabled = false
|
||||||
|
auth.user = "user"
|
||||||
|
auth.password = "password"
|
||||||
|
conf.auth = auth
|
||||||
|
|
||||||
|
return conf
|
||||||
@ -4,11 +4,16 @@
|
|||||||
|
|
||||||
return function (connection, args)
|
return function (connection, args)
|
||||||
|
|
||||||
local function sendHeader(connection, code, errorString, mimeType)
|
local function sendHeader(connection, code, errorString, extraHeaders, mimeType)
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
|
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
|
||||||
|
for i, header in ipairs(extraHeaders) do
|
||||||
|
connection:send(header .. "\r\n")
|
||||||
|
end
|
||||||
|
connection:send("connection: close\r\n\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Error " .. args.code .. ": " .. args.errorString)
|
print("Error " .. args.code .. ": " .. args.errorString)
|
||||||
sendHeader(connection, args.code, args.errorString, "text/html")
|
args.headers = args.headers or {}
|
||||||
|
sendHeader(connection, args.code, args.errorString, args.headers, "text/html")
|
||||||
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
||||||
end
|
end
|
||||||
|
|||||||
@ -40,16 +40,25 @@ return function (port)
|
|||||||
|
|
||||||
local function onReceive(connection, payload)
|
local function onReceive(connection, payload)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
-- print(payload) -- for debugging
|
local conf = dofile("httpserver-conf.lc")
|
||||||
|
local auth
|
||||||
|
local user = "Anonymous"
|
||||||
|
|
||||||
-- parse payload and decide what to serve.
|
-- parse payload and decide what to serve.
|
||||||
local req = dofile("httpserver-request.lc")(payload)
|
local req = dofile("httpserver-request.lc")(payload)
|
||||||
print("Requested URI: " .. req.request)
|
print("Requested URI: " .. req.request)
|
||||||
if req.methodIsValid and req.method == "GET" then
|
if conf.auth.enabled then
|
||||||
|
auth = dofile("httpserver-basicauth.lc")
|
||||||
|
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
|
||||||
|
end
|
||||||
|
if user and req.methodIsValid and req.method == "GET" then
|
||||||
onGet(connection, req.uri)
|
onGet(connection, req.uri)
|
||||||
else
|
else
|
||||||
local args = {}
|
local args = {}
|
||||||
local fileServeFunction = dofile("httpserver-error.lc")
|
local fileServeFunction = dofile("httpserver-error.lc")
|
||||||
if req.methodIsValid then
|
if not user then
|
||||||
|
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
|
||||||
|
elseif req.methodIsValid then
|
||||||
args = {code = 501, errorString = "Not Implemented"}
|
args = {code = 501, errorString = "Not Implemented"}
|
||||||
else
|
else
|
||||||
args = {code = 400, errorString = "Bad Request"}
|
args = {code = 400, errorString = "Bad Request"}
|
||||||
|
|||||||
4
init.lua
4
init.lua
@ -43,7 +43,7 @@ local compileAndRemoveIfNeeded = function(f)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
|
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'b64.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
|
||||||
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||||
|
|
||||||
compileAndRemoveIfNeeded = nil
|
compileAndRemoveIfNeeded = nil
|
||||||
@ -66,7 +66,7 @@ tmr.alarm(0, 3000, 1, function()
|
|||||||
else
|
else
|
||||||
print('IP: ',ip)
|
print('IP: ',ip)
|
||||||
-- Uncomment to automatically start the server in port 80
|
-- Uncomment to automatically start the server in port 80
|
||||||
--dofile("httpserver.lc")(80)
|
dofile("httpserver.lc")(80)
|
||||||
end
|
end
|
||||||
tmr.stop(0)
|
tmr.stop(0)
|
||||||
joinCounter = nil
|
joinCounter = nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user