forked from marcoskirsch/nodemcu-httpserver
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver-basicauth.lua
More file actions
Latest commit
29 lines (25 loc) · 947 Bytes
/
Copy pathhttpserver-basicauth.lua
File metadata and controls
29 lines (25 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- httpserver-basicauth.lua
-- Part of nodemcu-httpserver, authenticates a user using http basic auth.
-- Author: Sam Dieck
basicAuth= {}
functionbasicAuth.authenticate(header)
conf=dofile("httpserver-conf.lc")
-- Parse basic auth http header.
-- Returns the username if header contains valid credentials,
-- nil otherwise.
localcredentials_enc=header:match("Authorization: Basic ([A-Za-z0-9+/=]+)")
ifnotcredentials_encthen
returnnil
end
localcredentials=dofile("httpserver-b64decode.lc")(credentials_enc)
localuser, pwd=credentials:match("^(.*):(.*)$")
ifuser~=conf.auth.userorpwd~=conf.auth.passwordthen
returnnil
end
print("httpserver-basicauth: User \"" ..user.."\" authenticated.")
returnuser
end
functionbasicAuth.authErrorHeader()
return"WWW-Authenticate: Basic realm=\"" ..conf.auth.realm.."\""
end
returnbasicAuth