Skip to content

Commit

Permalink
Add basic-auth handler using lua-pwauth simple password authentication
Browse files Browse the repository at this point in the history
Includes an example utilising pwauth.pam and a sketchup for pwauth.sasl that still lacks the backend in lua-pwauth.
  • Loading branch information
devurandom committed Dec 15, 2012
1 parent fda1476 commit 02cd1f3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "luv"]
path = luv
url = https://github.com/creationix/luv.git
[submodule "lua-pwauth"]
path = lua-pwauth
url = https://github.com/devurandom/lua-pwauth.git
34 changes: 34 additions & 0 deletions basic-auth.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local base64 = require "base64"

return function (app, options)
return function (req, res)
if req.url.path == options.path then
local authorization = req.headers.authorization
if not authorization then
return res(401,{["Content-Type"] = "text/plain", ["WWW-Authenticate"] = "Basic realm="..options.realm},"Please auth!")
end

local userpass_b64 = authorization:match("Basic%s+(.*)")
if not userpass_b64 then
return res(400, {["Content-Type"] = "text/plain"}, "Your browser sent a bad Authorization HTTP header!")
end

local userpass = base64.decode(userpass_b64)
if not userpass then
return res(400, {["Content-Type"] = "text/plain"}, "Your browser sent a bad Authorization HTTP header!")
end

local username, password = userpass:match("([^:]*):(.*)")
if not (username and password) then
return res(400, {["Content-Type"] = "text/plain"}, "Your browser sent a bad Authorization HTTP header!")
end

local success, err = options.provider:authenticate(username, password)
if not success then
return res(403,{["Content-Type"] = "text/plain", ["WWW-Authenticate"] = "Basic realm="..options.realm},"<html><body><h1>Auth failed!</h1><p>"..err.."</p></body></html>")
end
end

app(req, res)
end
end
1 change: 1 addition & 0 deletions lua-pwauth
Submodule lua-pwauth added at 2f8fd6
45 changes: 45 additions & 0 deletions samples/test-basicauth.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/lua

-- Workaround Lua module system for modules loaded by modules:
package.path = package.path .. ";lua-?/?.lua;lua-pwauth/?.lua"
package.cpath = package.cpath .. ";lua-?/?.so;lua-pwauth/lua-?/?.so"

local p = require('utils').prettyPrint
local socketHandler = require('web').socketHandler
local createServer = require('uv').createServer

local host = os.getenv("IP") or "0.0.0.0"
local port = os.getenv("PORT") or 8080

local app = function (req, res)
res(200, {
["Content-Type"] = "text/plain"
}, {"Hello ", "World\n"})
end

--local sasl = require("pwauth").sasl
--local provider = sasl.new{application="TEST", service="www", hostname="localhost", realm="TEST", mechanism=sasl.mechanisms.PLAIN}

local pam = require("pwauth").pam
local provider = pam.new("system-auth")

app = require("basic-auth")(app, {path="/", realm="TEST", provider=provider})

app = require('autoheaders')(app)

app = require('log')(app)

p{app=app}

app({
method = "GET",
url = { path = "/" },
headers = {}
}, p)

createServer(host, port, socketHandler(app))
print("http server listening at http://localhost:8080/")

require('luv').run()

print("done.")

0 comments on commit 02cd1f3

Please sign in to comment.