Skip to content

jairojair/basic-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic Auth

Build Status

Generic basic Authorization based on header field, according RFC 2617.

Installation

luarocks install basic-auth

How to works ?

See a example below using Pegasus.lua

local auth      = require 'basic-auth'
local pegasus   = require 'pegasus'

local server = pegasus:new('9090')

server:start(function (req, rep)

    local user = auth(req)

    if user then

    	--[[ 

    	Use credentials from database, file, memory, whatever.
    	This example was used vars :)
    
    	--]] 

    	local fakeUserName = 'admin'
    	local fakePassWord = 'admin'

    	if user.name == fakeUserName and user.pass == fakePassWord then
    		rep:statusCode(200):write('Welcome')
    	else
    		rep:statusCode(403):write('Invalid credentials')
    	end
    else
    	rep:addHeader('WWW-Authenticate', 'Basic realm="Restricted"')
	rep:statusCode(401, 'Unauthorized'):write('Authentication required!')
	end
end)

Contributing

  • fork
  • add your contribution Thank's a lot !
  • send pull request

Issues

See the issues here

Tests

 busted

About

Inspired by jshttp and wsapi_basic_auth

License

MIT