Skip to content

Commit

Permalink
Create the nameid alias for name_id. And promote the use of nameid in…
Browse files Browse the repository at this point in the history
…stead of name_id
  • Loading branch information
pitbulk committed Jun 17, 2015
1 parent 5b7b7c6 commit 5a2a943
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ def consume
# We validate the SAML Response and check if the user already exists in the system
if response.is_valid?
# authorize_success, log the user
session[:userid] = response.name_id
session[:userid] = response.nameid
session[:attributes] = response.attributes
else
authorize_failure # This method shows an error message
end
end
```
In the above there are a few assumptions in place, one being that the response.name_id is an email address. This is all handled with how you specify the settings that are in play via the saml_settings method. That could be implemented along the lines of this:
In the above there are a few assumptions in place, one being that the response.nameid is an email address. This is all handled with how you specify the settings that are in play via the saml_settings method. That could be implemented along the lines of this:
If the assertion of the SAMLResponse is not encrypted, you can initialize the Response without the :settings parameter and set it later,
Expand Down Expand Up @@ -156,7 +156,7 @@ class SamlController < ApplicationController
# We validate the SAML Response and check if the user already exists in the system
if response.is_valid?
# authorize_success, log the user
session[:userid] = response.name_id
session[:userid] = response.nameid
session[:attributes] = response.attributes
else
authorize_failure # This method shows an error message
Expand Down
12 changes: 6 additions & 6 deletions lib/onelogin/ruby-saml/logoutrequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ def create_logout_request_xml_doc(settings)
issuer.text = settings.issuer
end

name_id = root.add_element "saml:NameID"
nameid = root.add_element "saml:NameID"
if settings.name_identifier_value
name_id.attributes['NameQualifier'] = settings.sp_name_qualifier if settings.sp_name_qualifier
name_id.attributes['Format'] = settings.name_identifier_format if settings.name_identifier_format
name_id.text = settings.name_identifier_value
nameid.attributes['NameQualifier'] = settings.sp_name_qualifier if settings.sp_name_qualifier
nameid.attributes['Format'] = settings.name_identifier_format if settings.name_identifier_format
nameid.text = settings.name_identifier_value
else
# If no NameID is present in the settings we generate one
name_id.text = "_" + UUID.new.generate
name_id.attributes['Format'] = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'
nameid.text = "_" + UUID.new.generate
nameid.attributes['Format'] = 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'
end

if settings.sessionindex
Expand Down
4 changes: 2 additions & 2 deletions lib/onelogin/ruby-saml/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def generate(settings, pretty_print=false)
}
end
if settings.name_identifier_format
name_id = sp_sso.add_element "md:NameIDFormat"
name_id.text = settings.name_identifier_format
nameid = sp_sso.add_element "md:NameIDFormat"
nameid.text = settings.name_identifier_format
end
if settings.assertion_consumer_service_url
sp_sso.add_element "md:AssertionConsumerService", {
Expand Down
2 changes: 2 additions & 0 deletions lib/onelogin/ruby-saml/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def name_id
end
end

alias_method :nameid, :name_id

This comment has been minimized.

Copy link
@luisvm

luisvm Jun 17, 2015

Contributor

@daniel-g please review


# Gets the SessionIndex from the AuthnStatement.
# Could be used to be stored in the local session in order
# to be used in a future Logout Request that the SP could
Expand Down
2 changes: 2 additions & 0 deletions lib/onelogin/ruby-saml/slo_logoutrequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def name_id
end
end

alias_method :nameid, :name_id

# @return [String|nil] Gets the ID attribute from the Logout Request. if exists.
#
def id
Expand Down
4 changes: 2 additions & 2 deletions test/logoutrequest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RequestTest < Minitest::Test
sessionidx = UUID.new.generate
settings.sessionindex = sessionidx

unauth_url = OneLogin::RubySaml::Logoutrequest.new.create(settings, { :name_id => "there" })
unauth_url = OneLogin::RubySaml::Logoutrequest.new.create(settings, { :nameid => "there" })
inflated = decode_saml_request_payload(unauth_url)

assert_match /<samlp:SessionIndex/, inflated
Expand All @@ -44,7 +44,7 @@ class RequestTest < Minitest::Test
name_identifier_value = "abc123"
settings.name_identifier_value = name_identifier_value

unauth_url = OneLogin::RubySaml::Logoutrequest.new.create(settings, { :name_id => "there" })
unauth_url = OneLogin::RubySaml::Logoutrequest.new.create(settings, { :nameid => "there" })
inflated = decode_saml_request_payload(unauth_url)

assert_match /<saml:NameID/, inflated
Expand Down
38 changes: 19 additions & 19 deletions test/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class RubySamlTest < Minitest::Test
end

it "adapt namespace" do
refute_nil response.name_id
refute_nil response_without_attributes.name_id
refute_nil response_with_signed_assertion.name_id
refute_nil response.nameid
refute_nil response_without_attributes.nameid
refute_nil response_with_signed_assertion.nameid
end

it "default to raw input when a response is not Base64 encoded" do
Expand All @@ -70,7 +70,7 @@ class RubySamlTest < Minitest::Test
response_wrapped.stubs(:conditions).returns(nil)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_wrapped.settings = settings
assert_nil response_wrapped.name_id
assert_nil response_wrapped.nameid
end
end

Expand Down Expand Up @@ -626,20 +626,20 @@ class RubySamlTest < Minitest::Test
end
end

describe "#name_id" do
describe "#nameid" do
it "extract the value of the name id element" do
assert_equal "[email protected]", response.name_id
assert_equal "[email protected]", response_with_signed_assertion.name_id
assert_equal "[email protected]", response.nameid
assert_equal "[email protected]", response_with_signed_assertion.nameid
end

it "be extractable from an OpenSAML response" do
response_open_saml = OneLogin::RubySaml::Response.new(fixture(:open_saml))
assert_equal "[email protected]", response_open_saml.name_id
assert_equal "[email protected]", response_open_saml.nameid
end

it "be extractable from a Simple SAML PHP response" do
response_ssp = OneLogin::RubySaml::Response.new(fixture(:simple_saml_php))
assert_equal "[email protected]", response_ssp.name_id
assert_equal "[email protected]", response_ssp.nameid
end
end

Expand Down Expand Up @@ -862,20 +862,20 @@ class RubySamlTest < Minitest::Test
describe "retrieve nameID" do
it 'is possible when nameID inside the assertion' do
response_valid_signed.settings = settings
assert_equal "[email protected]", response_valid_signed.name_id
assert_equal "[email protected]", response_valid_signed.nameid
end

it 'is not possible when encryptID inside the assertion but no private key' do
response_encrypted_nameid.settings = settings
assert_raises(OneLogin::RubySaml::ValidationError, "An EncryptedID found and no SP private key found on the settings to decrypt it") do
assert_equal "[email protected]", response_encrypted_nameid.name_id
assert_equal "[email protected]", response_encrypted_nameid.nameid
end
end

it 'is possible when encryptID inside the assertion and settings has the private key' do
settings.private_key = ruby_saml_key_text
response_encrypted_nameid.settings = settings
assert_equal "[email protected]", response_encrypted_nameid.name_id
assert_equal "[email protected]", response_encrypted_nameid.nameid
end

end
Expand Down Expand Up @@ -946,7 +946,7 @@ class RubySamlTest < Minitest::Test
assert response.is_valid?
assert_empty response.errors
assert_equal "test", response.attributes[:uid]
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.name_id
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.nameid
end
end

Expand All @@ -956,7 +956,7 @@ class RubySamlTest < Minitest::Test
assert response.is_valid?
assert_empty response.errors
assert_equal "test", response.attributes[:uid]
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.name_id
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.nameid
end
end

Expand All @@ -966,7 +966,7 @@ class RubySamlTest < Minitest::Test
assert response.is_valid?
assert_empty response.errors
assert_equal "test", response.attributes[:uid]
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.name_id
assert_equal "98e2bb61075e951b37d6b3be6954a54b340d86c7", response.nameid
end
end

Expand Down Expand Up @@ -1046,28 +1046,28 @@ class RubySamlTest < Minitest::Test
unsigned_message_des192_encrypted_signed_assertion = read_response('unsigned_message_des192_encrypted_signed_assertion.xml.base64')
response = OneLogin::RubySaml::Response.new(unsigned_message_des192_encrypted_signed_assertion, :settings => settings)
assert_equal "test", response.attributes[:uid]
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.name_id
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.nameid
end

it "EncryptionMethod AES-128 && Key Encryption Algorithm RSA-OAEP-MGF1P" do
unsigned_message_aes128_encrypted_signed_assertion = read_response('unsigned_message_aes128_encrypted_signed_assertion.xml.base64')
response = OneLogin::RubySaml::Response.new(unsigned_message_aes128_encrypted_signed_assertion, :settings => settings)
assert_equal "test", response.attributes[:uid]
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.name_id
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.nameid
end

it "EncryptionMethod AES-192 && Key Encryption Algorithm RSA-OAEP-MGF1P" do
unsigned_message_aes192_encrypted_signed_assertion = read_response('unsigned_message_aes192_encrypted_signed_assertion.xml.base64')
response = OneLogin::RubySaml::Response.new(unsigned_message_aes192_encrypted_signed_assertion, :settings => settings)
assert_equal "test", response.attributes[:uid]
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.name_id
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.nameid
end

it "EncryptionMethod AES-256 && Key Encryption Algorithm RSA-OAEP-MGF1P" do
unsigned_message_aes256_encrypted_signed_assertion = read_response('unsigned_message_aes256_encrypted_signed_assertion.xml.base64')
response = OneLogin::RubySaml::Response.new(unsigned_message_aes256_encrypted_signed_assertion, :settings => settings)
assert_equal "test", response.attributes[:uid]
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.name_id
assert_equal "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7", response.nameid
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/slo_logoutrequest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RubySamlTest < Minitest::Test
it "return true when the logout request is initialized with valid data" do
assert logout_request.is_valid?
assert_empty logout_request.errors
assert_equal '[email protected]', logout_request.name_id
assert_equal '[email protected]', logout_request.nameid
end

it "should be idempotent when the logout request is initialized with invalid data" do
Expand All @@ -58,9 +58,9 @@ class RubySamlTest < Minitest::Test
end
end

describe "#name_id" do
describe "#nameid" do
it "extract the value of the name id element" do
assert_equal "[email protected]", logout_request.name_id
assert_equal "[email protected]", logout_request.nameid
end
end

Expand Down

0 comments on commit 5a2a943

Please sign in to comment.