The project is in a healthy, maintained state
Instead of statically enabling basic auth on your controller by calling http_basic_authenticate_with, include HttpBasicAuthConfigurable in your controller, which allows you to configure basic auth through class attributes. This is particularly useful in certain use cases, such as allowing a host app to configure basic auth for a Rails engine it includes but doesn't control.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

 Project Readme

HttpBasicAuthConfigurable

Gem Version Ruby Style Guide

A simple configuration-driven concern to enable HTTP basic authentication for any controller or hierarchy of controllers.

What Is The Purpose Of This Gem?

To allow the dynamic enablement and configuration of HTTP basic authentication for any given controller or hierarchy of controllers.

This dynamism is useful in cases where you want to outsource the enablement and configuration of basic auth on a controller to an external source. For example, a host app that is including a Rails engine and placing its routes behind basic auth.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add http_basic_auth_configurable

If bundler is not being used to manage dependencies, install the gem by executing:

gem install http_basic_auth_configurable

General Usage

Include the concern in the controller you want to protect. For example, a controller at the root of an admin panel.

class AdminController < ApplicationController
  include HttpBasicAuthConfigurable
end

Then just configure the controller with the values of your choice.

AdminController.http_basic_auth_username = "username"
AdminController.http_basic_auth_password = "password"
AdminController.http_basic_auth_realm = "realm"
AdminController.http_basic_auth_message = "message"

That's it. Now every time a request is handled by this controller or any controller that inherits from it, basic auth will come into action if the request doesn't have the Authorization header with the right credentials.

Basic auth can also be conditionally disabled. For example, it can be disabled in the test environment to have simpler tests that don't require authentication for protected controllers. It is enabled by default when included in any controller.

AdminController.http_basic_auth_enabled = false

App Usage

Expanding on the example of the AdminController, create an initializer to dynamically load and set all configuration values on the controller.

Rails.configuration.to_prepare do
  Rails.configuration.x.admin.each do |key, value|
    AdminController.public_send("#{key}=", value)
  end
end

Then configure those values on each environment.

# development.rb
config.x.admin.http_basic_auth_username = "dev_username"
config.x.admin.http_basic_auth_password = "dev_password"
# production.rb
config.x.admin.http_basic_auth_username = "prod_username"
config.x.admin.http_basic_auth_password = "prod_password"
# test.rb
config.x.admin.http_basic_auth_enabled = false

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/alexzagu/http_basic_auth_configurable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the HttpBasicAuthConfigurable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.