ruby-saml-federazione-trentina 0.0.1

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 +189 -0
  9. data/lib/onelogin/ruby-saml/coding.rb +34 -0
  10. data/lib/onelogin/ruby-saml/logging.rb +26 -0
  11. data/lib/onelogin/ruby-saml/logout_request.rb +126 -0
  12. data/lib/onelogin/ruby-saml/logout_response.rb +132 -0
  13. data/lib/onelogin/ruby-saml/metadata.rb +270 -0
  14. data/lib/onelogin/ruby-saml/request.rb +83 -0
  15. data/lib/onelogin/ruby-saml/response.rb +211 -0
  16. data/lib/onelogin/ruby-saml/settings.rb +29 -0
  17. data/lib/onelogin/ruby-saml/validation_error.rb +7 -0
  18. data/lib/onelogin/ruby-saml/version.rb +5 -0
  19. data/lib/ruby-saml.rb +11 -0
  20. data/lib/schemas/saml20assertion_schema.xsd +283 -0
  21. data/lib/schemas/saml20protocol_schema.xsd +302 -0
  22. data/lib/schemas/xenc_schema.xsd +146 -0
  23. data/lib/schemas/xmldsig_schema.xsd +318 -0
  24. data/lib/xml_security.rb +165 -0
  25. data/ruby-saml.gemspec +21 -0
  26. data/test/certificates/certificate1 +12 -0
  27. data/test/logoutrequest_test.rb +98 -0
  28. data/test/request_test.rb +53 -0
  29. data/test/response_test.rb +219 -0
  30. data/test/responses/adfs_response_sha1.xml +46 -0
  31. data/test/responses/adfs_response_sha256.xml +46 -0
  32. data/test/responses/adfs_response_sha384.xml +46 -0
  33. data/test/responses/adfs_response_sha512.xml +46 -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 +43 -0
  46. data/test/test_helper.rb +66 -0
  47. data/test/xml_security_test.rb +123 -0
  48. metadata +155 -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,189 @@
1
+ require "base64"
2
+ require "uuid"
3
+ require "zlib"
4
+ require "cgi"
5
+ require "rexml/document"
6
+ require "rexml/xpath"
7
+ require "rubygems"
8
+ require "addressable/uri"
9
+
10
+ module Onelogin::Saml
11
+ include REXML
12
+ class Authrequest
13
+ # a few symbols for SAML class names
14
+ HTTP_POST = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
15
+ HTTP_GET = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
16
+
17
+ attr_accessor :uuid, :request
18
+
19
+ def initialize( settings )
20
+ @settings = settings
21
+ @request_params = Hash.new
22
+ end
23
+
24
+ def create(params = {})
25
+ uuid = "_" + UUID.new.generate
26
+ self.uuid = uuid
27
+ time = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
28
+ # Create AuthnRequest root element using REXML
29
+ request_doc = REXML::Document.new
30
+ request_doc.context[:attribute_quote] = :quote
31
+ root = request_doc.add_element "saml2p:AuthnRequest", { "xmlns:saml2p" => "urn:oasis:names:tc:SAML:2.0:protocol" }
32
+ root.attributes['ID'] = uuid
33
+ root.attributes['IssueInstant'] = time
34
+ root.attributes['Version'] = "2.0"
35
+ root.attributes['ProtocolBinding'] = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
36
+ root.attributes['AttributeConsumingServiceIndex'] = "2"
37
+ root.attributes['ForceAuthn'] = "false"
38
+ root.attributes['IsPassive'] = "false"
39
+
40
+ # Conditionally defined elements based on settings
41
+ if @settings.assertion_consumer_service_url != nil
42
+ root.attributes["AssertionConsumerServiceURL"] = @settings.assertion_consumer_service_url
43
+ end
44
+
45
+ if @settings.destination_service_url != nil
46
+ root.attributes["Destination"] = @settings.destination_service_url
47
+ end
48
+
49
+ if @settings.issuer != nil
50
+ issuer = root.add_element "saml2:Issuer", { "xmlns:saml2" => "urn:oasis:names:tc:SAML:2.0:assertion" }
51
+ issuer.text = @settings.issuer
52
+ end
53
+ if @settings.name_identifier_format != nil
54
+ root.add_element "saml2p:NameIDPolicy", {
55
+ # Might want to make AllowCreate a setting?
56
+ "AllowCreate" => "false",
57
+ "Format" => @settings.name_identifier_format[1],
58
+ "SPNameQualifier" => @settings.sp_name_qualifier
59
+ }
60
+ end
61
+
62
+ # BUG fix here -- if an authn_context is defined, add the tags with an "exact"
63
+ # match required for authentication to succeed. If this is not defined,
64
+ # the IdP will choose default rules for authentication. (Shibboleth IdP)
65
+ if @settings.authn_context != nil
66
+ requested_context = root.add_element "saml2p:RequestedAuthnContext", {
67
+ "Comparison" => "exact"
68
+ }
69
+ context_class = []
70
+ @settings.authn_context.each_with_index{ |context, index|
71
+ context_class[index] = requested_context.add_element "saml2:AuthnContextClassRef", {
72
+ "xmlns:saml2" => "urn:oasis:names:tc:SAML:2.0:assertion"
73
+ }
74
+ context_class[index].text = context
75
+ }
76
+
77
+ end
78
+
79
+ if @settings.requester_identificator != nil
80
+ requester_identificator = root.add_element "saml2p:Scoping", {
81
+ "ProxyCount" => "1"
82
+ }
83
+ identificators = []
84
+ @settings.requester_identificator.each_with_index{ |requester, index|
85
+ identificators[index] = requester_identificator.add_element "saml2p:RequesterID"
86
+ identificators[index].text = requester
87
+ }
88
+
89
+ end
90
+
91
+ request_doc << REXML::XMLDecl.new(version='1.0', encoding='UTF-8')
92
+ ret = ""
93
+ # pretty print the XML so IdP administrators can easily see what the SP supports
94
+ request_doc.write(ret, 1)
95
+
96
+ @request = ""
97
+ request_doc.write(@request)
98
+
99
+ #Logging.debug "Created AuthnRequest: #{@request}"
100
+
101
+ #params.each_pair do |key, value|
102
+ # #request_params << "&#{key}=#{CGI.escape(value.to_s)}"
103
+ # @request_params[key] = value
104
+ #end
105
+
106
+ #settings.idp_sso_target_url + request_params
107
+
108
+ return self
109
+
110
+ end
111
+
112
+ # get the IdP metadata, and select the appropriate SSO binding
113
+ # that we can support. Currently this is HTTP-Redirect and HTTP-POST
114
+ # but more could be added in the future
115
+ def binding_select
116
+ # first check if we're still using the old hard coded method for
117
+ # backwards compatability
118
+ if @settings.idp_metadata == nil && @settings.idp_sso_target_url != nil
119
+ @URL = @settings.idp_sso_target_url
120
+ return "GET", content_get
121
+ end
122
+ # grab the metadata
123
+ metadata = Metadata::new
124
+ meta_doc = metadata.get_idp_metadata(@settings)
125
+
126
+ # first try POST
127
+ sso_element = REXML::XPath.first(meta_doc,
128
+ "/EntityDescriptor/IDPSSODescriptor/SingleSignOnService[@Binding='#{HTTP_POST}']")
129
+ if sso_element
130
+ @URL = sso_element.attributes["Location"]
131
+ #Logging.debug "binding_select: POST to #{@URL}"
132
+ return "POST", content_post
133
+ end
134
+
135
+ # next try GET
136
+ sso_element = REXML::XPath.first(meta_doc,
137
+ "/EntityDescriptor/IDPSSODescriptor/SingleSignOnService[@Binding='#{HTTP_GET}']")
138
+ if sso_element
139
+ @URL = sso_element.attributes["Location"]
140
+ Logging.debug "binding_select: GET from #{@URL}"
141
+ return "GET", content_get
142
+ end
143
+ # other types we might want to add in the future: SOAP, Artifact
144
+ end
145
+
146
+ # construct the the parameter list on the URL and return
147
+ def content_get
148
+ # compress GET requests to try and stay under that 8KB request limit
149
+ deflated_request = Zlib::Deflate.deflate(@request, 9)[2..-5]
150
+ # strict_encode64() isn't available? sub out the newlines
151
+ @request_params["SAMLRequest"] = Base64.encode64(deflated_request).gsub(/\n/, "")
152
+
153
+ Logging.debug "SAMLRequest=#{@request_params["SAMLRequest"]}"
154
+ uri = Addressable::URI.parse(@URL)
155
+ if uri.query_values == nil
156
+ uri.query_values = @request_params
157
+ else
158
+ # solution to stevenwilkin's parameter merge
159
+ uri.query_values = @request_params.merge(uri.query_values)
160
+ end
161
+ url = uri.to_s
162
+ #url = @URL + "?SAMLRequest=" + @request_params["SAMLRequest"]
163
+ #Logging.debug "Sending to URL #{url}"
164
+ return url
165
+ end
166
+ # construct an HTML form (POST) and return the content
167
+ def content_post
168
+ # POST requests seem to bomb out when they're deflated
169
+ # and they probably don't need to be compressed anyway
170
+ @request_params["SAMLRequest"] = Base64.encode64(@request).gsub(/\n/, "")
171
+
172
+ #Logging.debug "SAMLRequest=#{@request_params["SAMLRequest"]}"
173
+ # kind of a cheesy method of building an HTML, form since we can't rely on Rails too much,
174
+ # and REXML doesn't work well with quote characters
175
+ str = "<html><body onLoad=\"document.getElementById('form').submit();\">\n"
176
+ str += "<form id='form' name='form' method='POST' action=\"#{@URL}\">\n"
177
+ # we could change this in the future to associate a temp auth session ID
178
+ str += "<input name='RelayState' value='ruby-saml' type='hidden' />\n"
179
+ @request_params.each_pair do |key, value|
180
+ str += "<input name=\"#{key}\" value=\"#{value}\" type='hidden' />\n"
181
+ #str += "<input name=\"#{key}\" value=\"#{CGI.escape(value)}\" type='hidden' />\n"
182
+ end
183
+ str += "</form></body></html>\n"
184
+
185
+ #Logging.debug "Created form:\n#{str}"
186
+ return str
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,34 @@
1
+ require "cgi"
2
+ require 'zlib'
3
+
4
+ module Onelogin
5
+ module Saml
6
+ module Coding
7
+ def decode(encoded)
8
+ Base64.decode64(encoded)
9
+ end
10
+
11
+ def encode(encoded)
12
+ Base64.encode64(encoded).gsub(/\n/, "")
13
+ end
14
+
15
+ def escape(unescaped)
16
+ CGI.escape(unescaped)
17
+ end
18
+
19
+ def unescape(escaped)
20
+ CGI.unescape(escaped)
21
+ end
22
+
23
+ def inflate(deflated)
24
+ zlib = Zlib::Inflate.new(-Zlib::MAX_WBITS)
25
+ zlib.inflate(deflated)
26
+ end
27
+
28
+ def deflate(inflated)
29
+ Zlib::Deflate.deflate(inflated, 9)[2..-5]
30
+ end
31
+
32
+ end
33
+ end
34
+ end