Commit de6f712

mo <mo@mokhan.ca>
2017-11-03 16:14:40
ensure AuthnRequest present.
1 parent 8e07117
Changed files (2)
lib/saml/kit/authentication_request.rb
@@ -3,25 +3,27 @@ module Saml
     class AuthenticationRequest
       include ActiveModel::Validations
       validates_presence_of :content
+      validate :must_be_request
       validate :must_have_valid_signature
 
-      attr_reader :content
+      attr_reader :content, :name
 
       def initialize(xml)
         @content = xml
+        @name = "AuthnRequest"
         @hash = Hash.from_xml(@content)
       end
 
       def id
-        @hash['AuthnRequest']['ID']
+        @hash[name]['ID']
       end
 
       def acs_url
-        @hash['AuthnRequest']['AssertionConsumerServiceURL']
+        @hash[name]['AssertionConsumerServiceURL']
       end
 
       def issuer
-        @hash['AuthnRequest']['Issuer']
+        @hash[name]['Issuer']
       end
 
       def to_xml
@@ -40,12 +42,20 @@ module Saml
         xml = Saml::Kit::Xml.new(to_xml)
         xml.valid?
         xml.errors.each do |error|
-          errors[:metadata] << error
+          errors[:base] << error
+        end
+      end
+
+      def must_be_request
+        return if @hash.nil?
+
+        if @hash[name].blank?
+          errors[:base] << error_message(:invalid)
         end
       end
 
       def error_message(key)
-        I18n.translate(key, scope: "saml/kit.errors.#{descriptor_name}")
+        I18n.translate(key, scope: "saml/kit.errors.#{name}")
       end
 
       class Builder
spec/saml/authentication_request_spec.rb
@@ -51,11 +51,15 @@ RSpec.describe Saml::Kit::AuthenticationRequest do
       raw_xml.gsub!(issuer, 'corrupt')
       subject = described_class.new(raw_xml)
       expect(subject).to_not be_valid
-      puts subject.errors.full_messages.inspect
     end
 
     it 'is invalid when blank' do
       expect(described_class.new('')).to be_invalid
     end
+
+    it 'is invalid when not an AuthnRequest' do
+      xml = Saml::Kit::IdentityProviderMetadata::Builder.new.to_xml
+      expect(described_class.new(xml)).to be_invalid
+    end
   end
 end