Project

basic-auth

0.0
No commit activity in last 3 years
No release in over 3 years
Basic Auth Rack Middleware that supports htpasswd files
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 10.0
~> 3.0

Runtime

>= 0
 Project Readme

basic-auth Middleware

Gem Version

BoltOps Badge

Basic Auth Rack Middleware that supports htpasswd files. It works with Rack compatible frameworks like Jets, Rails, Sinatra, etc.

Sinatra Example

require 'sinatra'
require 'basic-auth'

use BasicAuth::Middleware

get '/' do
  "42\n"
end

Jets Example

config/application.rb:

Jets.application.configure do
  config.middleware.use(BasicAuth::Middleware)
end

Customizations

Option Description
passwordfile Path to the htpasswd file. Default: config/htpasswd
protect Url patterns to protect. The default behavior is to protect all urls. Default: nil (results in protecting all).

Example:

use BasicAuth::Middleware, passwordfile: "config/pass.txt", protect: %r{/area51}

The url paths under /area51 will all be protected. Other urls like the homepage will not be protected.

Cached htpasswd

The htpasswd file is loaded into memory and cached. This means any changes you make to htpasswd, like adding users, will require a server restart. If you want to disable the cache, use BASIC_AUTH_NO_CACHE=1.

Generating htpasswd files

The htpasswd tool can be used to create htpasswd files. It is installed as part of the apache webserver. It's general form is:

htpasswd PASSWORDFILE USERNAME

Example:

$ htpasswd htpasswd user
New password:
Re-type new password:
Adding password for user user
$

There are also online htpasswd generators: Htpasswd Generator

Installation

Add this line to your application's Gemfile:

gem "basic-auth"

And then execute:

bundle

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am "Add some feature")
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request