Class: SAML2::Message Abstract
- Includes:
- Signable
- Defined in:
- lib/saml2/message.rb
Overview
In the SAML Schema, Request and Response don’t technically share a common ancestor, but they have several things in common so it’s useful to represent that in this gem as a common base class.
Direct Known Subclasses
Instance Attribute Summary collapse
- #destination ⇒ String?
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
- #issuer ⇒ NameID?
Attributes inherited from Base
Class Method Summary collapse
-
.from_xml(node) ⇒ Message
Create an appropriate Message subclass instance to represent the given XML element.
-
.parse(xml) ⇒ Message
Parses XML, and returns an appropriate Message subclass instance.
Instance Method Summary collapse
-
#from_xml(node) ⇒ void
Parse an XML element into this object.
- #id ⇒ String
-
#initialize ⇒ Message
constructor
A new instance of Message.
- #issue_instant ⇒ Time
-
#sign(x509_certificate, private_key, algorithm_name = :sha256) ⇒ self
Sign this object.
-
#valid_schema? ⇒ Boolean
If the XML is valid according to SAML XSDs.
- #validate ⇒ Object
Methods included from Signable
#signature, #signed?, #signing_key, #valid_signature?, #validate_signature
Methods inherited from Base
#decrypt, #inspect, load_object_array, load_string_array, lookup_qname, #to_s, #to_xml
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
95 96 97 98 99 100 |
# File 'lib/saml2/message.rb', line 95 def initialize super @errors = [] @id = "_#{SecureRandom.uuid}" @issue_instant = Time.now.utc end |
Instance Attribute Details
#destination ⇒ String?
145 146 147 148 149 150 |
# File 'lib/saml2/message.rb', line 145 def destination if xml && !instance_variable_defined?(:@destination) @destination = xml['Destination'] end @destination end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
47 48 49 |
# File 'lib/saml2/message.rb', line 47 def errors @errors end |
#issuer ⇒ NameID?
153 154 155 |
# File 'lib/saml2/message.rb', line 153 def issuer @issuer ||= NameID.from_xml(xml.at_xpath('saml:Issuer', Namespaces::ALL)) end |
Class Method Details
.from_xml(node) ⇒ Message
Create an appropriate SAML2::Message subclass instance to represent the given XML element.
When called on a subclass, it behaves the same as Base.from_xml
60 61 62 63 64 65 |
# File 'lib/saml2/message.rb', line 60 def from_xml(node) return super unless self == Message klass = Message.[node.name] raise UnknownMessage.new("Unknown message #{node.name}") unless klass klass.from_xml(node) end |
.parse(xml) ⇒ Message
Parses XML, and returns an appropriate SAML2::Message subclass instance.
74 75 76 77 78 79 80 |
# File 'lib/saml2/message.rb', line 74 def parse(xml) result = Message.from_xml(Nokogiri::XML(xml) { |config| config.strict }.root) raise UnexpectedMessage.new("Expected a #{self.name}, but got a #{result.class.name}") unless self == Message || result.class == self result rescue Nokogiri::XML::SyntaxError raise CorruptMessage end |
Instance Method Details
#from_xml(node) ⇒ void
This method returns an undefined value.
Parse an XML element into this object.
103 104 105 106 107 |
# File 'lib/saml2/message.rb', line 103 def from_xml(node) super @id = nil @issue_instant = nil end |
#id ⇒ String
135 136 137 |
# File 'lib/saml2/message.rb', line 135 def id @id ||= xml['ID'] end |
#issue_instant ⇒ Time
140 141 142 |
# File 'lib/saml2/message.rb', line 140 def issue_instant @issue_instant ||= Time.parse(xml['IssueInstant']) end |
#sign(x509_certificate, private_key, algorithm_name = :sha256) ⇒ self
Sign this object.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/saml2/message.rb', line 123 def sign(x509_certificate, private_key, algorithm_name = :sha256) super xml = @document.root # the Signature element must be right after the Issuer, so put it there issuer = xml.at_xpath("saml:Issuer", Namespaces::ALL) signature = xml.at_xpath("dsig:Signature", Namespaces::ALL) issuer.add_next_sibling(signature) self end |