Class: Spid::Saml2::SPMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/spid/saml2/sp_metadata.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings:) ⇒ SPMetadata

Returns a new instance of SPMetadata.



12
13
14
15
# File 'lib/spid/saml2/sp_metadata.rb', line 12

def initialize(settings:)
  @document = REXML::Document.new
  @settings = settings
end

Instance Attribute Details

#documentObject (readonly)

:nodoc:



9
10
11
# File 'lib/spid/saml2/sp_metadata.rb', line 9

def document
  @document
end

#settingsObject (readonly)

Returns the value of attribute settings.



10
11
12
# File 'lib/spid/saml2/sp_metadata.rb', line 10

def settings
  @settings
end

Instance Method Details

#ac_serviceObject



112
113
114
115
116
117
118
119
# File 'lib/spid/saml2/sp_metadata.rb', line 112

def ac_service
  @ac_service ||=
    begin
      element = REXML::Element.new("md:AssertionConsumerService")
      element.add_attributes(ac_service_attributes)
      element
    end
end

#ac_service_attributesObject



121
122
123
124
125
126
127
128
# File 'lib/spid/saml2/sp_metadata.rb', line 121

def ac_service_attributes
  @ac_service_attributes ||= {
    "Binding" => settings.sp_acs_binding,
    "Location" => settings.sp_acs_url,
    "index" => 0,
    "isDefault" => true
  }
end

#attribute_consuming_service(index, name, fields) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/spid/saml2/sp_metadata.rb', line 81

def attribute_consuming_service(index, name, fields)
  element = REXML::Element.new("md:AttributeConsumingService")
  element.add_attributes("index" => index)
  element.add_element service_name(name)
  fields.each do |field|
    element.add_element requested_attribute(field)
  end
  element
end

#entity_descriptorObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/spid/saml2/sp_metadata.rb', line 31

def entity_descriptor
  @entity_descriptor ||=
    begin
      element = REXML::Element.new("md:EntityDescriptor")
      element.add_attributes(entity_descriptor_attributes)
      element.add_element sp_sso_descriptor
      element.add_element signature
      element
    end
end

#entity_descriptor_attributesObject



42
43
44
45
46
47
48
49
# File 'lib/spid/saml2/sp_metadata.rb', line 42

def entity_descriptor_attributes
  @entity_descriptor_attributes ||= {
    "xmlns:ds" => "http://www.w3.org/2000/09/xmldsig#",
    "xmlns:md" => "urn:oasis:names:tc:SAML:2.0:metadata",
    "entityID" => settings.sp_entity_id,
    "ID" => entity_descriptor_id
  }
end

#key_descriptorObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/spid/saml2/sp_metadata.rb', line 142

def key_descriptor
  @key_descriptor ||=
    begin
      kd = REXML::Element.new("md:KeyDescriptor")
      kd.add_attributes("use" => "signing")
      ki = kd.add_element "ds:KeyInfo"
      data = ki.add_element "ds:X509Data"
      certificate = data.add_element "ds:X509Certificate"
      certificate.text = settings.x509_certificate_der
      kd
    end
end

#requested_attribute(name) ⇒ Object



98
99
100
101
102
# File 'lib/spid/saml2/sp_metadata.rb', line 98

def requested_attribute(name)
  element = REXML::Element.new("md:RequestedAttribute")
  element.add_attributes("Name" => ATTRIBUTES_MAP[name])
  element
end

#service_name(name) ⇒ Object



91
92
93
94
95
96
# File 'lib/spid/saml2/sp_metadata.rb', line 91

def service_name(name)
  element = REXML::Element.new("md:ServiceName")
  element.add_attributes("xml:lang" => "it")
  element.text = name
  element
end

#signatureObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



74
75
76
77
78
79
# File 'lib/spid/saml2/sp_metadata.rb', line 74

def signature
  @signature ||= ::Spid::Saml2::XmlSignature.new(
    settings: settings,
    sign_reference: entity_descriptor_id
  ).signature
end

#signed_documentObject



22
23
24
25
# File 'lib/spid/saml2/sp_metadata.rb', line 22

def signed_document
  doc = Xmldsig::SignedDocument.new(unsigned_document)
  doc.sign(settings.private_key)
end

#slo_serviceObject



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/spid/saml2/sp_metadata.rb', line 130

def slo_service
  @slo_service ||=
    begin
      element = REXML::Element.new("md:SingleLogoutService")
      element.add_attributes(
        "Binding" => settings.sp_slo_service_binding,
        "Location" => settings.sp_slo_service_url
      )
      element
    end
end

#sp_sso_descriptorObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spid/saml2/sp_metadata.rb', line 53

def sp_sso_descriptor
  @sp_sso_descriptor ||=
    begin
      element = REXML::Element.new("md:SPSSODescriptor")
      element.add_attributes(sp_sso_descriptor_attributes)
      element.add_element key_descriptor
      element.add_element ac_service
      element.add_element slo_service
      settings.sp_attribute_services.each.with_index do |service, index|
        name = service[:name]
        fields = service[:fields]
        element.add_element attribute_consuming_service(
          index, name, fields
        )
      end
      element
    end
end

#sp_sso_descriptor_attributesObject



104
105
106
107
108
109
110
# File 'lib/spid/saml2/sp_metadata.rb', line 104

def sp_sso_descriptor_attributes
  @sp_sso_descriptor_attributes ||= {
    "protocolSupportEnumeration" =>
      "urn:oasis:names:tc:SAML:2.0:protocol",
    "AuthnRequestsSigned" => true
  }
end

#to_samlObject



27
28
29
# File 'lib/spid/saml2/sp_metadata.rb', line 27

def to_saml
  signed_document
end

#unsigned_documentObject



17
18
19
20
# File 'lib/spid/saml2/sp_metadata.rb', line 17

def unsigned_document
  document.add_element(entity_descriptor)
  document.to_s
end