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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trustable

#provider, #signature, #signature_verified!, #signed?, #trusted?

Methods included from Translatable

#error_message

Methods included from XsdValidatable

#matches_xsd?

Constructor Details

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



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

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

Instance Attribute Details

#configuration ⇒ Object (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#content ⇒ Object (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.builder_class ⇒ Object



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

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, configuration: Saml::Kit.configuration) ⇒ Object



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

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

Instance Method Details

#destination ⇒ Object



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

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

#expected_type? ⇒ Boolean



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

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

#id ⇒ Object



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

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

#issue_instant ⇒ Object



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

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

#issuer ⇒ Object



29
30
31
# File 'lib/saml/kit/document.rb', line 29

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

#to_h ⇒ Object



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

def to_h
  @xml_hash
end

#to_s ⇒ Object



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

def to_s
  to_xml
end

#to_xhtml ⇒ Object



58
59
60
# File 'lib/saml/kit/document.rb', line 58

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

#to_xml(pretty: false) ⇒ Object



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

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

#version ⇒ Object



33
34
35
# File 'lib/saml/kit/document.rb', line 33

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