From 4c7a68186c29d55786b138c7b228271b5b5c1daa Mon Sep 17 00:00:00 2001
From: Ben Johnson <ben.johnson@corgibytes.com>
Date: Mon, 10 Dec 2018 14:45:23 -0500
Subject: [PATCH] Add Password support to Redis library
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

For Redis instances that require authentication, the current Redis store does not work.  So, this commit adds optional support for utilizing the Predis password option.  It will pick up the password from the config file and pass it along when it instantiates the Predis client.

The test simply checks that adding the password option doesn't break the constructor. Since Predis parameters end up as protected, we can't verify that the instance contains the password.
---
 lib/SimpleSAML/Store/Redis.php           |  3 ++-
 tests/lib/SimpleSAML/Store/RedisTest.php | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lib/SimpleSAML/Store/Redis.php b/lib/SimpleSAML/Store/Redis.php
index 9091ddd63..310caa98f 100644
--- a/lib/SimpleSAML/Store/Redis.php
+++ b/lib/SimpleSAML/Store/Redis.php
@@ -31,13 +31,14 @@ class Redis extends Store
             $host = $config->getString('store.redis.host', 'localhost');
             $port = $config->getInteger('store.redis.port', 6379);
             $prefix = $config->getString('store.redis.prefix', 'SimpleSAMLphp');
+            $password = $config->getString('store.redis.password', '');
 
             $redis = new \Predis\Client(
                 [
                     'scheme' => 'tcp',
                     'host' => $host,
                     'port' => $port,
-                ],
+                ] + (!empty($password) ? ['password' => $password] : []),
                 [
                     'prefix' => $prefix,
                 ]
diff --git a/tests/lib/SimpleSAML/Store/RedisTest.php b/tests/lib/SimpleSAML/Store/RedisTest.php
index 7774a5dd2..b1e8cc0e0 100644
--- a/tests/lib/SimpleSAML/Store/RedisTest.php
+++ b/tests/lib/SimpleSAML/Store/RedisTest.php
@@ -88,6 +88,27 @@ class RedisTest extends TestCase
         $this->clearInstance($store, '\SimpleSAML\Store');
     }
 
+    /**
+     * @covers \SimpleSAML\Store::getInstance
+     * @covers \SimpleSAML\Store\Redis::__construct
+     * @test
+     */
+    public function testRedisInstanceWithPassword()
+    {
+        $config = Configuration::loadFromArray([
+            'store.type' => 'redis',
+            'store.redis.prefix' => 'phpunit_',
+            'store.redis.password' => 'password',
+        ], '[ARRAY]', 'simplesaml');
+
+        $store = Store::getInstance();
+
+        $this->assertInstanceOf('SimpleSAML\Store\Redis', $store);
+
+        $this->clearInstance($config, '\SimpleSAML\Configuration');
+        $this->clearInstance($store, '\SimpleSAML\Store');
+    }
+
     /**
      * @covers \SimpleSAML\Store\Redis::get
      * @covers \SimpleSAML\Store\Redis::set
-- 
GitLab