Class: Saml::Kit::Metadata

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Buildable, XsdValidatable
Defined in:
lib/saml/kit/metadata.rb

Constant Summary collapse

METADATA_XSD =
File.expand_path("./xsd/saml-schema-metadata-2.0.xsd", File.dirname(__FILE__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XsdValidatable

#error_message, #matches_xsd?

Constructor Details

#initialize(name, xml) ⇒ Metadata

Returns a new instance of Metadata.



17
18
19
20
21
# File 'lib/saml/kit/metadata.rb', line 17

def initialize(name, xml)
  @name = name
  @xml = xml
  @hash_algorithm = OpenSSL::Digest::SHA256
end

Instance Attribute Details

#hash_algorithm ⇒ Object

Returns the value of attribute hash_algorithm.



15
16
17
# File 'lib/saml/kit/metadata.rb', line 15

def hash_algorithm
  @hash_algorithm
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/saml/kit/metadata.rb', line 14

def name
  @name
end

#xml ⇒ Object (readonly)

Returns the value of attribute xml.



14
15
16
# File 'lib/saml/kit/metadata.rb', line 14

def xml
  @xml
end

Class Method Details

.from(content) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/saml/kit/metadata.rb', line 99

def self.from(content)
  hash = Hash.from_xml(content)
  entity_descriptor = hash["EntityDescriptor"]
  if entity_descriptor.keys.include?("SPSSODescriptor")
    Saml::Kit::ServiceProviderMetadata.new(content)
  elsif entity_descriptor.keys.include?("IDPSSODescriptor")
    Saml::Kit::IdentityProviderMetadata.new(content)
  end
end

Instance Method Details

#certificates ⇒ Object



31
32
33
34
35
36
# File 'lib/saml/kit/metadata.rb', line 31

def certificates
  @certificates ||= document.find_all("/md:EntityDescriptor/md:#{name}/md:KeyDescriptor").map do |item|
    cert = item.at_xpath("./ds:KeyInfo/ds:X509Data/ds:X509Certificate", Xml::NAMESPACES).text
    Certificate.new(cert, use: item.attribute('use').value.to_sym)
  end
end

#encryption_certificates ⇒ Object



38
39
40
# File 'lib/saml/kit/metadata.rb', line 38

def encryption_certificates
  certificates.find_all(&:encryption?)
end

#entity_id ⇒ Object



23
24
25
# File 'lib/saml/kit/metadata.rb', line 23

def entity_id
  document.find_by("/md:EntityDescriptor/@entityID").value
end

#logout_request_for(user, binding: :http_post, relay_state: nil) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/saml/kit/metadata.rb', line 67

def logout_request_for(user, binding: :http_post, relay_state: nil)
  builder = Saml::Kit::LogoutRequest.builder(user) do |x|
    yield x if block_given?
  end
  request_binding = single_logout_service_for(binding: binding)
  request_binding.serialize(builder, relay_state: relay_state)
end

#matches?(fingerprint, use: :signing) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/saml/kit/metadata.rb', line 75

def matches?(fingerprint, use: :signing)
  certificates.find do |certificate|
    certificate.for?(use) && certificate.fingerprint == fingerprint
  end
end

#name_id_formats ⇒ Object



27
28
29
# File 'lib/saml/kit/metadata.rb', line 27

def name_id_formats
  document.find_all("/md:EntityDescriptor/md:#{name}/md:NameIDFormat").map(&:text)
end

#service_for(binding:, type:) ⇒ Object



54
55
56
57
# File 'lib/saml/kit/metadata.rb', line 54

def service_for(binding:, type:)
  binding = Saml::Kit::Bindings.binding_for(binding)
  services(type).find { |x| x.binding?(binding) }
end

#services(type) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/saml/kit/metadata.rb', line 46

def services(type)
  document.find_all("/md:EntityDescriptor/md:#{name}/md:#{type}").map do |item|
    binding = item.attribute("Binding").value
    location = item.attribute("Location").value
    Saml::Kit::Bindings.create_for(binding, location)
  end
end

#signing_certificates ⇒ Object



42
43
44
# File 'lib/saml/kit/metadata.rb', line 42

def signing_certificates
  certificates.find_all(&:signing?)
end

#single_logout_service_for(binding:) ⇒ Object



63
64
65
# File 'lib/saml/kit/metadata.rb', line 63

def single_logout_service_for(binding:)
  service_for(binding: binding, type: 'SingleLogoutService')
end

#single_logout_services ⇒ Object



59
60
61
# File 'lib/saml/kit/metadata.rb', line 59

def single_logout_services
  services('SingleLogoutService')
end

#to_h ⇒ Object



81
82
83
# File 'lib/saml/kit/metadata.rb', line 81

def to_h
  @xml_hash ||= Hash.from_xml(to_xml)
end

#to_s ⇒ Object



89
90
91
# File 'lib/saml/kit/metadata.rb', line 89

def to_s
  to_xml
end

#to_xml(pretty: false) ⇒ Object



85
86
87
# File 'lib/saml/kit/metadata.rb', line 85

def to_xml(pretty: false)
  document.to_xml(pretty: pretty)
end

#verify(algorithm, signature, data) ⇒ Object



93
94
95
96
97
# File 'lib/saml/kit/metadata.rb', line 93

def verify(algorithm, signature, data)
  signing_certificates.find do |cert|
    cert.public_key.verify(algorithm, signature, data)
  end
end