diff --git a/lib/omniauth/dynamic_full_host.rb b/lib/omniauth/dynamic_full_host.rb index aeb3232..231475e 100644 --- a/lib/omniauth/dynamic_full_host.rb +++ b/lib/omniauth/dynamic_full_host.rb @@ -1,6 +1,6 @@ # configures public url for our application module Omniauth::DynamicFullHost - def full_host_url(url = nil) + def self.full_host_url(url = nil) # unescapes url on-the-fly because it might be double-escaped in some environments #(it happens for me at work with 2 reverse-proxies in front of the app...) url = CGI.unescape(url) if url @@ -10,7 +10,7 @@ def full_host_url(url = nil) url = Setting["host_name"] # else, parse it and remove both request_uri and query_string else - uri = URI.parse(URI.encode(url)) # Encode to ensure we only have ASCII charaters in url + uri = URI.parse(URI::Parser.new.escape(url)) # Encode to ensure we only have ASCII charaters in url url = "#{uri.scheme}://#{uri.host}" url << ":#{uri.port}" unless uri.default_port == uri.port end @@ -19,5 +19,5 @@ def full_host_url(url = nil) end OmniAuth.config.full_host = Proc.new do |env| - full_host_url(env["rack.session"]["omniauth.origin"] || env["omniauth.origin"]) + Omniauth::DynamicFullHost.full_host_url(env["rack.session"]["omniauth.origin"] || env["omniauth.origin"]) end diff --git a/spec/models/redmine_omniauth_cas_spec.rb b/spec/models/redmine_omniauth_cas_spec.rb index 933a178..f89fe47 100644 --- a/spec/models/redmine_omniauth_cas_spec.rb +++ b/spec/models/redmine_omniauth_cas_spec.rb @@ -13,4 +13,16 @@ expect(RedmineOmniauthCas.cas_service_validate_url).to be_nil end end + + context "dynamic full host" do + it "should return host name from setting if no url" do + Setting["host_name"] = "http://redmine.example.com" + expect(Omniauth::DynamicFullHost.full_host_url).to eq "http://redmine.example.com" + end + + it "should return host name from url if url is present" do + url = "https://redmine.example.com:3000/some/path" + expect(Omniauth::DynamicFullHost.full_host_url(url)).to eq "https://redmine.example.com:3000" + end + end end