Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support RelayState binding by default during SSO #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/omniauth/strategies/saml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.inherited(subclass)
RUBYSAML_RESPONSE_OPTIONS = OneLogin::RubySaml::Response::AVAILABLE_OPTIONS

option :name_identifier_format, nil
option :idp_sso_service_url_runtime_params, {}
option :idp_sso_service_url_runtime_params, { RelayState: 'RelayState' }
option :request_attributes, [
{ :name => 'email', :name_format => 'urn:oasis:names:tc:SAML:2.0:attrname-format:basic', :friendly_name => 'Email address' },
{ :name => 'name', :name_format => 'urn:oasis:names:tc:SAML:2.0:attrname-format:basic', :friendly_name => 'Full name' },
Expand Down Expand Up @@ -118,6 +118,22 @@ def find_attribute_by(keys)
nil
end

def mock_request_call
# Per SAML 2.0, if a RelayState param is passed, IDPs "MUST place the exact RelayState
# data it received with the request into the corresponding RelayState parameter in the response."
#
# By default, the "mock" `OmniAuth::Strategy` implementation will forward along any URL params,
# so we can in turn take any POSTed RelayState params and put them in the GET query string:
query_hash = request.GET.merge!(additional_params_for_authn_request.slice('RelayState'))
query_string = Rack::Utils.build_query(query_hash)

request.set_header(Rack::QUERY_STRING, query_string)
request.set_header(Rack::RACK_REQUEST_QUERY_STRING, query_string)
request.set_header(Rack::RACK_REQUEST_QUERY_HASH, query_hash)

super
end

private

def request_path_pattern
Expand Down
27 changes: 27 additions & 0 deletions spec/omniauth/strategies/saml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ def post_xml(xml = :example_response, opts = {})
end
end

context 'with RelayState param' do
before do
post '/auth/saml', 'RelayState' => 'RELAY_STATE_VALUE'
end

it 'should get authentication page' do
expect(last_response).to be_redirect
expect(last_response.location).to match(
/\Ahttps:\/\/idp.sso.example.com\/signon\/29490\?SAMLRequest=.*&RelayState=RELAY_STATE_VALUE\z/,
)
end

context 'when test_mode is enabled' do
around do |example|
OmniAuth.config.test_mode = true
example.run
ensure
OmniAuth.config.test_mode = false
end

it 'should redirect to local saml callback page' do
expect(last_response).to be_redirect
expect(last_response.location).to eq('http://example.org/auth/saml/callback?RelayState=RELAY_STATE_VALUE')
end
end
end

context "when the assertion_consumer_service_url is the default" do
before :each do
saml_options[:compress_request] = false
Expand Down