module Saml module Kit class Document PROTOCOL_XSD = File.expand_path("./xsd/saml-schema-protocol-2.0.xsd", File.dirname(__FILE__)).freeze include ActiveModel::Validations include XsdValidatable include Translatable include Trustable include Buildable validates_presence_of :content validates_presence_of :id validate :must_match_xsd validate :must_be_expected_type validate :must_be_valid_version def initialize(xml, name:, configuration: Saml::Kit.configuration) @configuration = configuration @content = xml @name = name @xml_hash = Hash.from_xml(xml) || {} end # Returns the ID for the SAML document. def id to_h.fetch(name, {}).fetch('ID', nil) end # Returns the Issuer for the SAML document. def issuer to_h.fetch(name, {}).fetch('Issuer', nil) end # Returns the Version of the SAML document. def version to_h.fetch(name, {}).fetch('Version', {}) end # Returns the Destination of the SAML document. def destination to_h.fetch(name, {}).fetch('Destination', nil) end # Returns the Destination of the SAML document. def issue_instant Time.parse(to_h[name]['IssueInstant']) end # Returns the SAML document returned as a Hash. def to_h @xml_hash end # Returns the SAML document as an XML string. # # @param pretty [Boolean] formats the xml or returns the raw xml. def to_xml(pretty: false) pretty ? Nokogiri::XML(content).to_xml(indent: 2) : content end # Returns the SAML document as an XHTML string. # This is useful for rendering in a web page. def to_xhtml Nokogiri::XML(content, &:noblanks).to_xhtml end def to_s to_xml end class << self # Returns the raw xml as a Saml::Kit SAML document. # # @param xml [String] the raw xml string. # @param configuration [Saml::Kit::Configuration] the configuration to use for unpacking the document. 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 # @!visibility private def builder_class # :nodoc: 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 end private attr_reader :content, :name, :configuration def must_match_xsd matches_xsd?(PROTOCOL_XSD) end def must_be_expected_type return if to_h.nil? errors[:base] << error_message(:invalid) unless expected_type? end def expected_type? return false if to_xml.blank? to_h[name].present? end def must_be_valid_version return unless expected_type? return if "2.0" == version errors[:version] << error_message(:invalid_version) end end end end