Class: Ciam::Saml::Metadata
- Inherits:
-
Object
- Object
- Ciam::Saml::Metadata
- Includes:
- Coding, REXML
- Defined in:
- lib/ciam/ruby-saml/metadata.rb
Constant Summary collapse
- HTTP_POST =
a few symbols for SAML class names
"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
- HTTP_GET =
"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
Instance Attribute Summary collapse
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#binding_select(service) ⇒ Object
get the IdP metadata, and select the appropriate SSO binding that we can support.
-
#build_message(options = {}) ⇒ Object
Construct a SAML message using information in the IdP metadata.
- #create_slo_request(message, extra_parameters = {}) ⇒ Object
- #create_slo_response(message, extra_parameters = {}) ⇒ Object
- #create_sso_request(message, extra_parameters = {}) ⇒ Object
- #create_sso_response(message, extra_parameters = {}) ⇒ Object
- #extract_certificate(meta_doc) ⇒ Object
- #fetch(uri_str, limit = 10) ⇒ Object
- #generate(settings) ⇒ Object
-
#get_idp_metadata ⇒ Object
Retrieve the remote IdP metadata from the URL or a cached copy returns a REXML document of the metadata.
-
#initialize(settings = nil) ⇒ Metadata
constructor
A new instance of Metadata.
-
#message_get(type, url, message, extra_parameters = {}) ⇒ Object
construct the parameter list on the URL and return.
Methods included from Coding
#decode, #deflate, #encode, #escape, #inflate, #unescape
Constructor Details
#initialize(settings = nil) ⇒ Metadata
Returns a new instance of Metadata.
24 25 26 27 28 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 24 def initialize(settings=nil) if settings @settings = settings end end |
Instance Attribute Details
#uuid ⇒ Object
Returns the value of attribute uuid.
22 23 24 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 22 def uuid @uuid end |
Instance Method Details
#binding_select(service) ⇒ Object
get the IdP metadata, and select the appropriate SSO binding that we can support. Currently this is HTTP-Redirect and HTTP-POST but more could be added in the future
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 298 def binding_select(service) # first check if we're still using the old hard coded method for # backwards compatability if service == "SingleSignOnService" && @settings.idp_sso_target_url != nil return @settings.idp_sso_target_url end if service == "SingleLogoutService" && @settings.idp_slo_target_url != nil return @settings.idp_slo_target_url end = return nil unless # first try GET (REDIRECT) sso_element = REXML::XPath.first(, "/EntityDescriptor/IDPSSODescriptor/#{service}[@Binding='#{HTTP_GET}']") if !sso_element.nil? @URL = sso_element.attributes["Location"] Logging.debug "binding_select: GET from #{@URL}" return @URL end # next try post sso_element = REXML::XPath.first(, "/EntityDescriptor/IDPSSODescriptor/#{service}[@Binding='#{HTTP_POST}']") if !sso_element.nil? @URL = sso_element.attributes["Location"] #Logging.debug "binding_select: POST to #{@URL}" return @URL end # other types we might want to add in the future: SOAP, Artifact end |
#build_message(options = {}) ⇒ Object
Construct a SAML message using information in the IdP metadata.
:type can be either “SAMLRequest” or “SAMLResponse” :service refers to the Binding method,
either "SingleLogoutService" or "SingleSignOnService"
:message is the SAML message itself (XML)
I’ve provided easy to use wrapper functions above
289 290 291 292 293 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 289 def ( = {} ) opt = { :type => nil, :service => nil, :message => nil, :extra_parameters => nil }.merge() url = binding_select( opt[:service] ) return ( opt[:type], url, opt[:message], opt[:extra_parameters] ) end |
#create_slo_request(message, extra_parameters = {}) ⇒ Object
272 273 274 275 276 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 272 def create_slo_request(, extra_parameters = {} ) ( :type => "SAMLRequest", :service => "SingleLogoutService", :message => , :extra_parameters => extra_parameters) end |
#create_slo_response(message, extra_parameters = {}) ⇒ Object
277 278 279 280 281 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 277 def create_slo_response(, extra_parameters = {} ) ( :type => "SAMLResponse", :service => "SingleLogoutService", :message => , :extra_parameters => extra_parameters) end |
#create_sso_request(message, extra_parameters = {}) ⇒ Object
262 263 264 265 266 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 262 def create_sso_request(, extra_parameters = {} ) ( :type => "SAMLRequest", :service => "SingleSignOnService", :message => , :extra_parameters => extra_parameters) end |
#create_sso_response(message, extra_parameters = {}) ⇒ Object
267 268 269 270 271 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 267 def create_sso_response(, extra_parameters = {} ) ( :type => "SAMLResponse", :service => "SingleSignOnService", :message => , :extra_parameters => extra_parameters) end |
#extract_certificate(meta_doc) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 417 def extract_certificate() #ricerco il certificato con nokogiri # pull out the x509 tag x509 = .xpath("//EntityDescriptor//IDPSSODescriptor//KeyDescriptor//KeyInfo//X509Data//X509Certificate") #x509 = REXML::XPath.first(meta_doc, "/md:EntityDescriptor/md:IDPSSODescriptor"+"/md:KeyDescriptor"+"/ds:KeyInfo/ds:X509Data/ds:X509Certificate") # If the IdP didn't specify the use attribute if x509.nil? x509 = .xpath("//EntityDescriptor//IDPSSODescriptor//KeyDescriptor//KeyInfo//X509Data//X509Certificate") # x509 = REXML::XPath.first(meta_doc, # "/EntityDescriptor/IDPSSODescriptor" + # "/KeyDescriptor" + # "/ds:KeyInfo/ds:X509Data/ds:X509Certificate" # ) end @settings.idp_cert = x509.children.to_s.gsub(/\n/, "").gsub(/\t/, "") end |
#fetch(uri_str, limit = 10) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 331 def fetch(uri_str, limit = 10) # You should choose a better exception. raise ArgumentError, 'too many HTTP redirects' if limit == 0 uri = URI.parse(uri_str) if uri.scheme == "http" response = Net::HTTP.get_response(uri) elsif uri.scheme == "https" http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true # Most IdPs will probably use self signed certs #http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.verify_mode = OpenSSL::SSL::VERIFY_NONE get = Net::HTTP::Get.new(uri.request_uri) response = http.request(get) end case response when Net::HTTPSuccess then response when Net::HTTPRedirection then location = response['location'] warn "redirected to #{location}" fetch(location, limit - 1) else response.value end end |
#generate(settings) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 30 def generate(settings) #meta_doc = REXML::Document.new = Ciam::XMLSecurityNew::Document.new root = .add_element "md:EntityDescriptor", { "xmlns:md" => "urn:oasis:names:tc:SAML:2.0:metadata", "xmlns:xml" => "http://www.w3.org/XML/1998/namespace" } if settings.issuer != nil root.attributes["entityID"] = settings.issuer end #Tolto per non far cambiare sempre il metadata #uuid = "_" + UUID.new.generate #genero l'id come hash dell'entityID uuid = "_"+Digest::MD5.hexdigest(settings.issuer) self.uuid = uuid root.attributes["ID"] = uuid sp_sso = root.add_element "md:SPSSODescriptor", { "protocolSupportEnumeration" => "urn:oasis:names:tc:SAML:2.0:protocol", "WantAssertionsSigned" => "true", "AuthnRequestsSigned" => "true" } # if settings.sp_cert != nil # keyDescriptor = sp_sso.add_element "md:KeyDescriptor", { # "use" => "signing" # } # keyInfo = keyDescriptor.add_element "ds:KeyInfo", { # "xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#" # } # x509Data = keyInfo.add_element "ds:X509Data" # x509Certificate = x509Data.add_element "ds:X509Certificate" # file = "" # File.foreach(settings.sp_cert){ |line| # file += line unless (line.include?("RSA PUBLIC KEY") || line.include?("CERTIFICATE")) # } # x509Certificate.text = file # end # Add KeyDescriptor if messages will be signed / encrypted #cert = settings.get_sp_cert cert = settings.get_cert(settings.sp_cert) if cert if cert.is_a?(String) cert = OpenSSL::X509::Certificate.new(cert) end cert_text = Base64.encode64(cert.to_der).to_s.gsub(/\n/, "").gsub(/\t/, "") kd = sp_sso.add_element "md:KeyDescriptor", { "use" => "signing" } ki = kd.add_element "ds:KeyInfo", {"xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#"} xd = ki.add_element "ds:X509Data" xc = xd.add_element "ds:X509Certificate" xc.text = cert_text # kd2 = sp_sso.add_element "md:KeyDescriptor", { "use" => "encryption" } # ki2 = kd2.add_element "ds:KeyInfo", {"xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#"} # xd2 = ki2.add_element "ds:X509Data" # xc2 = xd2.add_element "ds:X509Certificate" # xc2.text = cert_text end if !settings.sp_external_consumer_cert.nil? && settings.sp_external_consumer_cert.length > 0 settings.sp_external_consumer_cert.each{ |cert_cons_external| cert_ex = settings.get_cert(cert_cons_external) if cert_ex if cert_ex.is_a?(String) cert_ex = OpenSSL::X509::Certificate.new(cert_ex) end cert_text = Base64.encode64(cert_ex.to_der).to_s.gsub(/\n/, "").gsub(/\t/, "") kd = sp_sso.add_element "md:KeyDescriptor", { "use" => "signing" } ki = kd.add_element "ds:KeyInfo", {"xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#"} xd = ki.add_element "ds:X509Data" xc = xd.add_element "ds:X509Certificate" xc.text = cert_text end } end if settings.single_logout_service_url != nil sp_sso.add_element "md:SingleLogoutService", { "Binding" => "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect", "Location" => settings.single_logout_service_url } sp_sso.add_element "md:SingleLogoutService", { "Binding" => "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", "Location" => settings.single_logout_service_url } end #Logout dei servizi esterni unless settings.hash_assertion_consumer.blank? settings.hash_assertion_consumer.each_pair{ |index, hash_service| unless hash_service['logout'].blank? sp_sso.add_element "md:SingleLogoutService", { "Binding" => hash_service['logout']['binding'] || "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", "Location" => hash_service['logout']['location'] } end } end name_identifier_formats = settings.name_identifier_format if name_identifier_formats != nil name_id = [] name_identifier_formats.each_with_index{ |format, index| name_id[index] = sp_sso.add_element "md:NameIDFormat" name_id[index].text = format } end if settings.assertion_consumer_service_url #ciclo e creo i vari tag AssertionConsumerService settings.hash_assertion_consumer.each_pair{ |index, hash_service| sp_sso.add_element "md:AssertionConsumerService", { "Binding" => settings.assertion_consumer_service_binding, "Location" => (hash_service['external'] ? hash_service['url_consumer'] : settings.assertion_consumer_service_url ), "isDefault" => hash_service['default'], "index" => index } } # #Caso con eidas # sp_sso.add_element "md:AssertionConsumerService", { # "Binding" => settings.assertion_consumer_service_binding, # "Location" => settings.assertion_consumer_service_url, # "index" => 99 # } # sp_sso.add_element "md:AssertionConsumerService", { # "Binding" => settings.assertion_consumer_service_binding, # "Location" => settings.assertion_consumer_service_url, # "index" => 100 # } settings.hash_assertion_consumer.each_pair{ |index, hash_service| #AttributeConsumingService attr_cons_service = sp_sso.add_element "md:AttributeConsumingService", { "index" => index, } service_name = attr_cons_service.add_element "md:ServiceName", { "xml:lang" => "it" } service_name.text = hash_service['testo'] unless hash_service['description'].blank? service_description = attr_cons_service.add_element "md:ServiceDescription", { "xml:lang" => "it" } service_description.text = hash_service['description'] end if hash_service['array_campi'].is_a?(Array) hash_service['array_campi'].each_with_index{ |attribute, index| attr_cons_service.add_element "md:RequestedAttribute", { "Name" => attribute } } else #hash hash_service['array_campi'].each_pair{ |attribute, name_format| attr_cons_service.add_element "md:RequestedAttribute", { "Name" => attribute, "NameFormat" => name_format } } end } end #organization organization = root.add_element "md:Organization" org_name = organization.add_element "md:OrganizationName", { "xml:lang" => "it" } org_name.text = settings.organization['org_name'] org_display_name = organization.add_element "md:OrganizationDisplayName", { "xml:lang" => "it" } org_display_name.text = settings.organization['org_display_name'] org_url = organization.add_element "md:OrganizationURL", { "xml:lang" => "it" } org_url.text = settings.organization['org_url'] #meta_doc << REXML::XMLDecl.new(version='1.0', encoding='UTF-8') << REXML::XMLDecl.new("1.0", "UTF-8") #SE SERVE ANCHE ENCRYPTION # # Add KeyDescriptor if messages will be signed / encrypted # # if cert # cert_text = Base64.encode64(cert.to_der).gsub("\n", '') # kd = sp_sso.add_element "md:KeyDescriptor", { "use" => "signing" } # ki = kd.add_element "ds:KeyInfo", {"xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#"} # xd = ki.add_element "ds:X509Data" # xc = xd.add_element "ds:X509Certificate" # xc.text = cert_text # kd2 = sp_sso.add_element "md:KeyDescriptor", { "use" => "encryption" } # ki2 = kd2.add_element "ds:KeyInfo", {"xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#"} # xd2 = ki2.add_element "ds:X509Data" # xc2 = xd2.add_element "ds:X509Certificate" # xc2.text = cert_text # end #cert = settings.get_sp_cert cert = settings.get_cert(settings.sp_cert) #inserisco il certificato principale # embed signature if settings. && settings.sp_private_key && settings.sp_cert private_key = settings.get_sp_key .sign_document(private_key, cert) end ret = "" # stampo come stringa semplice i metadata per non avere problemi con validazione firma ret = .to_s #Logging.debug "Generated metadata:\n#{ret}" return ret end |
#get_idp_metadata ⇒ Object
Retrieve the remote IdP metadata from the URL or a cached copy returns a REXML document of the metadata
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 364 def return false if @settings..nil? # Look up the metdata in cache first id = Digest::MD5.hexdigest(@settings.) response = fetch(@settings.) #meta_text = response.body #testo_response = meta_text.sub!(' xmlns:xml="http://www.w3.org/XML/1998/namespace"', '') da errori #uso nokogiri per cercare il certificato, uso la funzione che rimuove tutti i namespace doc_noko = Nokogiri::XML(response.body.gsub(/\n/, "").gsub(/\t/, "")) #modifica per poste doc_noko.remove_namespaces! extract_certificate(doc_noko) doc_rexml = REXML::Document.new(doc_noko.to_xml) return doc_rexml # USE OF CACHE WITH CERTIFICATE # lookup = @cache.read(id) # if lookup != nil # Logging.debug "IdP metadata cached lookup for #{@settings.idp_metadata}" # doc = REXML::Document.new( lookup ) # extract_certificate( doc ) # return doc # end # Logging.debug "IdP metadata cache miss on #{@settings.idp_metadata}" # # cache miss # if File.exists?(@settings.idp_metadata) # fp = File.open( @settings.idp_metadata, "r") # meta_text = fp.read # else # uri = URI.parse(@settings.idp_metadata) # if uri.scheme == "http" # response = Net::HTTP.get_response(uri) # meta_text = response.body # elsif uri.scheme == "https" # http = Net::HTTP.new(uri.host, uri.port) # http.use_ssl = true # # Most IdPs will probably use self signed certs # #http.verify_mode = OpenSSL::SSL::VERIFY_PEER # http.verify_mode = OpenSSL::SSL::VERIFY_NONE # get = Net::HTTP::Get.new(uri.request_uri) # response = http.request(get) # meta_text = response.body # end # end # # Add it to the cache # @cache.write(id, meta_text, @settings.idp_metadata_ttl ) # doc = REXML::Document.new( meta_text ) # extract_certificate(doc) # return doc end |
#message_get(type, url, message, extra_parameters = {}) ⇒ Object
construct the parameter list on the URL and return
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/ciam/ruby-saml/metadata.rb', line 436 def ( type, url, , extra_parameters = {} ) params = Hash.new if extra_parameters params.merge!(extra_parameters) end # compress GET requests to try and stay under that 8KB request limit #deflate of samlrequest params[type] = encode( deflate( ) ) #Logging.debug "#{type}=#{params[type]}" uri = Addressable::URI.parse(url) if uri.query_values == nil uri.query_values = params else # solution to stevenwilkin's parameter merge uri.query_values = params.merge(uri.query_values) end url = uri.to_s #Logging.debug "Sending to URL #{url}" return url end |