From 9a4dcff9f868551832a8d14b9f9c026b793711b1 Mon Sep 17 00:00:00 2001
From: David Maus <maus@hab.de>
Date: Wed, 9 Mar 2016 09:19:55 +0100
Subject: [PATCH] Handle authentication module runtime errors

* lib/Auth/Source/Pica.php (login): Handle authentication module runtime
  errors.
  (getAuthenticationModule): Declare protected to ease unit test.
---
 lib/Auth/Source/Pica.php |  8 ++++++--
 tests/PicaTest.php       | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/lib/Auth/Source/Pica.php b/lib/Auth/Source/Pica.php
index 6deb9da..9766405 100644
--- a/lib/Auth/Source/Pica.php
+++ b/lib/Auth/Source/Pica.php
@@ -60,7 +60,11 @@ class sspmod_pica_Auth_Source_Pica extends sspmod_core_Auth_UserPassBase
     protected function login ($username, $password)
     {
         $module = $this->getAuthenticationModule();
-        $attributes = $module->authenticate($username, $password);
+        try {
+            $attributes = $module->authenticate($username, $password);
+        } catch (RuntimeException $error) {
+            throw new SimpleSAML_Error_AuthSource('pica', $error->getMessage(), $error);
+        }
         if ($attributes === false) {
             throw new SimpleSAML_Error_Error('WRONGUSERPASS');
         }
@@ -118,7 +122,7 @@ class sspmod_pica_Auth_Source_Pica extends sspmod_core_Auth_UserPassBase
      *
      * @return Auth\AuthenticationInterface
      */
-    private function getAuthenticationModule ()
+    protected function getAuthenticationModule ()
     {
         return call_user_func($this->factory);
     }
diff --git a/tests/PicaTest.php b/tests/PicaTest.php
index a5b060b..1ff87a9 100644
--- a/tests/PicaTest.php
+++ b/tests/PicaTest.php
@@ -48,4 +48,30 @@ class PicaTest extends TestCase
                 ->getMock();
         $source->createAuthenticationModuleFactory($config);
     }
+
+    /**
+     * @expectedException SimpleSAML_Error_AuthSource
+     */
+    public function testExceptionOnAuthenticationModuleRuntimeError ()
+    {
+        $module = $this->getMockForAbstractClass('HAB\Pica\Auth\AuthenticationInterface');
+        $module
+            ->expects($this->any())
+            ->method('authenticate')
+            ->will($this->throwException(new RuntimeException()));
+
+        $source = $this
+                ->getMockBuilder('sspmod_pica_Auth_Source_Pica')
+                ->disableOriginalConstructor()
+                ->setMethods(array('getAuthenticationModule'))
+                ->getMock();
+        $source
+            ->expects($this->any())
+            ->method('getAuthenticationModule')
+            ->will($this->returnValue($module));
+
+        $method = new ReflectionMethod($source, 'login');
+        $method->setAccessible(true);
+        $method->invoke($source, 'foo', 'bar');
+    }
 }
\ No newline at end of file
-- 
GitLab