Class: Saml::Kit::Document

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Buildable, 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trustable

#certificate, #fingerprint, #provider, #signature_verified!, #signed?, #trusted?

Methods included from XsdValidatable

#error_message, #matches_xsd?

Constructor Details

#initialize(xml, name:) ⇒ Document

Returns a new instance of Document.



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

def initialize(xml, name:)
  @content = xml
  @name = name
  @xml_hash = Hash.from_xml(xml) || {}
end

Instance Attribute Details

#content ⇒ Object (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.builder_class ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/saml/kit/document.rb', line 81

def builder_class
  case name
  when Saml::Kit::Response.to_s
    Saml::Kit::Builders::Response
  when Saml::Kit::LogoutResponse.to_s
    Saml::Kit::Builders::LogoutResponse
  when Saml::Kit::AuthenticationRequest.to_s
    Saml::Kit::Builders::AuthenticationRequest
  when Saml::Kit::LogoutRequest.to_s
    Saml::Kit::Builders::LogoutRequest
  else
    raise ArgumentError.new("Unknown SAML Document #{name}")
  end
end

.to_saml_document(xml) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/saml/kit/document.rb', line 65

def to_saml_document(xml)
  hash = Hash.from_xml(xml)
  if hash['Response'].present?
    Response.new(xml)
  elsif hash['LogoutResponse'].present?
    LogoutResponse.new(xml)
  elsif hash['AuthnRequest'].present?
    AuthenticationRequest.new(xml)
  elsif hash['LogoutRequest'].present?
    LogoutRequest.new(xml)
  end
rescue => error
  Saml::Kit.logger.error(error)
  InvalidDocument.new(xml)
end

Instance Method Details

#destination ⇒ Object



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

def destination
  to_h.fetch(name, {}).fetch('Destination', nil)
end

#expected_type? ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/saml/kit/document.rb', line 43

def expected_type?
  return false if to_xml.blank?
  to_h[name].present?
end

#id ⇒ Object



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

def id
  to_h.fetch(name, {}).fetch('ID', nil)
end

#issue_instant ⇒ Object



39
40
41
# File 'lib/saml/kit/document.rb', line 39

def issue_instant
  to_h[name]['IssueInstant']
end

#issuer ⇒ Object



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

def issuer
  to_h.fetch(name, {}).fetch('Issuer', nil)
end

#to_h ⇒ Object



48
49
50
# File 'lib/saml/kit/document.rb', line 48

def to_h
  @xml_hash
end

#to_s ⇒ Object



60
61
62
# File 'lib/saml/kit/document.rb', line 60

def to_s
  to_xml
end

#to_xhtml ⇒ Object



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

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

#to_xml(pretty: false) ⇒ Object



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

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

#version ⇒ Object



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

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