Class: Saml::Kit::Document

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

Constant Summary collapse

PROTOCOL_XSD =
File.expand_path('./xsd/saml-schema-protocol-2.0.xsd', File.dirname(__FILE__)).freeze
NAMESPACES =
{
  "NameFormat": ::Saml::Kit::Namespaces::ATTR_SPLAT,
  "ds": ::Xml::Kit::Namespaces::XMLDSIG,
  "md": ::Saml::Kit::Namespaces::METADATA,
  "saml": ::Saml::Kit::Namespaces::ASSERTION,
  "samlp": ::Saml::Kit::Namespaces::PROTOCOL,
  'xmlenc' => ::Xml::Kit::Namespaces::XMLENC,
}.freeze
XPATH =
[
  '/samlp:AuthnRequest',
  '/samlp:LogoutRequest',
  '/samlp:LogoutResponse',
  '/samlp:Response',
].join('|')

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trustable

#signed?, #trusted?

Constructor Details

#initialize(xml, name:, configuration: Saml::Kit.configuration) ⇒ Document

Returns a new instance of Document.



24
25
26
27
28
# File 'lib/saml/kit/document.rb', line 24

def initialize(xml, name:, configuration: Saml::Kit.configuration)
  @configuration = configuration
  @content = xml
  @name = name
end

Class Method Details

.to_saml_document(xml, configuration: Saml::Kit.configuration) ⇒ Object

Returns the raw xml as a Saml::Kit SAML document.

Parameters:

  • xml (String)

    the raw xml string.

  • configuration (Saml::Kit::Configuration) (defaults to: Saml::Kit.configuration)

    the configuration to use for unpacking the document.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/saml/kit/document.rb', line 104

def to_saml_document(xml, configuration: Saml::Kit.configuration)
  xml_document = ::Xml::Kit::Document.new(xml, namespaces: {
                                            "samlp": ::Saml::Kit::Namespaces::PROTOCOL
                                          })
  constructor = {
    'AuthnRequest' => Saml::Kit::AuthenticationRequest,
    'LogoutRequest' => Saml::Kit::LogoutRequest,
    'LogoutResponse' => Saml::Kit::LogoutResponse,
    'Response' => Saml::Kit::Response,
  }[xml_document.find_by(XPATH).name] || InvalidDocument
  constructor.new(xml, configuration: configuration)
rescue StandardError => error
  Saml::Kit.logger.error(error)
  InvalidDocument.new(xml, configuration: configuration)
end

Instance Method Details

#destinationObject

Returns the Destination of the SAML document.



46
47
48
# File 'lib/saml/kit/document.rb', line 46

def destination
  root.fetch('Destination', nil)
end

#idObject

Returns the ID for the SAML document.



31
32
33
# File 'lib/saml/kit/document.rb', line 31

def id
  root.fetch('ID', nil)
end

#issue_instantObject

Returns the Destination of the SAML document.



51
52
53
# File 'lib/saml/kit/document.rb', line 51

def issue_instant
  Time.parse(root['IssueInstant'])
end

#issuerObject

Returns the Issuer for the SAML document.



36
37
38
# File 'lib/saml/kit/document.rb', line 36

def issuer
  root.fetch('Issuer', nil)
end

#to_hObject

Returns the SAML document returned as a Hash.



56
57
58
# File 'lib/saml/kit/document.rb', line 56

def to_h
  @xml_hash ||= Hash.from_xml(content) || {}
end

#to_sObject



88
89
90
# File 'lib/saml/kit/document.rb', line 88

def to_s
  to_xml
end

#to_xhtmlObject

Returns the SAML document as an XHTML string. This is useful for rendering in a web page.



69
70
71
# File 'lib/saml/kit/document.rb', line 69

def to_xhtml
  Nokogiri::XML(to_xml, &:noblanks).to_xhtml
end

#to_xml(pretty: false) ⇒ Object

Returns the SAML document as an XML string.

Parameters:

  • pretty (Boolean) (defaults to: false)

    formats the xml or returns the raw xml.



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

def to_xml(pretty: false)
  pretty ? to_nokogiri.to_xml(indent: 2) : content
end

#versionObject

Returns the Version of the SAML document.



41
42
43
# File 'lib/saml/kit/document.rb', line 41

def version
  root.fetch('Version', {})
end