ruby-saml-uppercase 0.5.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +19 -0
  6. data/README.md +126 -0
  7. data/Rakefile +41 -0
  8. data/lib/onelogin/ruby-saml/authrequest.rb +79 -0
  9. data/lib/onelogin/ruby-saml/logging.rb +26 -0
  10. data/lib/onelogin/ruby-saml/logoutrequest.rb +82 -0
  11. data/lib/onelogin/ruby-saml/logoutresponse.rb +160 -0
  12. data/lib/onelogin/ruby-saml/metadata.rb +47 -0
  13. data/lib/onelogin/ruby-saml/response.rb +195 -0
  14. data/lib/onelogin/ruby-saml/settings.rb +19 -0
  15. data/lib/onelogin/ruby-saml/validation_error.rb +7 -0
  16. data/lib/onelogin/ruby-saml/version.rb +5 -0
  17. data/lib/ruby-saml.rb +9 -0
  18. data/lib/schemas/saml20assertion_schema.xsd +283 -0
  19. data/lib/schemas/saml20protocol_schema.xsd +302 -0
  20. data/lib/schemas/xenc_schema.xsd +146 -0
  21. data/lib/schemas/xmldsig_schema.xsd +318 -0
  22. data/lib/xml_security.rb +168 -0
  23. data/ruby-saml.gemspec +29 -0
  24. data/test/certificates/certificate1 +12 -0
  25. data/test/logoutrequest_test.rb +111 -0
  26. data/test/logoutresponse_test.rb +116 -0
  27. data/test/request_test.rb +53 -0
  28. data/test/response_test.rb +219 -0
  29. data/test/responses/adfs_response_sha1.xml +46 -0
  30. data/test/responses/adfs_response_sha256.xml +46 -0
  31. data/test/responses/adfs_response_sha384.xml +46 -0
  32. data/test/responses/adfs_response_sha512.xml +46 -0
  33. data/test/responses/logoutresponse_fixtures.rb +67 -0
  34. data/test/responses/no_signature_ns.xml +48 -0
  35. data/test/responses/open_saml_response.xml +56 -0
  36. data/test/responses/response1.xml.base64 +1 -0
  37. data/test/responses/response2.xml.base64 +79 -0
  38. data/test/responses/response3.xml.base64 +66 -0
  39. data/test/responses/response4.xml.base64 +93 -0
  40. data/test/responses/response5.xml.base64 +102 -0
  41. data/test/responses/response_with_ampersands.xml +139 -0
  42. data/test/responses/response_with_ampersands.xml.base64 +93 -0
  43. data/test/responses/simple_saml_php.xml +71 -0
  44. data/test/responses/wrapped_response_2.xml.base64 +150 -0
  45. data/test/settings_test.rb +44 -0
  46. data/test/test_helper.rb +66 -0
  47. data/test/xml_security_test.rb +123 -0
  48. metadata +166 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ Gemfile.lock
7
+ .idea/*
8
+ lib/Lib.iml
9
+ test/Test.iml
10
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "ruby-debug", "~> 0.10.4", :require => nil, :platforms => :ruby_18
7
+ gem "debugger", "~> 1.1.1", :require => nil, :platforms => :ruby_19
8
+ gem "shoulda"
9
+ gem "rake"
10
+ gem "mocha"
11
+ gem "nokogiri"
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 OneLogin, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Ruby SAML [![Build Status](https://secure.travis-ci.org/onelogin/ruby-saml.png)](http://travis-ci.org/onelogin/ruby-saml)
2
+
3
+ The Ruby SAML library is for implementing the client side of a SAML authorization, i.e. it provides a means for managing authorization initialization and confirmation requests from identity providers.
4
+
5
+ SAML authorization is a two step process and you are expected to implement support for both.
6
+
7
+ ## The initialization phase
8
+
9
+ This is the first request you will get from the identity provider. It will hit your application at a specific URL (that you've announced as being your SAML initialization point). The response to this initialization, is a redirect back to the identity provider, which can look something like this (ignore the saml_settings method call for now):
10
+
11
+ ```ruby
12
+ def init
13
+ request = Onelogin::Saml::Authrequest.new
14
+ redirect_to(request.create(saml_settings))
15
+ end
16
+ ```
17
+
18
+ Once you've redirected back to the identity provider, it will ensure that the user has been authorized and redirect back to your application for final consumption, this is can look something like this (the authorize_success and authorize_failure methods are specific to your application):
19
+
20
+ ```ruby
21
+ def consume
22
+ response = Onelogin::Saml::Response.new(params[:SAMLResponse])
23
+ response.settings = saml_settings
24
+
25
+ if response.is_valid? && user = current_account.users.find_by_email(response.name_id)
26
+ authorize_success(user)
27
+ else
28
+ authorize_failure(user)
29
+ end
30
+ end
31
+ ```
32
+
33
+ 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:
34
+
35
+ ```ruby
36
+ def saml_settings
37
+ settings = Onelogin::Saml::Settings.new
38
+
39
+ settings.assertion_consumer_service_url = "http://#{request.host}/saml/finalize"
40
+ settings.issuer = request.host
41
+ settings.idp_sso_target_url = "https://app.onelogin.com/saml/signon/#{OneLoginAppId}"
42
+ settings.idp_cert_fingerprint = OneLoginAppCertFingerPrint
43
+ settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
44
+ # Optional for most SAML IdPs
45
+ settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
46
+
47
+ settings
48
+ end
49
+ ```
50
+
51
+ What's left at this point, is to wrap it all up in a controller and point the initialization and consumption URLs in OneLogin at that. A full controller example could look like this:
52
+
53
+ ```ruby
54
+ # This controller expects you to use the URLs /saml/init and /saml/consume in your OneLogin application.
55
+ class SamlController < ApplicationController
56
+ def init
57
+ request = Onelogin::Saml::Authrequest.new
58
+ redirect_to(request.create(saml_settings))
59
+ end
60
+
61
+ def consume
62
+ response = Onelogin::Saml::Response.new(params[:SAMLResponse])
63
+ response.settings = saml_settings
64
+
65
+ if response.is_valid? && user = current_account.users.find_by_email(response.name_id)
66
+ authorize_success(user)
67
+ else
68
+ authorize_failure(user)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def saml_settings
75
+ settings = Onelogin::Saml::Settings.new
76
+
77
+ settings.assertion_consumer_service_url = "http://#{request.host}/saml/consume"
78
+ settings.issuer = request.host
79
+ settings.idp_sso_target_url = "https://app.onelogin.com/saml/signon/#{OneLoginAppId}"
80
+ settings.idp_cert_fingerprint = OneLoginAppCertFingerPrint
81
+ settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
82
+ # Optional for most SAML IdPs
83
+ settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
84
+
85
+ settings
86
+ end
87
+ end
88
+ ```
89
+
90
+ If are using saml:AttributeStatement to transfare metadata, like the user name, you can access all the attributes through response.attributes. It
91
+ contains all the saml:AttributeStatement with its 'Name' as a indifferent key and the one saml:AttributeValue as value.
92
+
93
+ response = Onelogin::Saml::Response.new(params[:SAMLResponse])
94
+ response.settings = saml_settings
95
+
96
+ response.attributes[:username]
97
+
98
+ ## Service Provider Metadata
99
+
100
+ To form a trusted pair relationship with the IdP, the SP (you) need to provide metadata XML
101
+ to the IdP for various good reasons. (Caching, certificate lookups, relying party permissions, etc)
102
+
103
+ The class Onelogin::Saml::Metdata takes care of this by reading the Settings and returning XML. All
104
+ you have to do is add a controller to return the data, then give this URL to the IdP administrator.
105
+ The metdata will be polled by the IdP every few minutes, so updating your settings should propagate
106
+ to the IdP settings.
107
+
108
+ ```ruby
109
+ class SamlController < ApplicationController
110
+ # ... the rest of your controller definitions ...
111
+ def metadata
112
+ settings = Account.get_saml_settings
113
+ meta = Onelogin::Saml::Metadata.new
114
+ render :xml => meta.generate(settings)
115
+ end
116
+ end
117
+ ```
118
+
119
+ ## Note on Patches/Pull Requests
120
+
121
+ * Fork the project.
122
+ * Make your feature addition or bug fix.
123
+ * Add tests for it. This is important so I don't break it in a
124
+ future version unintentionally.
125
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
126
+ * Send me a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ #not being used yet.
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ begin
13
+ require 'rcov/rcovtask'
14
+ Rcov::RcovTask.new do |test|
15
+ test.libs << 'test'
16
+ test.pattern = 'test/**/*_test.rb'
17
+ test.verbose = true
18
+ end
19
+ rescue LoadError
20
+ task :rcov do
21
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
22
+ end
23
+ end
24
+
25
+ task :test
26
+
27
+ task :default => :test
28
+
29
+ # require 'rake/rdoctask'
30
+ # Rake::RDocTask.new do |rdoc|
31
+ # if File.exist?('VERSION')
32
+ # version = File.read('VERSION')
33
+ # else
34
+ # version = ""
35
+ # end
36
+
37
+ # rdoc.rdoc_dir = 'rdoc'
38
+ # rdoc.title = "ruby-saml #{version}"
39
+ # rdoc.rdoc_files.include('README*')
40
+ # rdoc.rdoc_files.include('lib/**/*.rb')
41
+ #end
@@ -0,0 +1,79 @@
1
+ require "base64"
2
+ require "uuid"
3
+ require "zlib"
4
+ require "cgi"
5
+ require "rexml/document"
6
+ require "rexml/xpath"
7
+
8
+ module Onelogin
9
+ module Saml
10
+ include REXML
11
+ class Authrequest
12
+ def create(settings, params = {})
13
+ request_doc = create_authentication_xml_doc(settings)
14
+
15
+ request = ""
16
+ request_doc.write(request)
17
+
18
+ Logging.debug "Created AuthnRequest: #{request}"
19
+
20
+ deflated_request = Zlib::Deflate.deflate(request, 9)[2..-5]
21
+ base64_request = Base64.encode64(deflated_request)
22
+ encoded_request = CGI.escape(base64_request)
23
+ params_prefix = (settings.idp_sso_target_url =~ /\?/) ? '&' : '?'
24
+ request_params = "#{params_prefix}SAMLRequest=#{encoded_request}"
25
+
26
+ params.each_pair do |key, value|
27
+ request_params << "&#{key.to_s}=#{CGI.escape(value.to_s)}"
28
+ end
29
+
30
+ settings.idp_sso_target_url + request_params
31
+ end
32
+
33
+ def create_authentication_xml_doc(settings)
34
+ uuid = "_" + UUID.new.generate
35
+ time = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S")
36
+ # Create AuthnRequest root element using REXML
37
+ request_doc = REXML::Document.new
38
+
39
+ root = request_doc.add_element "samlp:AuthnRequest", { "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol" }
40
+ root.attributes['ID'] = uuid
41
+ root.attributes['IssueInstant'] = time
42
+ root.attributes['Version'] = "2.0"
43
+
44
+ # Conditionally defined elements based on settings
45
+ if settings.assertion_consumer_service_url != nil
46
+ root.attributes["AssertionConsumerServiceURL"] = settings.assertion_consumer_service_url
47
+ end
48
+ if settings.issuer != nil
49
+ issuer = root.add_element "saml:Issuer", { "xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion" }
50
+ issuer.text = settings.issuer
51
+ end
52
+ if settings.name_identifier_format != nil
53
+ root.add_element "samlp:NameIDPolicy", {
54
+ "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol",
55
+ # Might want to make AllowCreate a setting?
56
+ "AllowCreate" => "true",
57
+ "Format" => settings.name_identifier_format
58
+ }
59
+ end
60
+
61
+ # BUG fix here -- if an authn_context is defined, add the tags with an "exact"
62
+ # match required for authentication to succeed. If this is not defined,
63
+ # the IdP will choose default rules for authentication. (Shibboleth IdP)
64
+ if settings.authn_context != nil
65
+ requested_context = root.add_element "samlp:RequestedAuthnContext", {
66
+ "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol",
67
+ "Comparison" => "exact",
68
+ }
69
+ class_ref = requested_context.add_element "saml:AuthnContextClassRef", {
70
+ "xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion",
71
+ }
72
+ class_ref.text = settings.authn_context
73
+ end
74
+ request_doc
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,26 @@
1
+ # Simplistic log class when we're running in Rails
2
+ module Onelogin
3
+ module Saml
4
+ class Logging
5
+ def self.debug(message)
6
+ return if !!ENV["ruby-saml/testing"]
7
+
8
+ if defined? Rails
9
+ Rails.logger.debug message
10
+ else
11
+ puts message
12
+ end
13
+ end
14
+
15
+ def self.info(message)
16
+ return if !!ENV["ruby-saml/testing"]
17
+
18
+ if defined? Rails
19
+ Rails.logger.info message
20
+ else
21
+ puts message
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,82 @@
1
+ require "base64"
2
+ require "uuid"
3
+ require "zlib"
4
+ require "cgi"
5
+
6
+ module Onelogin
7
+ module Saml
8
+ include REXML
9
+ class Logoutrequest
10
+
11
+ attr_reader :uuid # Can be obtained if neccessary
12
+
13
+ def initialize
14
+ @uuid = "_" + UUID.new.generate
15
+ end
16
+
17
+ def create(settings, params={})
18
+ request_doc = create_unauth_xml_doc(settings, params)
19
+ request = ""
20
+ request_doc.write(request)
21
+
22
+ deflated_request = Zlib::Deflate.deflate(request, 9)[2..-5]
23
+ base64_request = Base64.encode64(deflated_request)
24
+ encoded_request = CGI.escape(base64_request)
25
+
26
+ params_prefix = (settings.idp_slo_target_url =~ /\?/) ? '&' : '?'
27
+ request_params = "#{params_prefix}SAMLRequest=#{encoded_request}"
28
+
29
+ params.each_pair do |key, value|
30
+ request_params << "&#{key}=#{CGI.escape(value.to_s)}"
31
+ end
32
+
33
+ @logout_url = settings.idp_slo_target_url + request_params
34
+ end
35
+
36
+ def create_unauth_xml_doc(settings, params)
37
+
38
+ time = Time.new().strftime("%Y-%m-%dT%H:%M:%SZ")
39
+
40
+ request_doc = REXML::Document.new
41
+ root = request_doc.add_element "samlp:LogoutRequest", { "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol" }
42
+ root.attributes['ID'] = @uuid
43
+ root.attributes['IssueInstant'] = time
44
+ root.attributes['Version'] = "2.0"
45
+
46
+ if settings.issuer
47
+ issuer = root.add_element "saml:Issuer", { "xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion" }
48
+ issuer.text = settings.issuer
49
+ end
50
+
51
+ if settings.name_identifier_value
52
+ name_id = root.add_element "saml:NameID", { "xmlns:saml" => "urn:oasis:names:tc:SAML:2.0:assertion" }
53
+ name_id.attributes['NameQualifier'] = settings.sp_name_qualifier if settings.sp_name_qualifier
54
+ name_id.attributes['Format'] = settings.name_identifier_format if settings.name_identifier_format
55
+ name_id.text = settings.name_identifier_value
56
+ else
57
+ raise ValidationError.new("Missing required name identifier")
58
+ end
59
+
60
+ if settings.sessionindex
61
+ sessionindex = root.add_element "samlp:SessionIndex", { "xmlns:samlp" => "urn:oasis:names:tc:SAML:2.0:protocol" }
62
+ sessionindex.text = settings.sessionindex
63
+ end
64
+
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
78
+ request_doc
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,160 @@
1
+ require "xml_security"
2
+ require "time"
3
+ require "base64"
4
+ require "zlib"
5
+ require "open-uri"
6
+
7
+ module Onelogin
8
+ module Saml
9
+ class Logoutresponse
10
+
11
+ ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
12
+ PROTOCOL = "urn:oasis:names:tc:SAML:2.0:protocol"
13
+
14
+ # For API compability, this is mutable.
15
+ attr_accessor :settings
16
+
17
+ attr_reader :document
18
+ attr_reader :options
19
+ attr_reader :response
20
+ attr_reader :raw_response
21
+ attr_reader :in_response_to, :issuer
22
+
23
+ #
24
+ # In order to validate that the response matches a given request, append
25
+ # the option:
26
+ # :matches_request_id => REQUEST_ID
27
+ #
28
+ # It will validate that the logout response matches the ID of the request.
29
+ # You can also do this yourself through the in_response_to accessor.
30
+ #
31
+ def initialize(response, settings = nil, options = {})
32
+ raise ArgumentError.new("Logoutresponse cannot be nil") if response.nil?
33
+ self.settings = settings
34
+
35
+ @options = options
36
+ @raw_response = response
37
+
38
+ parse_logoutresponse
39
+ end
40
+
41
+ def validate!
42
+ validate(false)
43
+ end
44
+
45
+ def validate(soft = true)
46
+ return false unless valid_saml?(soft) && valid_state?(soft)
47
+
48
+ valid_in_response_to?(soft) && valid_issuer?(soft) && success?(soft)
49
+ end
50
+
51
+ def success?(soft = true)
52
+ unless @status_code == "urn:oasis:names:tc:SAML:2.0:status:Success"
53
+ return soft ? false : validation_error("Bad status code. Expected <urn:oasis:names:tc:SAML:2.0:status:Success>, but was: <#@status_code> ")
54
+ end
55
+ true
56
+ end
57
+
58
+ private
59
+
60
+ # TODO: move these to a helper?
61
+ def decode(encoded)
62
+ Base64.decode64(encoded)
63
+ end
64
+ def inflate(deflated)
65
+ zlib = Zlib::Inflate.new(-Zlib::MAX_WBITS)
66
+ zlib.inflate(deflated)
67
+ end
68
+
69
+ # TODO: This is pretty ugly... Mimic an applicative functor?
70
+ def parse_samlresponse!
71
+ return if @response =~ /^</
72
+
73
+ if raw_response =~ /^</
74
+ @response = raw_response
75
+ else
76
+ @response = ((decoded = decode(raw_response)) =~ /^</) ? decoded : inflate(decoded)
77
+ end
78
+
79
+ raise Exception.new("Couldn't decode SAMLResponse") unless @response =~ /^</
80
+ end
81
+
82
+ def parse_logoutresponse
83
+ parse_samlresponse!
84
+
85
+ begin
86
+ @document = XMLSecurity::SignedDocument.new(response)
87
+ rescue REXML::ParseException => e
88
+ raise e
89
+ end
90
+
91
+ @in_response_to ||= begin
92
+ node = REXML::XPath.first(document, "/p:LogoutResponse", { "p" => PROTOCOL, "a" => ASSERTION })
93
+ node.nil? ? nil : node.attributes['InResponseTo']
94
+ end
95
+ @issuer ||= begin
96
+ node = REXML::XPath.first(document, "/p:LogoutResponse/a:Issuer", { "p" => PROTOCOL, "a" => ASSERTION })
97
+ node ||= REXML::XPath.first(document, "/p:LogoutResponse/a:Assertion/a:Issuer", { "p" => PROTOCOL, "a" => ASSERTION })
98
+ node.nil? ? nil : node.text
99
+ end
100
+ @status_code ||= begin
101
+ node = REXML::XPath.first(document, "/p:LogoutResponse/p:Status/p:StatusCode", { "p" => PROTOCOL, "a" => ASSERTION })
102
+ node.nil? ? nil : node.attributes["Value"]
103
+ end
104
+ end
105
+
106
+ def valid_saml?(soft = true)
107
+ Dir.chdir(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'schemas'))) do
108
+ @schema = Nokogiri::XML::Schema(IO.read('saml20protocol_schema.xsd'))
109
+ @xml = Nokogiri::XML(self.document.to_s)
110
+ end
111
+ if soft
112
+ @schema.validate(@xml).map{ return false }
113
+ else
114
+ @schema.validate(@xml).map{ |error| raise(Exception.new("#{error.message}\n\n#{@xml.to_s}")) }
115
+ end
116
+ end
117
+
118
+ def valid_state?(soft = true)
119
+ if response.empty?
120
+ return soft ? false : validation_error("Blank response")
121
+ end
122
+
123
+ if settings.nil?
124
+ return soft ? false : validation_error("No settings on response")
125
+ end
126
+
127
+ if settings.issuer.nil?
128
+ return soft ? false : validation_error("No issuer in settings")
129
+ end
130
+
131
+ if settings.idp_cert_fingerprint.nil? && settings.idp_cert.nil?
132
+ return soft ? false : validation_error("No fingerprint or certificate on settings")
133
+ end
134
+
135
+ true
136
+ end
137
+
138
+ def valid_in_response_to?(soft = true)
139
+ return true unless self.options.has_key? :matches_request_id
140
+
141
+ unless self.options[:matches_request_id] == in_response_to
142
+ return soft ? false : validation_error("Response does not match the request ID, expected: <#{self.options[:matches_request_id]}>, but was: <#{in_response_to}>")
143
+ end
144
+
145
+ true
146
+ end
147
+
148
+ def valid_issuer?(soft = true)
149
+ unless URI.parse(issuer) == URI.parse(self.settings.issuer)
150
+ return soft ? false : validation_error("Doesn't match the issuer, expected: <#{self.settings.issuer}>, but was: <#{issuer}>")
151
+ end
152
+ true
153
+ end
154
+
155
+ def validation_error(message)
156
+ raise ValidationError.new(message)
157
+ end
158
+ end
159
+ end
160
+ end