Skip to content

Commit 1279013

Browse files
authored
Merge pull request #548 from 10Kft/feature/skip-audience
Add :skip_audience option
2 parents 63f43a7 + 86c1219 commit 1279013

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], {skip_authnst
292292
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], {skip_conditions: true}) # skips conditions
293293
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], {skip_subject_confirmation: true}) # skips subject confirmation
294294
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], {skip_recipient_check: true}) # doens't skip subject confirmation, but skips the recipient check which is a sub check of the subject_confirmation check
295+
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], {skip_audience: true}) # skips audience check
295296
```
296297
297298
All that's left is to wrap everything in a controller and reference it in the initialization and consumption URLs in OneLogin. A full controller example could look like this:

lib/onelogin/ruby-saml/response.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Response < SamlMessage
3434
# This is not a whitelist to allow people extending OneLogin::RubySaml:Response
3535
# and pass custom options
3636
AVAILABLE_OPTIONS = [
37-
:allowed_clock_drift, :check_duplicated_attributes, :matches_request_id, :settings, :skip_authnstatement, :skip_conditions,
37+
:allowed_clock_drift, :check_duplicated_attributes, :matches_request_id, :settings, :skip_audience, :skip_authnstatement, :skip_conditions,
3838
:skip_destination, :skip_recipient_check, :skip_subject_confirmation
3939
]
4040
# TODO: Update the comment on initialize to describe every option
@@ -47,6 +47,8 @@ class Response < SamlMessage
4747
# or :matches_request_id that will validate that the response matches the ID of the request,
4848
# or skip the subject confirmation validation with the :skip_subject_confirmation option
4949
# or skip the recipient validation of the subject confirmation element with :skip_recipient_check option
50+
# or skip the audience validation with :skip_audience option
51+
#
5052
def initialize(response, options = {})
5153
raise ArgumentError.new("Response cannot be nil") if response.nil?
5254

@@ -595,11 +597,13 @@ def validate_in_response_to
595597
end
596598

597599
# Validates the Audience, (If the Audience match the Service Provider EntityID)
600+
# If the response was initialized with the :skip_audience option, this validation is skipped,
598601
# If fails, the error is added to the errors array
599602
# @return [Boolean] True if there is an Audience Element that match the Service Provider EntityID, otherwise False if soft=True
600603
# @raise [ValidationError] if soft == false and validation fails
601604
#
602605
def validate_audience
606+
return true if options[:skip_audience]
603607
return true if audiences.empty? || settings.sp_entity_id.nil? || settings.sp_entity_id.empty?
604608

605609
unless audiences.include? settings.sp_entity_id

test/response_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class RubySamlTest < Minitest::Test
3838
let(:response_multiple_signed) { OneLogin::RubySaml::Response.new(read_invalid_response("multiple_signed.xml.base64")) }
3939
let(:response_audience_self_closed) { OneLogin::RubySaml::Response.new(read_response("response_audience_self_closed_tag.xml.base64")) }
4040
let(:response_invalid_audience) { OneLogin::RubySaml::Response.new(read_invalid_response("invalid_audience.xml.base64")) }
41+
let(:response_invalid_audience_with_skip) { OneLogin::RubySaml::Response.new(read_invalid_response("invalid_audience.xml.base64"), {:skip_audience => true}) }
4142
let(:response_invalid_signed_element) { OneLogin::RubySaml::Response.new(read_invalid_response("response_invalid_signed_element.xml.base64")) }
4243
let(:response_invalid_issuer_assertion) { OneLogin::RubySaml::Response.new(read_invalid_response("invalid_issuer_assertion.xml.base64")) }
4344
let(:response_invalid_issuer_message) { OneLogin::RubySaml::Response.new(read_invalid_response("invalid_issuer_message.xml.base64")) }
@@ -683,6 +684,13 @@ def generate_audience_error(expected, actual)
683684
assert !response_invalid_audience.send(:validate_audience)
684685
assert_includes response_invalid_audience.errors, generate_audience_error(response_invalid_audience.settings.sp_entity_id, ['http://invalid.audience.com'])
685686
end
687+
688+
it "return true when there is no valid audience but skip_destination option is used" do
689+
response_invalid_audience_with_skip.settings = settings
690+
response_invalid_audience_with_skip.settings.sp_entity_id = "https://invalid.example.com/audience"
691+
assert response_invalid_audience_with_skip.send(:validate_audience)
692+
assert_empty response_invalid_audience_with_skip.errors
693+
end
686694
end
687695

688696
describe "#validate_issuer" do

0 commit comments

Comments
 (0)