Skip to content

Commit 5143b44

Browse files
committed
Merge pull request #152 from onelogin/contextauth
Fix the PR #99
2 parents 05e22f0 + 5050ac8 commit 5143b44

5 files changed

Lines changed: 67 additions & 24 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ The following attributes are set:
154154
* idp_slo_target_url
155155
* id_cert_fingerpint
156156

157-
If are using saml:AttributeStatement to transfer metadata, like the user name, you can access all the attributes through response.attributes. It contains all the saml:AttributeStatement with its 'Name' as a indifferent key and the one saml:AttributeValue as value.
157+
If are using saml:AttributeStatement to transfer metadata, like the user name, you can access all the attributes through `response.attributes`. It contains all the saml:AttributeStatement with its 'Name' as a indifferent key and the one saml:AttributeValue as value.
158158

159159
```ruby
160160
response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
@@ -263,6 +263,9 @@ pp(response.attributes.multi(:not_exists))
263263
# => nil
264264
```
265265

266+
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.
267+
+If we want to add a saml:AuthnContextDeclRef, define a `settings.authn_context_decl_ref`.
268+
266269
## Service Provider Metadata
267270

268271
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
@@ -68,19 +68,34 @@ def create_authentication_xml_doc(settings)
6868
}
6969
end
7070

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

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[\S ]+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[\S ]+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)