HttpBasicAuthConfigurable
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_configurableIf bundler is not being used to manage dependencies, install the gem by executing:
gem install http_basic_auth_configurableGeneral 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
endThen 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 = falseApp 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
endThen 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 = falseDevelopment
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.