Skip to content

Commit

Permalink
Refactor dsi redirect in omniauth
Browse files Browse the repository at this point in the history
We need the dsi redirect to be more flexible, to allow more hosts to
user DFE sign in so this commit just gets the base url from the request
i.e. http://host:port and we can append `auth/dfe/callback` to it.
  • Loading branch information
CatalinVoineag committed Apr 12, 2024
1 parent e9e0b83 commit 2b86071
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 54 deletions.
8 changes: 3 additions & 5 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
end

SETUP_PROC = lambda do |env|
service = HostingEnvironment.current_service(Rack::Request.new(env))
request = Rack::Request.new(env)
service = HostingEnvironment.current_service(request)

dfe_sign_in_redirect_uri = URI.join(
HostingEnvironment.application_url(service),
"/auth/dfe/callback",
)
dfe_sign_in_redirect_uri = URI.join(request.base_url, "/auth/dfe/callback")

env["omniauth.strategy"].options.client_options = {
port: dfe_sign_in_issuer_uri&.port,
Expand Down
13 changes: 0 additions & 13 deletions lib/hosting_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ module HostingEnvironment
placements: ENV["PLACEMENTS_DFE_SIGN_IN_SECRET"],
}.with_indifferent_access.freeze

SERVICE_HOST = {
claims: ENV["CLAIMS_HOST"],
placements: ENV["PLACEMENTS_HOST"],
}.with_indifferent_access.freeze

def self.env
@env ||= ActiveSupport::EnvironmentInquirer.new(ENV["HOSTING_ENV"].presence || "development")
end
Expand All @@ -39,14 +34,6 @@ def self.dfe_sign_in_secret(current_service)
DFE_SIGN_IN_CLIENT_SECRETS.fetch(current_service)
end

def self.application_url(current_service)
if Rails.env.development?
"http://#{SERVICE_HOST.fetch(current_service)}:#{ENV.fetch("PORT", 3000)}"
else
"https://#{SERVICE_HOST.fetch(current_service)}"
end
end

def self.current_service(request)
case request.host
when ENV["CLAIMS_HOST"], *ENV.fetch("CLAIMS_HOSTS", "").split(",")
Expand Down
36 changes: 0 additions & 36 deletions spec/lib/hosting_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,42 +88,6 @@
end
end

describe ".application_url" do
it "returns the application url for claims" do
current_service = "claims"
expect(described_class.application_url(current_service)).to eq(
"https://claims.localhost",
)
end

it "returns the application url for placements" do
current_service = "placements"
expect(described_class.application_url(current_service)).to eq(
"https://placements.localhost",
)
end

context "when env is development" do
it "returns the application url for claims with port" do
allow(Rails.env).to receive(:development?).and_return(true)
current_service = "claims"

expect(described_class.application_url(current_service)).to eq(
"http://claims.localhost:3000",
)
end

it "returns the application url for placements with port" do
allow(Rails.env).to receive(:development?).and_return(true)
current_service = "placements"

expect(described_class.application_url(current_service)).to eq(
"http://placements.localhost:3000",
)
end
end
end

describe ".current_service" do
it "returns the current service for claims" do
request = instance_double("Rack::Request", host: ENV["CLAIMS_HOST"])
Expand Down

0 comments on commit 2b86071

Please sign in to comment.