Skip to content

Commit 427ff44

Browse files
committed
Fix the PR #99
1 parent 6e6a8ad commit 427ff44

5 files changed

Lines changed: 67 additions & 23 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ response.settings = saml_settings
104104
response.attributes[:username]
105105
```
106106

107+
The saml:AuthnContextClassRef of the AuthNRequest can be provided by settings.authn_context , possible values are described at [SAMLAuthnCxt]. The comparison method can be set using the parameter settings.authn_context_comparison (the possible values are: 'exact', 'better', 'maximum' and 'minimum'), 'exact' is the default value.
108+
If we want to add a saml:AuthnContextDeclRef, define a settings.authn_context_decl_ref
109+
110+
107111
## Service Provider Metadata
108112

109113
To form a trusted pair relationship with the IdP, the SP (you) need to provide metadata XML

lib/onelogin/ruby-saml/authrequest.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,34 @@ def create_authentication_xml_doc(settings)
6666
}
6767
end
6868

69-
# BUG fix here -- if an authn_context is defined, add the tags with an "exact"
70-
# match required for authentication to succeed. If this is not defined,
71-
# the IdP will choose default rules for authentication. (Shibboleth IdP)
72-
if settings.authn_context != nil
69+
if settings.authn_context || settings.authn_context_decl_ref
70+
71+
if settings.authn_context_comparison != nil
72+
comparison = settings.authn_context_comparison
73+
else
74+
comparison = 'exact'
75+
end
76+
7377
requested_context = root.add_element "samlp:RequestedAuthnContext", {
7478
"xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol",
75-
"Comparison" => "exact",
76-
}
77-
class_ref = requested_context.add_element "saml:AuthnContextClassRef", {
78-
"xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
79+
"Comparison" => comparison,
7980
}
80-
class_ref.text = settings.authn_context
81+
82+
if settings.authn_context != nil
83+
class_ref = requested_context.add_element "saml:AuthnContextClassRef", {
84+
"xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
85+
}
86+
class_ref.text = settings.authn_context
87+
end
88+
# add saml:AuthnContextDeclRef element
89+
if settings.authn_context_decl_ref != nil
90+
class_ref = requested_context.add_element "saml:AuthnContextDeclRef", {
91+
"xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
92+
}
93+
class_ref.text = settings.authn_context_decl_ref
94+
end
8195
end
96+
8297
request_doc
8398
end
8499

lib/onelogin/ruby-saml/logoutrequest.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ def create_unauth_xml_doc(settings, params)
6262
sessionindex.text = settings.sessionindex
6363
end
6464

65-
# BUG fix here -- if an authn_context is defined, add the tags with an "exact"
66-
# match required for authentication to succeed. If this is not defined,
67-
# the IdP will choose default rules for authentication. (Shibboleth IdP)
68-
if settings.authn_context != nil
69-
requested_context = root.add_element "samlp:RequestedAuthnContext", {
70-
"xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol",
71-
"Comparison" => "exact",
72-
}
73-
class_ref = requested_context.add_element "saml:AuthnContextClassRef", {
74-
"xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
75-
}
76-
class_ref.text = settings.authn_context
77-
end
7865
request_doc
7966
end
8067
end

lib/onelogin/ruby-saml/settings.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def initialize(overrides = {})
1010
end
1111
attr_accessor :assertion_consumer_service_url, :issuer, :sp_name_qualifier
1212
attr_accessor :idp_sso_target_url, :idp_cert_fingerprint, :idp_cert, :name_identifier_format
13-
attr_accessor :authn_context
1413
attr_accessor :idp_slo_target_url
1514
attr_accessor :name_identifier_value
1615
attr_accessor :sessionindex
@@ -21,6 +20,9 @@ def initialize(overrides = {})
2120
attr_accessor :protocol_binding
2221
attr_accessor :attributes_index
2322
attr_accessor :force_authn
23+
attr_accessor :authn_context
24+
attr_accessor :authn_context_comparison
25+
attr_accessor :authn_context_decl_ref
2426

2527
private
2628

test/request_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,41 @@ class RequestTest < Test::Unit::TestCase
142142
assert auth_url =~ /^http:\/\/example.com\?field=value&SAMLRequest/
143143
end
144144
end
145+
146+
should "create the saml:AuthnContextClassRef element correctly" do
147+
settings = OneLogin::RubySaml::Settings.new
148+
settings.idp_sso_target_url = "http://example.com"
149+
settings.authn_context = 'secure/name/password/uri'
150+
auth_doc = OneLogin::RubySaml::Authrequest.new.create_authentication_xml_doc(settings)
151+
assert auth_doc.to_s =~ /<saml:AuthnContextClassRef[\S ]+>secure\/name\/password\/uri<\/saml:AuthnContextClassRef>/
152+
end
153+
154+
should "create the saml:AuthnContextClassRef with comparison exact" do
155+
settings = OneLogin::RubySaml::Settings.new
156+
settings.idp_sso_target_url = "http://example.com"
157+
settings.authn_context = 'secure/name/password/uri'
158+
auth_doc = OneLogin::RubySaml::Authrequest.new.create_authentication_xml_doc(settings)
159+
assert auth_doc.to_s =~ /<samlp:RequestedAuthnContext Comparison='exact'/
160+
assert auth_doc.to_s =~ /<saml:AuthnContextClassRef[\S ]+>secure\/name\/password\/uri<\/saml:AuthnContextClassRef>/
161+
end
162+
163+
should "create the saml:AuthnContextClassRef with comparison minimun" do
164+
settings = OneLogin::RubySaml::Settings.new
165+
settings.idp_sso_target_url = "http://example.com"
166+
settings.authn_context = 'secure/name/password/uri'
167+
settings.authn_context_comparison = 'minimun'
168+
auth_doc = OneLogin::RubySaml::Authrequest.new.create_authentication_xml_doc(settings)
169+
assert auth_doc.to_s =~ /<samlp:RequestedAuthnContext Comparison='minimun'/
170+
assert auth_doc.to_s =~ /<saml:AuthnContextClassRef[\S ]+>secure\/name\/password\/uri<\/saml:AuthnContextClassRef>/
171+
end
172+
173+
should "create the saml:AuthnContextDeclRef element correctly" do
174+
settings = OneLogin::RubySaml::Settings.new
175+
settings.idp_sso_target_url = "http://example.com"
176+
settings.authn_context_decl_ref = 'urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport'
177+
auth_doc = OneLogin::RubySaml::Authrequest.new.create_authentication_xml_doc(settings)
178+
assert auth_doc.to_s =~ /<saml:AuthnContextDeclRef[\S ]+>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport<\/saml:AuthnContextDeclRef>/
179+
end
180+
145181
end
146182
end

0 commit comments

Comments
 (0)