Skip to content

Commit

Permalink
Add url_by_request_host option @lleirborras @MirkoMignini
Browse files Browse the repository at this point in the history
  • Loading branch information
vjt authored and tagliala committed Nov 12, 2023
1 parent 623d0bd commit eee8a39
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ Other configuration options:
extra_info
}
```
* `url_by_request_host` - Optional. Hash keyed by request host, to use
different CAS Server URLs depending on the request host. *Requires* `url`
or `host` to be set anyway, that'll be used as defaults if no host
matches.
```ruby
provider :cas,
url: 'https://cas.example.org',
url_by_request_host: {
'host1.example.org' => 'https://host1.cas.example.org',
'host2.example.org' => 'https://host2.cas.example.org',
}
```
Configurable options for values returned by CAS:
Expand Down
29 changes: 21 additions & 8 deletions lib/omniauth/strategies/cas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,27 @@ def single_sign_out_phase
def cas_url
extract_url if options['url']
validate_cas_setup
@cas_url ||= begin
uri = Addressable::URI.new
uri.host = options.host
uri.scheme = options.ssl ? 'https' : 'http'
uri.port = options.port
uri.path = options.path
uri.to_s
end

by_host_cas_url || static_cas_url
end

def by_host_cas_url
return unless options.url_by_request_host && \
options.url_by_request_host.respond_to?(:fetch)

uri = options.url_by_request_host.fetch(request.host, nil)
return unless uri

Addressable::URI.parse(uri).to_s
end

def static_cas_url
uri = Addressable::URI.new
uri.host = options.host
uri.scheme = options.ssl ? 'https' : 'http'
uri.port = options.port
uri.path = options.path
uri.to_s
end

def extract_url
Expand Down
36 changes: 36 additions & 0 deletions spec/omniauth/strategies/cas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@
expect(provider.options).to include path:'/a/path'
end
end

context 'with a URL by host mapping' do
let(:params) { super().merge(
url: 'https://default.cas.host',
url_by_request_host: {
'host1.example.org': 'https://host1.cas.host',
'host2.example.org': 'https://host2.cas.host',
})
}

let(:request_host) { nil }

before do
allow_any_instance_of(MyCasProvider)
.to receive(:request)
.and_return(Rack::Request.new('HTTP_HOST' => request_host))
end

it { true }

context 'and an host in the map' do
let(:request_host) { 'host1.example.org' }

it 'returns the corresponding CAS host in the map' do
expect(subject).to eq('https://host1.cas.host')
end
end

context 'and an host not in the map' do
let(:request_host) { 'foo.bar' }

it 'returns the default CAS host' do
expect(subject).to eq('https://default.cas.host')
end
end
end
end

describe 'defaults' do
Expand Down

1 comment on commit eee8a39

@lleirborras
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Please sign in to comment.