Commit 3e36caf

mo <mo.khan@gmail.com>
2017-11-17 23:49:30
AuthenticationRequest < Document
1 parent ef3c1e4
lib/saml/kit/authentication_request.rb
@@ -1,10 +1,6 @@
 module Saml
   module Kit
-    class AuthenticationRequest
-      PROTOCOL_XSD = File.expand_path("./xsd/saml-schema-protocol-2.0.xsd", File.dirname(__FILE__)).freeze
-      include XsdValidatable
-      include ActiveModel::Validations
-
+    class AuthenticationRequest < Document
       validates_presence_of :content
       validates_presence_of :acs_url, if: :login_request?
       validate :must_be_request
@@ -12,12 +8,8 @@ module Saml
       validate :must_be_registered
       validate :must_match_xsd
 
-      attr_reader :content, :name
-
       def initialize(xml)
-        @content = xml
-        @name = "AuthnRequest"
-        @hash = Hash.from_xml(@content)
+        super(xml, name: "AuthnRequest")
       end
 
       def query_string_parameter
@@ -62,18 +54,6 @@ module Saml
         to_h[name]['Signature'].present?
       end
 
-      def to_h
-        @hash
-      end
-
-      def to_xml
-        @content
-      end
-
-      def to_s
-        to_xml
-      end
-
       def response_for(user)
         Response::Builder.new(user, self).build
       end
@@ -121,7 +101,7 @@ module Saml
       end
 
       def must_be_request
-        return if @hash.nil?
+        return if to_h.nil?
 
         errors[:base] << error_message(:invalid) unless login_request?
       end
@@ -132,7 +112,7 @@ module Saml
 
       def login_request?
         return false if to_xml.blank?
-        @hash[name].present?
+        to_h[name].present?
       end
 
       class Builder
lib/saml/kit/document.rb
@@ -1,7 +1,30 @@
-
 module Saml
   module Kit
     class Document
+      PROTOCOL_XSD = File.expand_path("./xsd/saml-schema-protocol-2.0.xsd", File.dirname(__FILE__)).freeze
+      include XsdValidatable
+      include ActiveModel::Validations
+
+      attr_reader :content, :name
+
+      def initialize(xml, name:)
+        @content = xml
+        @name = name
+        @xml_hash = Hash.from_xml(xml)
+      end
+
+      def to_h
+        @xml_hash
+      end
+
+      def to_xml
+        @content
+      end
+
+      def to_s
+        to_xml
+      end
+
       def self.to_saml_document(xml)
         hash = Hash.from_xml(xml)
         if hash['Response'].present?
lib/saml/kit.rb
@@ -14,11 +14,11 @@ require "xmldsig"
 
 require "saml/kit/serializable"
 require "saml/kit/xsd_validatable"
+require "saml/kit/document"
 
 require "saml/kit/authentication_request"
 require "saml/kit/binding"
 require "saml/kit/configuration"
-require "saml/kit/document"
 require "saml/kit/default_registry"
 require "saml/kit/fingerprint"
 require "saml/kit/logout_response"