File tree Expand file tree Collapse file tree 3 files changed +61
-1
lines changed
Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 11class ApplicationController < ActionController ::Base
22 protect_from_forgery with : :exception , prepend : true
3- before_action :restrict_request_format
3+ before_action :authenticate , : restrict_request_format
44 rescue_from SolrDataset ::NotFound , with : :render_not_found
55 rescue_from SolrDatafile ::NotFound , with : :render_not_found
66 before_action :set_collections
@@ -132,4 +132,25 @@ def set_data_manual_menu_items
132132 } ,
133133 ]
134134 end
135+
136+ def authenticate
137+ # /healthz endpoint override unnecessary as rails health endpoint does not inherit from this controller
138+ if ENV [ "BASIC_AUTH_BYPASS" ] . present?
139+ header_key , header_value = ENV [ "BASIC_AUTH_BYPASS" ] . split ( ":" ) . map ( &:strip )
140+
141+ if request . headers . key? ( header_key ) && ( request . headers [ header_key ] == header_value )
142+ return true
143+ end
144+ end
145+
146+ if ENV [ "BASIC_AUTH_USERNAME" ] . present? && ENV [ "BASIC_AUTH_PASSWORD" ] . present?
147+ authenticate_or_request_with_http_basic do |username , password |
148+ ActiveSupport ::SecurityUtils . secure_compare ( username , ENV [ "BASIC_AUTH_USERNAME" ] ) &
149+ ActiveSupport ::SecurityUtils . secure_compare ( password , ENV [ "BASIC_AUTH_PASSWORD" ] )
150+ end
151+ else
152+ logger . warn "BASIC_AUTH_USERNAME and BASIC_AUTH_PASSWORD environment variables are not set. Basic authentication is disabled."
153+ true
154+ end
155+ end
135156end
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ USER root
55COPY ./spec ./spec
66ENV BUNDLE_WITHOUT=""
77ENV RAILS_ENV=development
8+ ENV BASIC_AUTH_USERNAME=admin
9+ ENV BASIC_AUTH_PASSWORD=password
810ENV GOVUK_TEST_CHROME_NO_SANDBOX=1
911RUN apt-get update && apt-get install -y \
1012 g++ git gpg libc-dev libcurl4-openssl-dev libgdbm-dev libssl-dev \
Original file line number Diff line number Diff line change 1+ require "rails_helper"
2+
3+ RSpec . feature "basic auth" , type : :feature do
4+ before do
5+ stub_const ( "ENV" , ENV . to_hash . merge ( "BASIC_AUTH_USERNAME" => "test-username" ) )
6+ stub_const ( "ENV" , ENV . to_hash . merge ( "BASIC_AUTH_PASSWORD" => "test-password" ) )
7+ stub_const ( "ENV" , ENV . to_hash . merge ( "BASIC_AUTH_BYPASS" => "some-header: some-value" ) )
8+ end
9+
10+ scenario "I visit the homepage without setting authentication" do
11+ visit "/"
12+ expect ( page ) . to have_http_status ( :unauthorized )
13+ end
14+
15+ scenario "I visit the homepage with good authentication" do
16+ page . driver . browser . authorize ( "test-username" , "test-password" )
17+ visit "/"
18+ expect ( page ) . to have_http_status ( :ok )
19+ end
20+
21+ scenario "I visit the homepage with basic auth bypass header" do
22+ page . driver . header "some-header" , "some-value"
23+ visit "/"
24+ expect ( page ) . to have_http_status ( :ok )
25+ end
26+
27+ scenario "I visit the homepage with incorrect basic auth bypass header" do
28+ page . driver . header "some-header" , "some-incorrect-value"
29+ visit "/"
30+ expect ( page ) . to have_http_status ( :unauthorized )
31+ end
32+
33+ scenario "I visit the healthcheck page without setting authentication" do
34+ visit "/healthz"
35+ expect ( page ) . to have_http_status ( :ok )
36+ end
37+ end
You can’t perform that action at this time.
0 commit comments